2012-06-19 15 views
30

Sto cercando di implementare un'attività gradle per creare dinamicamente un file buildsignature.properties da una serie di valori di variabili di ambiente ed esecuzioni di shell. Ce l'ho quasi sempre funzionante, ma non riesco a ottenere l'output dei comandi della shell. Ecco il mio compito ...Come utilizzare l'output exec() nel gradle

task generateBuildSignature << { 
    ext.whoami = exec() { 
     executable = "whoami" 
    } 
    ext.hostname = exec() { 
     executable = "hostname" 
    } 
    ext.buildTag = System.env.BUILD_TAG ?: "dev" 

    ant.propertyfile(
     file: "${buildDir}/buildsignature.properties", 
     comment: "This file is automatically generated - DO NOT EDIT!") { 
     entry(key: "version", value: "${project.version}") 
     entry(key: "buildTimestamp", value: "${new Date().format('yyyy-MM-dd HH:mm:ss z')}") 
     entry(key: "buildUser", value: "${ext.whoami}") 
     entry(key: "buildSystem", value: "${ext.hostname}") 
     entry(key: "buildTag", value: "$ext.buildTag") 
    } 
} 

Ma la proprietà campo risultante non ottiene i risultati desiderati per buildUser e di compilazione.

#This file is automatically generated - DO NOT EDIT! 
#Mon, 18 Jun 2012 18:14:14 -0700 
version=1.1.0 
buildTimestamp=2012-06-18 18\:14\:14 PDT 
buildUs[email protected]2e6a54f9 
buildSyst[email protected]46f0bf3d 
buildTag=dev 

Come faccio ad avere buildUser e di compilazione per abbinare l'uscita del exec corrispondente piuttosto che qualche difetto ExecResultImpl toString? Questo davvero non può essere così difficile, vero?

risposta

28

Questo post descrive come analizzare l'uscita da una chiamata Exec. Di seguito troverai due attività che eseguono i tuoi comandi.

task setWhoamiProperty { 
    doLast { 
     new ByteArrayOutputStream().withStream { os -> 
      def result = exec { 
       executable = 'whoami' 
       standardOutput = os 
      } 
      ext.whoami = os.toString() 
     } 
    } 
} 

task setHostnameProperty { 
    doLast { 
     new ByteArrayOutputStream().withStream { os -> 
      def result = exec { 
       executable = 'hostname' 
       standardOutput = os 
      } 
      ext.hostname = os.toString() 
     } 
    } 
} 

task printBuildInfo { 
    dependsOn setWhoamiProperty, setHostnameProperty 
    doLast { 
     println whoami 
     println hostname 
    } 
} 

C'è in realtà un modo più semplice per ottenere queste informazioni senza dover richiamare un comando di shell.

Attualmente utenti registrati: System.getProperty('user.name')

Nome host: InetAddress.getLocalHost().getHostName()

+0

Easy è meglio. Non mi aspettavo davvero che catturare l'output di un comando shell richiedesse l'analisi manuale di un flusso di output. Non importa. Grazie per la guida. –

+3

Domanda veloce, manca il tuo codice 'standardOutput = os' ?? –

+0

Quando facciamo questo sembra essere asincrono. Prova ad emettere whoami e hostname prima che esistano. Idee? –

50

Questa è la mia sintassi preferito per ottenere lo stdout da exec:

def stdout = new ByteArrayOutputStream() 
exec{ 
    commandLine "whoami" 
    standardOutput = stdout; 
} 
println "Output:\n$stdout"; 

Trovato qui: http://gradle.1045684.n5.nabble.com/external-process-execution-td1431883.html (Si noti che la pagina ha un errore di battitura e cita ByteArrayInputStream invece di ByteArrayOutputStream)

+0

gradle 3.3: questo non funziona. Se commento print, non succede nulla, se lo rimprovero - ci sono molte nuove righe in uscita, è strano ma nemmeno una parola "Output" – Tertium

2

parafrasato dalla Gradle docs for Exec:

task execSomething { 
    exec { 
    workingDir '/some/dir' 
    commandLine '/some/command', 'arg' 

    ... 
    //store the output instead of printing to the console: 
    standardOutput = new ByteArrayOutputStream() 

    //extension method execSomething.output() can be used to obtain the output: 
    ext.output = { 
     return standardOutput.toString() 
    } 
    } 
} 
Problemi correlati