如何在gradle中使用exec()输出 [英] How to use exec() output in gradle

查看:649
本文介绍了如何在gradle中使用exec()输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图执行一个gradle任务,从一系列环境变量值和shell执行动态地创建一个buildsignature.properties文件。我主要工作,但我似乎无法得到shell命令的输出。这是我的任务......

  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,
条目(key:version,value:$ {project.version})
条目(键值:buildTimestamp,value:$ {new Date()。format('yyyy-MM-dd HH:mm:ss z')})
entry(key:buildUser,value:$ (key:buildTag,value:$ ext.buildTag)
entry(key:buildSystem,value:$ {ext.hostname}) )
}
}

但是得到的属性字段没有得到所需的结果为buildUser和buildSystem。

 #这个文件是自动生成的 - 不要编辑! 
#Mon,2012年6月18日18:14:14 -0700
版本= 1.1.0
buildTimestamp = 2012-06-18 18 \:14 \:14 PDT
buildUser=org.gradle.process.internal.DefaultExecHandle$ExecResultImpl@2e6a54f9
buildSystem=org.gradle.process.internal.DefaultExecHandle$ExecResultImpl@46f0bf3d
buildTag = dev

如何获取buildUser和buildSystem以匹配相应的exec的输出而不是某些默认的ExecResultImpl toString?这真的不能那么辛苦,可以吗? Exec 中的输出, code>调用。下面你会发现运行你的命令的两个任务。

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



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



任务printBuildInfo {
dependsOn setWhoamiProperty,setHostnameProperty
doLast {
println whoami
println hostname
}

$ / code>

实际上有一种更简单的方法可以获得这些信息,而无需调用shell命令。



当前登录用户: System.getProperty('user.name')



主机名: InetAddress.getLocalHost()。getHostName()


I am trying to implement a gradle task to dynamically create a buildsignature.properties file from a series of environment variable values and shell executions. I have it mostly working, but I can't seem to get the output of the shell commands. Here's my task...

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" )
    }
}

But the resulting properties field does not get the desired results for buildUser and buildSystem.

#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
buildUser=org.gradle.process.internal.DefaultExecHandle$ExecResultImpl@2e6a54f9
buildSystem=org.gradle.process.internal.DefaultExecHandle$ExecResultImpl@46f0bf3d
buildTag=dev

How do I get buildUser and buildSystem to match the output of the corresponding exec rather than some default ExecResultImpl toString? This really can't be that hard, can it?

解决方案

This post describes how to parse the output from an Exec invocation. Below you'll find two tasks that run your commands.

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
    }
}

There's actually an easier way to get this information without having to invoke a shell command.

Currently logged in user: System.getProperty('user.name')

Hostname: InetAddress.getLocalHost().getHostName()

这篇关于如何在gradle中使用exec()输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆