From 07001922f750586cbbeeaa7e24ebf7bf2c81712c Mon Sep 17 00:00:00 2001 From: Serban Iordache Date: Fri, 17 Apr 2020 16:26:07 +0200 Subject: [PATCH] fix issue #131: Compatibility with Gradle 6.4 --- .../groovy/org/beryx/jlink/BaseTask.groovy | 50 +++++++++++++++++++ .../org/beryx/jlink/JPackageImageTask.groovy | 12 ----- .../org/beryx/jlink/JPackageTask.groovy | 12 ----- .../groovy/org/beryx/jlink/JlinkTask.groovy | 13 ----- .../SuggestedMergedModuleInfoBuilder.groovy | 2 +- .../jlink/util/SuggestedModulesBuilder.groovy | 3 +- .../org/beryx/jlink/JlinkPluginSpec.groovy | 28 +++++++++-- .../hello-logback/build.modular.gradle | 42 ++++++++++++++++ 8 files changed, 120 insertions(+), 42 deletions(-) create mode 100644 src/test/resources/hello-logback/build.modular.gradle diff --git a/src/main/groovy/org/beryx/jlink/BaseTask.groovy b/src/main/groovy/org/beryx/jlink/BaseTask.groovy index 7babb209..80abade6 100644 --- a/src/main/groovy/org/beryx/jlink/BaseTask.groovy +++ b/src/main/groovy/org/beryx/jlink/BaseTask.groovy @@ -15,14 +15,19 @@ */ package org.beryx.jlink +import groovy.transform.CompileDynamic import groovy.transform.CompileStatic import org.beryx.jlink.data.JlinkPluginExtension import org.gradle.api.DefaultTask +import org.gradle.api.logging.Logger +import org.gradle.api.logging.Logging import org.gradle.api.tasks.Input import org.gradle.api.tasks.Internal @CompileStatic class BaseTask extends DefaultTask { + private static final Logger LOGGER = Logging.getLogger(BaseTask.class); + @Internal final JlinkPluginExtension extension @@ -35,4 +40,49 @@ class BaseTask extends DefaultTask { String getJlinkBasePath() { extension.jlinkBasePath.get() } + @Internal + String getDefaultMainClass() { + def mainClass = defaultMainClassModern + if(!mainClass) return defaultMainClassLegacy + def mainModule = defaultModuleModern + def moduleName = extension.moduleName.get() + if(mainModule != moduleName) { + LOGGER.warn("The module name specified in 'application.mainModule' ($mainModule) has not the expected value ($moduleName).") + } + mainClass + } + + @CompileDynamic + @Internal + String getDefaultMainClassModern() { + try { + return project.application?.mainClass?.get() as String + } catch (Exception e) { + return null + } + } + + @CompileDynamic + @Internal + String getDefaultModuleModern() { + try { + return project.application?.mainModule?.get() as String + } catch (Exception e) { + return null + } + } + + @Internal + String getDefaultMainClassLegacy() { + def mainClass = project['mainClassName'] as String + int pos = mainClass.lastIndexOf('/') + if(pos < 0) return mainClass + def mainClassModule = mainClass.substring(0, pos) + def moduleName = extension.moduleName.get() + if(mainClassModule != moduleName) { + LOGGER.warn("The module name specified in 'mainClassName' ($mainClassModule) has not the expected value ($moduleName).") + } + mainClass.substring(pos + 1) + } } + diff --git a/src/main/groovy/org/beryx/jlink/JPackageImageTask.groovy b/src/main/groovy/org/beryx/jlink/JPackageImageTask.groovy index 1a43f079..e905aae0 100644 --- a/src/main/groovy/org/beryx/jlink/JPackageImageTask.groovy +++ b/src/main/groovy/org/beryx/jlink/JPackageImageTask.groovy @@ -85,16 +85,4 @@ class JPackageImageTask extends BaseTask { File getImageDirFromName() { project.file("$project.buildDir/${imageName}") } - - @Internal - String getDefaultMainClass() { - def mainClass = project['mainClassName'] as String - int pos = mainClass.lastIndexOf('/') - if(pos < 0) return mainClass - def mainClassModule = mainClass.substring(0, pos) - if(mainClassModule != moduleName) { - LOGGER.warn("The module name specified in 'mainClassName' ($mainClassModule) has not the expected value (${moduleName}).") - } - mainClass.substring(pos + 1) - } } diff --git a/src/main/groovy/org/beryx/jlink/JPackageTask.groovy b/src/main/groovy/org/beryx/jlink/JPackageTask.groovy index ce7b1c17..391ff9e9 100644 --- a/src/main/groovy/org/beryx/jlink/JPackageTask.groovy +++ b/src/main/groovy/org/beryx/jlink/JPackageTask.groovy @@ -85,16 +85,4 @@ class JPackageTask extends BaseTask { File getImageDirFromName() { project.file("$project.buildDir/${imageName}") } - - @Internal - String getDefaultMainClass() { - def mainClass = project['mainClassName'] as String - int pos = mainClass.lastIndexOf('/') - if(pos < 0) return mainClass - def mainClassModule = mainClass.substring(0, pos) - if(mainClassModule != moduleName) { - LOGGER.warn("The module name specified in 'mainClassName' ($mainClassModule) has not the expected value (${moduleName}).") - } - mainClass.substring(pos + 1) - } } diff --git a/src/main/groovy/org/beryx/jlink/JlinkTask.groovy b/src/main/groovy/org/beryx/jlink/JlinkTask.groovy index 0dd8d1d0..04aa4ac5 100644 --- a/src/main/groovy/org/beryx/jlink/JlinkTask.groovy +++ b/src/main/groovy/org/beryx/jlink/JlinkTask.groovy @@ -19,7 +19,6 @@ import groovy.transform.CompileStatic import org.beryx.jlink.data.* import org.beryx.jlink.impl.JlinkTaskImpl import org.beryx.jlink.util.PathUtil -import org.gradle.api.artifacts.Configuration import org.gradle.api.file.Directory import org.gradle.api.logging.Logger import org.gradle.api.logging.Logging @@ -129,16 +128,4 @@ class JlinkTask extends BaseTask { File getImageDirFromName() { project.file("$project.buildDir/$imageName") } - - @Internal - String getDefaultMainClass() { - def mainClass = project['mainClassName'] as String - int pos = mainClass.lastIndexOf('/') - if(pos < 0) return mainClass - def mainClassModule = mainClass.substring(0, pos) - if(mainClassModule != moduleName) { - LOGGER.warn("The module name specified in 'mainClassName' ($mainClassModule) has not the expected value ($moduleName).") - } - mainClass.substring(pos + 1) - } } diff --git a/src/main/groovy/org/beryx/jlink/util/SuggestedMergedModuleInfoBuilder.groovy b/src/main/groovy/org/beryx/jlink/util/SuggestedMergedModuleInfoBuilder.groovy index b866d026..c4807935 100644 --- a/src/main/groovy/org/beryx/jlink/util/SuggestedMergedModuleInfoBuilder.groovy +++ b/src/main/groovy/org/beryx/jlink/util/SuggestedMergedModuleInfoBuilder.groovy @@ -116,7 +116,7 @@ class SuggestedMergedModuleInfoBuilder { scanner.externalPackages.each { pkg -> def moduleName = moduleManager.exportMap[pkg] if(!moduleName) { - LOGGER.info("Cannot find module exporting $pkg") + LOGGER.info("Cannot find module exporting $pkg (used by the merged module)") } else if(moduleName != 'java.base'){ builders << new RequiresBuilder(moduleName) } diff --git a/src/main/groovy/org/beryx/jlink/util/SuggestedModulesBuilder.groovy b/src/main/groovy/org/beryx/jlink/util/SuggestedModulesBuilder.groovy index b37a2360..851fc724 100644 --- a/src/main/groovy/org/beryx/jlink/util/SuggestedModulesBuilder.groovy +++ b/src/main/groovy/org/beryx/jlink/util/SuggestedModulesBuilder.groovy @@ -36,6 +36,7 @@ class SuggestedModulesBuilder { Set getProjectModules() { Set modules = [] + LOGGER.info("Retrieving project modules for configuration $configuration.name") for(ResolvedDependency dep: configuration.resolvedConfiguration.firstLevelModuleDependencies) { def f = Util.getArtifact(dep) modules.addAll(getModulesRequiredBy(f)) @@ -60,7 +61,7 @@ class SuggestedModulesBuilder { scanner.externalPackages.each { pkg -> def moduleName = moduleManager.exportMap[pkg] if(!moduleName) { - LOGGER.info("Cannot find module exporting $pkg") + LOGGER.info("Cannot find module exporting $pkg (required by $jarOrDir.name)") } else if(moduleName != 'java.base'){ modules << moduleName } diff --git a/src/test/groovy/org/beryx/jlink/JlinkPluginSpec.groovy b/src/test/groovy/org/beryx/jlink/JlinkPluginSpec.groovy index 9dd76a8b..de5dd00c 100644 --- a/src/test/groovy/org/beryx/jlink/JlinkPluginSpec.groovy +++ b/src/test/groovy/org/beryx/jlink/JlinkPluginSpec.groovy @@ -31,11 +31,18 @@ class JlinkPluginSpec extends Specification { println "CLEANUP" } - def setUpBuild(String projectDir) { + def setUpBuild(String projectDir, String buildScriptName = 'build.gradle') { new AntBuilder().copy(todir: testProjectDir.root) { - fileset(dir: "src/test/resources/$projectDir") + def options = [dir: "src/test/resources/$projectDir"] + if(buildScriptName != 'build.gradle') { + options.excludes = 'build.gradle' + } + fileset(options) } - new File(testProjectDir.root, "build.gradle") + if(buildScriptName != 'build.gradle') { + new File(testProjectDir.root, buildScriptName).renameTo("$testProjectDir.root/build.gradle") + } + new File(testProjectDir.root, 'build.gradle') } def setUpHelloLogbackBuild(String moduleName, String launcherName, String mainClass, String mergedModuleName) { @@ -58,6 +65,21 @@ class JlinkPluginSpec extends Specification { println "Executing build script:\n${buildFile.text}" } + def "should be compatible with the new JPMS features introduced in Gradle 6.4"() { + when: + setUpBuild('hello-logback', 'build.modular.gradle') + BuildResult result = GradleRunner.create() + .withDebug(true) + .withProjectDir(testProjectDir.root) + .withPluginClasspath() + .withGradleVersion('6.4-rc-1') + .withArguments(JlinkPlugin.TASK_NAME_JLINK, "-is") + .build(); + + then: + checkOutput(result, 'modular-hello', 'LOG: Hello, modular Java!') + } + @Unroll def "should execute task with Gradle #gradleVersion, moduleName=#moduleName, launcherName=#launcherName, mainClass=#mainClass and mergedModuleName=#mergedModuleName"() { when: diff --git a/src/test/resources/hello-logback/build.modular.gradle b/src/test/resources/hello-logback/build.modular.gradle new file mode 100644 index 00000000..2dbe1282 --- /dev/null +++ b/src/test/resources/hello-logback/build.modular.gradle @@ -0,0 +1,42 @@ +plugins { + id 'org.beryx.jlink' +} + +repositories { + mavenCentral() +} + +sourceCompatibility = 10 +targetCompatibility = 10 + +dependencies { + compile 'org.slf4j:slf4j-api:1.7.25' + compile 'ch.qos.logback:logback-classic:1.2.3' + compile 'bouncycastle:bcprov-jdk16:140' + compile 'javax.xml.bind:jaxb-api:2.3.0' +} + +application { + mainModule.set("modular.example.hello") + mainClass.set("org.example.modular.Hello") +} + +jar { + manifest { + attributes 'Implementation-Title': "modular-hello", + 'Main-Class': application.mainClass + } +} + +compileJava { + options.compilerArgs = ['--module-path', classpath.asPath] + classpath = files() +} + +jlink { + options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages'] + mergedModule { + requires 'java.naming'; + requires 'java.xml'; + } +}