Skip to content

Commit

Permalink
Support env vars in static-launcher.yml (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
mglazer authored and uschi2000 committed Nov 2, 2016
1 parent ef47254 commit 4759b52
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class DistributionExtension {
private List<String> args = []
private List<String> checkArgs = []
private List<String> defaultJvmOpts = []
private Map<String,String> env = [:]
private boolean enableManifestClasspath = false
private String javaHome = null
private List<String> excludeFromVar = ['log', 'run']
Expand Down Expand Up @@ -91,6 +92,14 @@ class DistributionExtension {
this.javaHome = javaHome
}

public void setEnv(Map<String, String> env) {
this.env = env
}

public void env(Map<String, String> env) {
this.env.putAll(env)
}

public void excludeFromVar(String... excludeFromVar) {
this.excludeFromVar.addAll(excludeFromVar)
}
Expand Down Expand Up @@ -127,6 +136,10 @@ class DistributionExtension {
return enableManifestClasspath
}

public Map<String, String> getEnv() {
return env
}

public String getJavaHome() {
return javaHome
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -45,6 +47,7 @@ class LaunchConfigTask extends BaseTask {
List<String> classpath
List<String> jvmOpts
List<String> args
Map<String,String> env
}

@Input
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ class DistributionExtensionTest extends Specification {

excludeFromVar 'a', 'b'
excludeFromVar 'c', 'd'

env 'a': 'b'
env 'c': 'd'
}

then:
ext.args == ['a', 'b', 'c', 'd']
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'() {
Expand All @@ -61,13 +65,16 @@ class DistributionExtensionTest extends Specification {
setDefaultJvmOpts(['c', 'd'])
setExcludeFromVar(['a', 'b'])
setExcludeFromVar(['c', 'd'])
setEnv(['a': 'b', 'c': 'd'])
setEnv(['foo': 'bar'])
}

then:
ext.args == ['c', 'd']
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'() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}"

Expand All @@ -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
Expand Down

0 comments on commit 4759b52

Please sign in to comment.