Skip to content

Commit

Permalink
allow using the {{BIN_DIR}} placeholder in jvmArgs and args
Browse files Browse the repository at this point in the history
  • Loading branch information
siordache committed Jan 29, 2020
1 parent 51dd46f commit 5d7ff0b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 40 deletions.
15 changes: 5 additions & 10 deletions doc/user_guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ The following keys are allowed in the _constraints_ map: +


By calling one of the above methods you automatically enable the additive mode.
This means that is no longer necessary to explicitly set the `additive` property to `true`.
This means that it's no longer necessary to explicitly set the `additive` property to `true`.


_Usage example_
Expand Down Expand Up @@ -279,6 +279,8 @@ to parse the templates, with the following variables available:
- jvmArgs
- args

The values in the *args* and *jvmArgs* lists may contain the placeholder `{{BIN_DIR}}`, which stands for the _bin_ directory of your custom runtime image.

_Usage example_
[source,groovy,indent=0,subs="verbatim,attributes",role="primary"]
.Groovy
Expand All @@ -287,7 +289,7 @@ jlink {
...
launcher {
name = 'my-app'
jvmArgs = ['-Dlog4j.debug=true', '-Dlog4j.configurationFile=./log4j2.xml']
jvmArgs = ['-Dlog4j.debug=true', '-Dlog4j.configurationFile={{BIN_DIR}}/log4j2.xml']
args = ['--user', 'alice']
unixScriptTemplate = file('unixStartScript.txt')
windowsScriptTemplate = file('windowsStartScript.txt')
Expand All @@ -303,7 +305,7 @@ jlink {
...
launcher {
name = "my-app"
jvmArgs = listOf("-Dlog4j.debug=true", "-Dlog4j.configurationFile=./log4j2.xml")
jvmArgs = listOf("-Dlog4j.debug=true", "-Dlog4j.configurationFile={{BIN_DIR}}/log4j2.xml")
args = listOf("--user", "alice")
unixScriptTemplate = file("unixStartScript.txt")
windowsScriptTemplate = file("windowsStartScript.txt")
Expand Down Expand Up @@ -369,13 +371,6 @@ jdkModules:: list of JDK modules to be included in the generated image. +
jdkAdditive:: if true, the custom image will contain both the modules in the `jdkModules` list and the JDK modules identified as required by the plugin itself.
_defaultValue_: false

In many cases the "suggested" descriptor is just the right one for your merged module, so you don't need to provide a `mergedModule` block.
In some other cases the "suggested" descriptor is _almost_ right, in the sense that it only misses one or a few clauses.
In these cases you are allowed to configure only the missing clauses in the `mergedModule` block and instruct the plugin
to add them to the suggested descriptor by setting the attribute `additive` to true.
(The default value of `additive` is false.)


appModules:: list of application modules to be included in the generated image. +
Modules required by those in this list will be automatically included. +
_defaultValue_: null (all application modules are included)
Expand Down
18 changes: 11 additions & 7 deletions src/main/groovy/org/beryx/jlink/util/LaunchScriptGenerator.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,24 @@ class LaunchScriptGenerator {
UNIX(
'',
{LauncherData ld -> ld.unixScriptTemplate},
'unixScriptTemplate.txt'),
'unixScriptTemplate.txt',
'$DIR'),
WINDOWS(
'.bat',
{LauncherData ld -> ld.windowsScriptTemplate},
'windowsScriptTemplate.txt')
'windowsScriptTemplate.txt',
'%~dp0')

final String extension
final Function<LauncherData, File> templateProvider
final String defaultTemplate
final String binDirPlaceholder

Type(String extension, Function<LauncherData, File> templateProvider, String defaultTemplate) {
Type(String extension, Function<LauncherData, File> templateProvider, String defaultTemplate, String binDirPlaceholder) {
this.extension = extension
this.templateProvider = templateProvider
this.defaultTemplate = defaultTemplate
this.binDirPlaceholder = binDirPlaceholder
}
}

Expand All @@ -65,8 +69,8 @@ class LaunchScriptGenerator {
@CompileDynamic
String getScript(Type type) {
def engine = new SimpleTemplateEngine()
def args = launcherData.args.collect{adjustArg(it)}.join(' ')
def jvmArgs = launcherData.jvmArgs.collect{adjustArg(it)}.join(' ')
def args = launcherData.args.collect{adjustArg(it, type)}.join(' ')
def jvmArgs = launcherData.jvmArgs.collect{adjustArg(it, type)}.join(' ')
def bindings = [
moduleName: moduleName,
mainClassName: mainClassName,
Expand All @@ -90,11 +94,11 @@ class LaunchScriptGenerator {
template.make(bindings).toString()
}

static String adjustArg(String arg) {
static String adjustArg(String arg, Type type) {
def adjusted = arg.replace('"', '\\"')
if(!(adjusted ==~ /[\w\-\+=\/\\,;.:#]+/)) {
adjusted = '"' + adjusted + '"'
}
adjusted
adjusted.replace('{{BIN_DIR}}', type.binDirPlaceholder)
}
}
18 changes: 10 additions & 8 deletions src/test/groovy/org/beryx/jlink/LaunchScriptGeneratorSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ class LaunchScriptGeneratorSpec extends Specification {
scriptLines[2].replace(' ', ' ') == lastLine

where:
jvmArgs | args | lastLine
[] | [] | '"$DIR/java" -p "$DIR/../app" -m org.example.hello/org.example.Hello "$@"'
['-Xmx200m'] | ['Alice'] | '"$DIR/java" -Xmx200m -p "$DIR/../app" -m org.example.hello/org.example.Hello Alice "$@"'
['-Xmx200m', '-Ddebug=true'] | ['Alice', 'Bob'] | '"$DIR/java" -Xmx200m -Ddebug=true -p "$DIR/../app" -m org.example.hello/org.example.Hello Alice Bob "$@"'
jvmArgs | args | lastLine
[] | [] | '"$DIR/java" -p "$DIR/../app" -m org.example.hello/org.example.Hello "$@"'
['-Xmx200m'] | ['Alice'] | '"$DIR/java" -Xmx200m -p "$DIR/../app" -m org.example.hello/org.example.Hello Alice "$@"'
['-Xmx200m', '-Ddebug=true'] | ['Alice', 'Bob'] | '"$DIR/java" -Xmx200m -Ddebug=true -p "$DIR/../app" -m org.example.hello/org.example.Hello Alice Bob "$@"'
['-cp', '{{BIN_DIR}}/data'] | ['{{BIN_DIR}}/../names.txt'] | '"$DIR/java" -cp "$DIR/data" -p "$DIR/../app" -m org.example.hello/org.example.Hello "$DIR/../names.txt" "$@"'
}

@Unroll
Expand All @@ -60,9 +61,10 @@ class LaunchScriptGeneratorSpec extends Specification {
scriptLines[3].replace(' ', ' ') == lastLine

where:
jvmArgs | args | lastLine
[] | [] | 'pushd %DIR% & %JAVA_EXEC% -p "%~dp0/../app" -m org.example.hello/org.example.Hello %* & popd'
['-Xmx200m'] | ['Alice'] | 'pushd %DIR% & %JAVA_EXEC% -Xmx200m -p "%~dp0/../app" -m org.example.hello/org.example.Hello Alice %* & popd'
['-Xmx200m', '-Ddebug=true'] | ['Alice', 'Bob'] | 'pushd %DIR% & %JAVA_EXEC% -Xmx200m -Ddebug=true -p "%~dp0/../app" -m org.example.hello/org.example.Hello Alice Bob %* & popd'
jvmArgs | args | lastLine
[] | [] | 'pushd %DIR% & %JAVA_EXEC% -p "%~dp0/../app" -m org.example.hello/org.example.Hello %* & popd'
['-Xmx200m'] | ['Alice'] | 'pushd %DIR% & %JAVA_EXEC% -Xmx200m -p "%~dp0/../app" -m org.example.hello/org.example.Hello Alice %* & popd'
['-Xmx200m', '-Ddebug=true'] | ['Alice', 'Bob'] | 'pushd %DIR% & %JAVA_EXEC% -Xmx200m -Ddebug=true -p "%~dp0/../app" -m org.example.hello/org.example.Hello Alice Bob %* & popd'
['-cp', '{{BIN_DIR}}/data'] | ['{{BIN_DIR}}/../names.txt'] | 'pushd %DIR% & %JAVA_EXEC% -cp "%~dp0/data" -p "%~dp0/../app" -m org.example.hello/org.example.Hello "%~dp0/../names.txt" %* & popd'
}
}
15 changes: 0 additions & 15 deletions src/test/resources/hello-log4j-2.9.0/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,4 @@ tasks.jlink.doLast {
from('src/main/resources')
into("$buildDir/image/bin")
}
adjustStartScripts()
}

def adjustStartScripts() {
['hello', 'hello.bat'].each { script ->
def scriptFile = new File("$buildDir/image/bin/$script")
if(scriptFile.file) {
def quotes = script.endsWith('.bat') ? '' : '"'
def binDir = script.endsWith('.bat') ? '%~dp0' : '.'
def newText = scriptFile.text
newText = newText.replace('JLINK_VM_OPTIONS=',
"JLINK_VM_OPTIONS=${quotes}-Dlog4j.configurationFile=$binDir/log4j2.xml${quotes}")
scriptFile.newWriter().withWriter {w -> w << newText}
}
}
}

0 comments on commit 5d7ff0b

Please sign in to comment.