Skip to content

Commit

Permalink
fix issue #131: Compatibility with Gradle 6.4
Browse files Browse the repository at this point in the history
  • Loading branch information
siordache committed Apr 17, 2020
1 parent 1d46ce7 commit 0700192
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 42 deletions.
50 changes: 50 additions & 0 deletions src/main/groovy/org/beryx/jlink/BaseTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
}
}

12 changes: 0 additions & 12 deletions src/main/groovy/org/beryx/jlink/JPackageImageTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
12 changes: 0 additions & 12 deletions src/main/groovy/org/beryx/jlink/JPackageTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
13 changes: 0 additions & 13 deletions src/main/groovy/org/beryx/jlink/JlinkTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class SuggestedModulesBuilder {

Set<String> getProjectModules() {
Set<String> 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))
Expand All @@ -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
}
Expand Down
28 changes: 25 additions & 3 deletions src/test/groovy/org/beryx/jlink/JlinkPluginSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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:
Expand Down
42 changes: 42 additions & 0 deletions src/test/resources/hello-logback/build.modular.gradle
Original file line number Diff line number Diff line change
@@ -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';
}
}

0 comments on commit 0700192

Please sign in to comment.