From 4759b52c04aebb68923431c59878e6367afcef6e Mon Sep 17 00:00:00 2001 From: Mike Glazer Date: Wed, 2 Nov 2016 12:20:58 -0700 Subject: [PATCH] Support env vars in static-launcher.yml (#137) --- readme.md | 4 ++++ .../gradle/javadist/DistributionExtension.groovy | 13 +++++++++++++ .../gradle/javadist/tasks/LaunchConfigTask.groovy | 4 ++++ .../javadist/DistributionExtensionTest.groovy | 7 +++++++ .../javadist/JavaDistributionPluginTests.groovy | 6 ++++++ 5 files changed, 34 insertions(+) diff --git a/readme.md b/readme.md index 11e66fb7..adf1b4df 100644 --- a/readme.md +++ b/readme.md @@ -52,6 +52,7 @@ program for a default run configuration: serviceName 'my-service' mainClass 'com.palantir.foo.bar.MyServiceMainClass' args 'server', 'var/conf/my-service.yml' + env 'KEY1': 'value1', 'KEY2': 'value1' } The `distribution` block offers the following options: @@ -61,6 +62,9 @@ The `distribution` block offers the following options: * (optional) `args` a list of arguments to supply when running `start`. * (optional) `checkArgs` a list of arguments to supply to the monitoring script, if omitted, no monitoring script will be generated. + * (optional) `env` a map of environment variables that will be placed into the `env` block + of the static launcher config. See [go-java-launcher](https://github.com/palantir/go-java-launcher) + for details on the custom environment block. * (optional) `defaultJvmOpts` a list of default JVM options to set on the program. * (optional) `enableManifestClasspath` a boolean flag; if set to true, then the explicit Java classpath is omitted from the generated Windows start script and instead inferred diff --git a/src/main/groovy/com/palantir/gradle/javadist/DistributionExtension.groovy b/src/main/groovy/com/palantir/gradle/javadist/DistributionExtension.groovy index 44c16489..02123ad5 100644 --- a/src/main/groovy/com/palantir/gradle/javadist/DistributionExtension.groovy +++ b/src/main/groovy/com/palantir/gradle/javadist/DistributionExtension.groovy @@ -39,6 +39,7 @@ class DistributionExtension { private List args = [] private List checkArgs = [] private List defaultJvmOpts = [] + private Map env = [:] private boolean enableManifestClasspath = false private String javaHome = null private List excludeFromVar = ['log', 'run'] @@ -91,6 +92,14 @@ class DistributionExtension { this.javaHome = javaHome } + public void setEnv(Map env) { + this.env = env + } + + public void env(Map env) { + this.env.putAll(env) + } + public void excludeFromVar(String... excludeFromVar) { this.excludeFromVar.addAll(excludeFromVar) } @@ -127,6 +136,10 @@ class DistributionExtension { return enableManifestClasspath } + public Map getEnv() { + return env + } + public String getJavaHome() { return javaHome } diff --git a/src/main/groovy/com/palantir/gradle/javadist/tasks/LaunchConfigTask.groovy b/src/main/groovy/com/palantir/gradle/javadist/tasks/LaunchConfigTask.groovy index 55e839ca..5a89cec1 100644 --- a/src/main/groovy/com/palantir/gradle/javadist/tasks/LaunchConfigTask.groovy +++ b/src/main/groovy/com/palantir/gradle/javadist/tasks/LaunchConfigTask.groovy @@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.dataformat.yaml.YAMLFactory import com.palantir.gradle.javadist.JavaDistributionPlugin import groovy.transform.EqualsAndHashCode +import groovy.transform.ToString import org.gradle.api.file.FileCollection import org.gradle.api.plugins.JavaPlugin import org.gradle.api.tasks.Input @@ -36,6 +37,7 @@ class LaunchConfigTask extends BaseTask { } @EqualsAndHashCode + @ToString public static class StaticLaunchConfig { // keep in sync with StaticLaunchConfig struct in go-java-launcher String configType = "java" @@ -45,6 +47,7 @@ class LaunchConfigTask extends BaseTask { List classpath List jvmOpts List args + Map env } @Input @@ -90,6 +93,7 @@ class LaunchConfigTask extends BaseTask { config.classpath = relativizeToServiceLibDirectory( project.tasks[JavaPlugin.JAR_TASK_NAME].outputs.files + project.configurations.runtime) config.jvmOpts = distributionExtension().defaultJvmOpts + config.env = distributionExtension().env return config } diff --git a/src/test/groovy/com/palantir/gradle/javadist/DistributionExtensionTest.groovy b/src/test/groovy/com/palantir/gradle/javadist/DistributionExtensionTest.groovy index 53a7e5a4..40ff193b 100644 --- a/src/test/groovy/com/palantir/gradle/javadist/DistributionExtensionTest.groovy +++ b/src/test/groovy/com/palantir/gradle/javadist/DistributionExtensionTest.groovy @@ -38,6 +38,9 @@ class DistributionExtensionTest extends Specification { excludeFromVar 'a', 'b' excludeFromVar 'c', 'd' + + env 'a': 'b' + env 'c': 'd' } then: @@ -45,6 +48,7 @@ class DistributionExtensionTest extends Specification { ext.checkArgs == ['a', 'b', 'c', 'd'] ext.defaultJvmOpts == DistributionExtension.requiredJvmOpts + ['a', 'b', 'c', 'd'] ext.excludeFromVar == ['log', 'run', 'a', 'b', 'c', 'd'] + ext.env == ['a': 'b', 'c': 'd'] } def 'collection setters replace existing data'() { @@ -61,6 +65,8 @@ class DistributionExtensionTest extends Specification { setDefaultJvmOpts(['c', 'd']) setExcludeFromVar(['a', 'b']) setExcludeFromVar(['c', 'd']) + setEnv(['a': 'b', 'c': 'd']) + setEnv(['foo': 'bar']) } then: @@ -68,6 +74,7 @@ class DistributionExtensionTest extends Specification { ext.checkArgs == ['c', 'd'] ext.defaultJvmOpts == DistributionExtension.requiredJvmOpts + ['c', 'd'] ext.excludeFromVar == ['c', 'd'] + ext.env == ['foo': 'bar'] } def 'serviceGroup uses project group as default'() { diff --git a/src/test/groovy/com/palantir/gradle/javadist/JavaDistributionPluginTests.groovy b/src/test/groovy/com/palantir/gradle/javadist/JavaDistributionPluginTests.groovy index 6d08d1a2..345957ee 100644 --- a/src/test/groovy/com/palantir/gradle/javadist/JavaDistributionPluginTests.groovy +++ b/src/test/groovy/com/palantir/gradle/javadist/JavaDistributionPluginTests.groovy @@ -281,6 +281,8 @@ class JavaDistributionPluginTests extends GradleTestSpec { javaHome 'foo' args 'myArg1', 'myArg2' checkArgs 'myCheckArg1', 'myCheckArg2' + env "key1": "val1", + "key2": "val2" }'''.stripIndent() file('src/main/java/test/Test.java') << "package test;\npublic class Test {}" @@ -307,6 +309,10 @@ class JavaDistributionPluginTests extends GradleTestSpec { '-verbose:gc', '-Xmx4M', '-Djavax.net.ssl.trustStore=truststore.jks']) + expectedStaticConfig.setEnv([ + "key1": "val1", + "key2": "val2" + ]) def actualStaticConfig = new ObjectMapper(new YAMLFactory()).readValue( file('dist/service-name-0.1/service/bin/launcher-static.yml'), LaunchConfigTask.StaticLaunchConfig) expectedStaticConfig == actualStaticConfig