From 93b2a17fb8cf178695a98f4cf1a67cfd0426e13b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 21 Jan 2015 15:02:10 +0000 Subject: [PATCH] Make repackage depend on jar tasks from runtime project dependencies By default, when building a project's jar its runtime dependencies are not taken into account as they are not needed to successfully compile the code that will be packaged in the jar. A side-effect of this was that, if a project that was being repackaged had a runtime dependency on another project, then the repackaged jar would not include the jar of the project on which it has the runtime dependency as the jar had not been built. This commit updates Boot's repackage task to have a dependency on the jar task of any project dependencies in the runtime configuration thereby ensuring that those dependencies' jars will have been built before the repackaging occurs. Fixes gh-2344 --- .../gradle/MultiProjectRepackagingTests.java | 13 +++++++++++ .../build.gradle | 22 +++++++++++++++++++ .../settings.gradle | 1 + .../repackage/RepackagePluginFeatures.java | 19 +++++++++++----- 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 spring-boot-integration-tests/src/test/resources/multi-project-runtime-project-dependency/build.gradle create mode 100644 spring-boot-integration-tests/src/test/resources/multi-project-runtime-project-dependency/settings.gradle diff --git a/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/MultiProjectRepackagingTests.java b/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/MultiProjectRepackagingTests.java index 4d59a522f0..2666bc510a 100644 --- a/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/MultiProjectRepackagingTests.java +++ b/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/MultiProjectRepackagingTests.java @@ -63,4 +63,17 @@ public class MultiProjectRepackagingTests { assertThat(jarFile.getEntry("lib/foo.jar"), notNullValue()); jarFile.close(); } + + @Test + public void repackageWithRuntimeProjectDependency() throws Exception { + ProjectConnection project = new ProjectCreator() + .createProject("multi-project-runtime-project-dependency"); + project.newBuild().forTasks("clean", "build") + .withArguments("-PbootVersion=" + BOOT_VERSION).run(); + File buildLibs = new File( + "target/multi-project-runtime-project-dependency/projectA/build/libs"); + JarFile jarFile = new JarFile(new File(buildLibs, "projectA.jar")); + assertThat(jarFile.getEntry("lib/projectB.jar"), notNullValue()); + jarFile.close(); + } } diff --git a/spring-boot-integration-tests/src/test/resources/multi-project-runtime-project-dependency/build.gradle b/spring-boot-integration-tests/src/test/resources/multi-project-runtime-project-dependency/build.gradle new file mode 100644 index 0000000000..53e3dd2755 --- /dev/null +++ b/spring-boot-integration-tests/src/test/resources/multi-project-runtime-project-dependency/build.gradle @@ -0,0 +1,22 @@ +buildscript { + repositories { + mavenLocal() + } + dependencies { + classpath "org.springframework.boot:spring-boot-gradle-plugin:${project.bootVersion}" + } +} + +project(':projectA') { + apply plugin: 'spring-boot' + dependencies { + runtime project(':projectB') + } + bootRepackage { + mainClass 'com.foo.Bar' + } +} + +project(':projectB') { + apply plugin: 'java' +} \ No newline at end of file diff --git a/spring-boot-integration-tests/src/test/resources/multi-project-runtime-project-dependency/settings.gradle b/spring-boot-integration-tests/src/test/resources/multi-project-runtime-project-dependency/settings.gradle new file mode 100644 index 0000000000..ee6b6cd92c --- /dev/null +++ b/spring-boot-integration-tests/src/test/resources/multi-project-runtime-project-dependency/settings.gradle @@ -0,0 +1 @@ +include 'projectA', 'projectB' \ No newline at end of file diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackagePluginFeatures.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackagePluginFeatures.java index 1f5cfe71b2..c4679f64d0 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackagePluginFeatures.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackagePluginFeatures.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,9 +22,12 @@ import java.io.IOException; import org.gradle.api.Action; import org.gradle.api.Project; import org.gradle.api.Task; +import org.gradle.api.artifacts.Configuration; import org.gradle.api.artifacts.Dependency; import org.gradle.api.logging.Logger; import org.gradle.api.plugins.BasePlugin; +import org.gradle.api.plugins.JavaPlugin; +import org.gradle.api.tasks.TaskDependency; import org.gradle.api.tasks.bundling.Jar; import org.springframework.boot.gradle.PluginFeatures; import org.springframework.boot.gradle.SpringBootPluginExtension; @@ -37,6 +40,7 @@ import org.springframework.util.StringUtils; * * @author Phillip Webb * @author Dave Syer + * @author Andy Wilkinson */ public class RepackagePluginFeatures implements PluginFeatures { @@ -55,9 +59,14 @@ public class RepackagePluginFeatures implements PluginFeatures { + "archives so that they can be executed from the command " + "line using 'java -jar'"); task.setGroup(BasePlugin.BUILD_GROUP); - task.dependsOn(project.getConfigurations() - .getByName(Dependency.ARCHIVES_CONFIGURATION).getAllArtifacts() - .getBuildDependencies()); + Configuration runtimeConfiguration = project.getConfigurations().getByName( + JavaPlugin.RUNTIME_CONFIGURATION_NAME); + TaskDependency runtimeProjectDependencyJarTasks = runtimeConfiguration + .getTaskDependencyFromProjectDependency(true, JavaPlugin.JAR_TASK_NAME); + task.dependsOn( + project.getConfigurations().getByName(Dependency.ARCHIVES_CONFIGURATION) + .getAllArtifacts().getBuildDependencies(), + runtimeProjectDependencyJarTasks); registerOutput(project, task); ensureTaskRunsOnAssembly(project, task); } @@ -121,7 +130,7 @@ public class RepackagePluginFeatures implements PluginFeatures { private void setupInputOutputs(Jar jarTask, String classifier) { Logger logger = this.project.getLogger(); logger.debug("Using classifier: " + classifier + " for task " - + task.getName()); + + this.task.getName()); File inputFile = jarTask.getArchivePath(); String outputName = inputFile.getName(); outputName = StringUtils.stripFilenameExtension(outputName) + "-"