【问题标题】:executing shell script and using its output as input to next gradle task执行 shell 脚本并将其输出用作下一个 gradle 任务的输入
【发布时间】:2016-06-28 08:40:10
【问题描述】:

我使用 gradle 进行构建和发布,所以我的 gradle 脚本执行一个 shell 脚本。 shell 脚本输出一个 ip 地址,该地址必须作为输入提供给我的下一个 gradle ssh 任务。我能够获得输出并在控制台上打印,但无法将此输出用作下一个任务的输入。

remotes {
  web01 {
    def ip = exec {
    commandLine './returnid.sh'
    }
    println ip  --> i am able to see the ip address on console
    role 'webServers'
    host = ip  --> i tried referring as $ip '$ip' , both results into syntax error
    user = 'ubuntu'
    password = 'ubuntu'
  }
}

task checkWebServers1 << {

  ssh.run {
    session(remotes.web01) {
    execute 'mkdir -p /home/ubuntu/abc3'
}
}
}

但它会导致错误“

What went wrong:
Execution failed for task ':checkWebServers1'.
 java.net.UnknownHostException: {exitValue=0, failure=null}"

谁能帮助我以正确的语法使用输出变量或提供一些可以帮助我的提示。

提前致谢

【问题讨论】:

    标签: gradle build gradle-ssh-plugin javaexec-gradle-plugin


    【解决方案1】:

    它不起作用的原因是,exec 调用返回是ExecResult(这里是JavaDoc description),它不是执行的文本输出。

    如果您需要获取文本输出,则必须指定exec 任务的standardOutput 属性。可以这样做:

    remotes {
        web01 {
            def ip = new ByteArrayOutputStream()
            exec {
                commandLine './returnid.sh'
                standardOutput = ip
            }
            println ip
            role 'webServers'
            host = ip.toString().split("\n")[2].trim()
            user = 'ubuntu'
            password = 'ubuntu'
        }
    }
    

    请注意,默认情况下ip值会有多行输出,包括命令本身,因此必须对其进行解析以获得正确的输出,对于我的Win机器,可以这样做:

    ip.toString().split("\n")[2].trim()
    

    这里只取输出的第一行。

    【讨论】:

    • 非常感谢@Stanislav。我使用 def values = ip.toString().split("\n") 并分配了 host = values[0] 。
    猜你喜欢
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 2015-03-08
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 2021-07-08
    相关资源
    最近更新 更多