Skip to content

Commit

Permalink
issue #197: application.applicationDefaultJvmArgs not taking effect i…
Browse files Browse the repository at this point in the history
…n launcher
  • Loading branch information
siordache committed Sep 16, 2021
1 parent 4cb7a8c commit b516a97
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/main/groovy/org/beryx/jlink/data/JPackageData.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ class JPackageData {

@Input
List<String> getJvmArgs() {
this.@jvmArgs ?: launcherData.jvmArgs
this.@jvmArgs ?: launcherData.getJvmArgs(project)
}

@Input
List<String> getArgs() {
this.@args ?: launcherData.args
this.@args ?: launcherData.getArgs(project)
}

@OutputDirectory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ class JlinkPluginExtension {
mergedModuleJarVersion.set(project.provider{project.version as String})

launcherData = project.objects.property(LauncherData)
def ld = new LauncherData()
ld.name = project.name
ld.args = Util.getDefaultArgs(project)
ld.jvmArgs = Util.getDefaultJvmArgs(project)
def ld = new LauncherData(project.name)
launcherData.set(ld)

customImageData = project.objects.property(CustomImageData)
Expand Down Expand Up @@ -178,7 +175,7 @@ class JlinkPluginExtension {
}

void secondaryLauncher(Action<LauncherData> action) {
def ld = new SecondaryLauncherData()
def ld = new SecondaryLauncherData(null)
ld.moduleName = moduleName.get()
Util.addToListProperty(secondaryLaunchers, ld)
action.execute(ld)
Expand Down
27 changes: 25 additions & 2 deletions src/main/groovy/org/beryx/jlink/data/LauncherData.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,37 @@ package org.beryx.jlink.data

import groovy.transform.CompileStatic
import groovy.transform.ToString
import org.beryx.jlink.util.Util
import org.gradle.api.Project

@CompileStatic
@ToString(includeNames = true)
class LauncherData implements Serializable {
private static final List<String> UNDEFINED_ARGS = ['<UNDEFINED>']

private List<String> configuredArgs = UNDEFINED_ARGS
private List<String> configuredJvmArgs = UNDEFINED_ARGS

String name
List<String> jvmArgs = []
List<String> args = []
File unixScriptTemplate
File windowsScriptTemplate
boolean noConsole

LauncherData(String name) {
this.name = name
}

List<String> getArgs(Project project) {
(configuredArgs != UNDEFINED_ARGS) ? configuredArgs : Util.getDefaultArgs(project)
}
void setArgs(List<String> args) {
this.configuredArgs = args
}

List<String> getJvmArgs(Project project) {
(configuredJvmArgs != UNDEFINED_ARGS) ? configuredJvmArgs : Util.getDefaultJvmArgs(project)
}
void setJvmArgs(List<String> jvmArgs) {
this.configuredJvmArgs = jvmArgs
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class SecondaryLauncherData extends LauncherData {
String icon
Boolean winConsole

SecondaryLauncherData(String name) {
super(name)
}

@CompileDynamic
void check() {
["name", "moduleName", "mainClass"].each {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ class JPackageImageTaskImpl extends BaseTaskImpl<JPackageTaskData> {
if(launcher.mainClass) {
stream << "main-class=$launcher.mainClass\n"
}
if(launcher.args) {
stream << "arguments=${launcher.args.collect{adjustArg(it)}.join('\\n')}\n"

def args = launcher.getArgs(project)
if(args) {
stream << "arguments=${args.collect{adjustArg(it)}.join('\\n')}\n"
}
if(launcher.jvmArgs) {
stream << "java-options=${launcher.jvmArgs.collect{adjustArg(it)}.join('\\n')}\n"

def jvmArgs = launcher.getJvmArgs(project)
if(jvmArgs) {
stream << "java-options=${jvmArgs.collect{adjustArg(it)}.join('\\n')}\n"
}
if(launcher.appVersion) {
stream << "app-version=$launcher.appVersion\n"
Expand Down
4 changes: 2 additions & 2 deletions src/main/groovy/org/beryx/jlink/impl/JlinkTaskImpl.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ class JlinkTaskImpl extends BaseTaskImpl<JlinkTaskData> {
}

void createLaunchScripts(File imageDir) {
def generator = new LaunchScriptGenerator(td.moduleName, td.mainClass, td.launcherData)
def generator = new LaunchScriptGenerator(project, td.moduleName, td.mainClass, td.launcherData)
generator.generate("$imageDir/bin")
td.secondaryLaunchers.each { launcher ->
def secondaryGenerator = new LaunchScriptGenerator(launcher.moduleName, launcher.mainClass, launcher)
def secondaryGenerator = new LaunchScriptGenerator(project, launcher.moduleName, launcher.mainClass, launcher)
secondaryGenerator.generate("$imageDir/bin")
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/main/groovy/org/beryx/jlink/util/LaunchScriptGenerator.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ package org.beryx.jlink.util

import groovy.text.SimpleTemplateEngine
import groovy.text.Template
import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import groovy.transform.TupleConstructor
import org.beryx.jlink.data.LauncherData
import org.gradle.api.GradleException
import org.gradle.api.Project

import java.util.function.Function
import java.util.stream.Collectors
import java.util.stream.Stream

@CompileStatic
@TupleConstructor
class LaunchScriptGenerator {
final Project project
final String moduleName
final String mainClassName
final LauncherData launcherData
Expand Down Expand Up @@ -82,11 +85,17 @@ class LaunchScriptGenerator {
}
}

@CompileDynamic
String getScript(Type type) {
def engine = new SimpleTemplateEngine()
def args = launcherData.args.collect{adjustArg(it, type)}.join(' ')
def jvmArgs = launcherData.jvmArgs.collect{adjustArg(it, type)}.join(' ')

def args = launcherData.getArgs(project).stream()
.map{adjustArg(it, type) as CharSequence}
.collect(Collectors.joining(' '))

def jvmArgs = launcherData.getJvmArgs(project).stream()
.map{adjustArg(it, type) as CharSequence}
.collect(Collectors.joining(' '))

def bindings = [
moduleName: moduleName,
mainClassName: mainClassName,
Expand Down
10 changes: 4 additions & 6 deletions src/test/groovy/org/beryx/jlink/LaunchScriptGeneratorSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ class LaunchScriptGeneratorSpec extends Specification {
@Unroll
def "should generate the correct unix script with jvmArgs=#jvmArgs and args=#args"() {
when:
LauncherData ld = new LauncherData()
ld.name = 'hello'
LauncherData ld = new LauncherData('hello')
ld.args = args
ld.jvmArgs = jvmArgs
def generator = new LaunchScriptGenerator('org.example.hello', 'org.example.Hello', ld)
def generator = new LaunchScriptGenerator(null, 'org.example.hello', 'org.example.Hello', ld)
def scriptLines = generator.getScript(LaunchScriptGenerator.Type.UNIX).readLines()

then:
Expand All @@ -47,11 +46,10 @@ class LaunchScriptGeneratorSpec extends Specification {
@Unroll
def "should generate the correct windows script with jvmArgs=#jvmArgs and args=#args"() {
when:
LauncherData ld = new LauncherData()
ld.name = 'hello'
LauncherData ld = new LauncherData('hello')
ld.args = args
ld.jvmArgs = jvmArgs
def generator = new LaunchScriptGenerator('org.example.hello', 'org.example.Hello', ld)
def generator = new LaunchScriptGenerator(null, 'org.example.hello', 'org.example.Hello', ld)
def scriptLines = generator.getScript(LaunchScriptGenerator.Type.WINDOWS).readLines()

then:
Expand Down

0 comments on commit b516a97

Please sign in to comment.