Gradle执行带有动态参数列表的commandLine

时间:2018-08-24 10:33:42

标签: gradle command-line

我们有一个可以在本地或远程服务器上运行的应用程序。在本地运行时,由于该应用程序使用云上的另一项服务,因此需要设置代理。 这样做是这样的:

commandLine "java", "-Xmx227m",
        "-Dapplication.name=showcase-rest",
        "-Dserver.port=$applicationPort",
        "-Dspring.profiles.active=$springActiveProfiles",
        "-Didm.realm=develop",
        "-Dhttp.proxyHost=10.xx.xxx.129", "-Dhttp.proxyPort=3xxx",
        "-Dhttps.proxyHost=10.xx.xxx.129", "-Dhttps.proxyPort=3xxx",
        "-Dhttp.nonProxyHosts=localhost|127.0.0.1",
        "-jar", tasks.jar.destinationDir.toString() + "/showcase-rest-SNAPSHOT.jar"

但是,当在云上的适当服务器上运行时,我们不需要代理设置:

commandLine "java", "-Xmx227m",
        "-Dapplication.name=showcase-rest",
        "-Dserver.port=$applicationPort",
        "-Dspring.profiles.active=$springActiveProfiles",
        "-Didm.realm=develop",
        "-jar", tasks.jar.destinationDir.toString() + "/showcase-rest-SNAPSHOT.jar"

那将如何实现?

我知道如何提供和解析jvm参数以进行gradle;问题是如何动态地将这些代理设置“注入” commandLine

到目前为止,我已经尝试过:

    def proxyConfig = ["-Dhttp.proxyHost=10.xx.xxx.129", "-Dhttp.proxyPort=3xxx",
                       "-Dhttps.proxyHost=10.xx.xxx.129", "-Dhttps.proxyPort=3xxx",
                               "-Dhttp.nonProxyHosts=localhost|127.0.0.1"] as List<String>
commandLine "java", "-Xmx227m",
        "-Dapplication.name=showcase-rest",
        "-Dserver.port=$applicationPort",
        "-Dspring.profiles.active=$springActiveProfiles",
        "-Didm.realm=develop",
        proxyConfig,
        "-jar", tasks.jar.destinationDir.toString() + "/showcase-rest-SNAPSHOT.jar"

但是显然那是行不通的。

2 个答案:

答案 0 :(得分:2)

尝试一下:

def params = ["java", "-Xmx227m",
        "-Dapplication.name=showcase-rest",
        "-Dserver.port=$applicationPort",
        "-Dspring.profiles.active=$springActiveProfiles",
        "-Didm.realm=develop",
        "-Dhttp.proxyHost=10.xx.xxx.129", "-Dhttp.proxyPort=3xxx",
        "-Dhttps.proxyHost=10.xx.xxx.129", "-Dhttps.proxyPort=3xxx",
        "-Dhttp.nonProxyHosts=localhost|127.0.0.1",
        "-jar", tasks.jar.destinationDir.toString() + "/showcase-rest-SNAPSHOT.jar"]

def withProxy = true

commandLine (*(params.findAll { withProxy || !it.toLowerCase().contains('proxy') }))

答案 1 :(得分:0)

谢谢ToYonos。我的解决方法(不是groovy-ninja)如下:

def proxyConfig = ["-Dhttp.proxyHost=10.xx.xxx.129",
                   "-Dhttp.proxyPort=3xxx",
                   "-Dhttps.proxyHost=10.xx.xxx.129",
                   "-Dhttps.proxyPort=3xxx",
                   "-Dhttp.nonProxyHosts=localhost|127.0.0.1"]
if (springActiveProfiles == 'cicd') {
    proxyConfig = []
}
println("proxy config: $proxyConfig")
def args = ["java", "-Xmx227m",
            "-Dapplication.name=showcase-rest",
            "-Dserver.port=$applicationPort",
            "-Dspring.profiles.active=$springActiveProfiles",
            "-Didm.realm=develop"]
def jarArg = ["-jar", tasks.jar.destinationDir.toString() + "/showcase-rest-SNAPSHOT.jar"]
args.addAll(proxyConfig)
args.addAll(jarArg)
println("Running with arguments: $args")
commandLine args