Nestor Milyaev
Nestor Milyaev

Reputation: 6595

Gradle execute commandLine with dynamic list of parameters

We have an application that one can run either locally or on a remote server. When run locally, one needs to set proxies as the app uses another service on the cloud. This is done like that:

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"

However, when run on proper server on the cloud, we don't need the proxy settings:

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"

How that could be achieved?

I know how to provide and parse jvm arguments to gradle; the question is how can I "inject" these proxy settings in the commandLine dynamically.

So far I tried:

    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"

But obviously that doesn't work.

Upvotes: 1

Views: 570

Answers (2)

Nestor Milyaev
Nestor Milyaev

Reputation: 6595

Thank you ToYonos. My solution, not as groovy-ninja, is below:

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

Upvotes: 0

ToYonos
ToYonos

Reputation: 16833

Try this :

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

Upvotes: 3

Related Questions