我们有一个可以在本地或远程服务器上运行的应用程序。在本地运行时,需要设置代理,因为应用程序使用云上的另一项服务。
这样做是这样的:
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 参数;问题是如何在
commandLine
中“注入(inject)”这些代理设置动态的。到目前为止,我尝试过:
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 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') }))
关于Gradle 使用动态参数列表执行命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52002618/