Skip to content

Commit

Permalink
ShadowApplicationPlugin: configure JavaJarExec to use javaLauncher in…
Browse files Browse the repository at this point in the history
… order to leverage Toolchains. Fixes #690
  • Loading branch information
rpalcolea authored and johnrengelman committed Aug 1, 2021
1 parent c071110 commit 629114a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import org.gradle.api.file.CopySpec
import org.gradle.api.plugins.ApplicationPlugin
import org.gradle.api.plugins.ApplicationPluginConvention
import org.gradle.api.plugins.JavaApplication
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Sync
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.application.CreateStartScripts
import org.gradle.jvm.toolchain.JavaLauncher
import org.gradle.jvm.toolchain.JavaToolchainService

class ShadowApplicationPlugin implements Plugin<Project> {

Expand Down Expand Up @@ -87,9 +91,17 @@ class ShadowApplicationPlugin implements Plugin<Project> {
run.conventionMapping.jarFile = {
project.file("${install.get().destinationDir.path}/lib/${jar.get().archivePath.name}")
}
configureJavaLauncher(run)
}
}

private void configureJavaLauncher(JavaJarExec run) {
def toolchain = project.getExtensions().getByType(JavaPluginExtension.class).toolchain
JavaToolchainService service = project.getExtensions().getByType(JavaToolchainService.class)
Provider<JavaLauncher> defaultLauncher = service.launcherFor(toolchain)
run.getJavaLauncher().set(defaultLauncher)
}

protected void addCreateScriptsTask(Project project) {
ApplicationPluginConvention pluginConvention =
(ApplicationPluginConvention) project.convention.plugins.application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,77 @@ class ApplicationSpec extends PluginSpecification {
jar?.close()
}

def 'integration with application plugin and java toolchains'() {
given:
repo.module('shadow', 'a', '1.0')
.insertFile('a.properties', 'a')
.insertFile('a2.properties', 'a2')
.publish()

file('src/main/java/myapp/Main.java') << """
package myapp;
public class Main {
public static void main(String[] args) {
System.out.println("TestApp: Hello World! (" + args[0] + ")");
}
}
""".stripIndent()

buildFile << """
apply plugin: 'application'
mainClassName = 'myapp.Main'
dependencies {
implementation 'shadow:a:1.0'
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(16)
}
}
runShadow {
args 'foo'
doFirst {
project.logger.lifecycle("Running application with JDK \${it.javaLauncher.get().metadata.languageVersion.asInt()}")
}
}
""".stripIndent()

settingsFile << "rootProject.name = 'myapp'"

when:
BuildResult result = run('runShadow', '--stacktrace')

then: 'tests that runShadow executed and exited'
assert result.output.contains('Running application with JDK 16')
assert result.output.contains('TestApp: Hello World! (foo)')

and: 'Check that the proper jar file was installed'
File installedJar = getFile('build/install/myapp-shadow/lib/myapp-1.0-all.jar')
assert installedJar.exists()

and: 'And that jar file as the correct files in it'
contains(installedJar, ['a.properties', 'a2.properties', 'myapp/Main.class'])

and: 'Check the manifest attributes in the jar file are correct'
JarFile jar = new JarFile(installedJar)
Attributes attributes = jar.manifest.mainAttributes
assert attributes.getValue('Main-Class') == 'myapp.Main'

then: 'Check that the start scripts is written out and has the correct Java invocation'
File startScript = getFile('build/install/myapp-shadow/bin/myapp')
assert startScript.exists()
assert startScript.text.contains("CLASSPATH=\$APP_HOME/lib/myapp-1.0-all.jar")
assert startScript.text.contains("-jar \"\\\"\$CLASSPATH\\\"\" \"\$APP_ARGS\"")
assert startScript.text.contains("exec \"\$JAVACMD\" \"\$@\"")

cleanup:
jar?.close()
}

@Issue('SHADOW-89')
def 'shadow application distributions should use shadow jar'() {
given:
Expand Down

0 comments on commit 629114a

Please sign in to comment.