Gradle使用动态参数列表执行命令行

Gradle使用动态参数列表执行命令行,gradle,command-line,Gradle,Command Line,我们有一个可以在本地或远程服务器上运行的应用程序。在本地运行时,需要设置代理,因为应用程序使用云上的其他服务。 这是这样做的: commandLine "java", "-Xmx227m", "-Dapplication.name=showcase-rest", "-Dserver.port=$applicationPort", "-Dspring.profiles.active=$springActiveProfiles", "-

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

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"
如何实现这一目标

我知道如何向gradle提供和解析jvm参数;问题是如何在
命令行中动态地“注入”这些代理设置

到目前为止,我试过:

    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"
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”]作为列表
命令行“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”
但显然这不起作用。

试试这个:

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

谢谢你,托约诺斯。我的解决方案(不是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