Skip to content

Commit 93b2a17

Browse files
committed
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
1 parent bbd2486 commit 93b2a17

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/MultiProjectRepackagingTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,17 @@ public void repackageWithCommonFileDependency() throws Exception {
6363
assertThat(jarFile.getEntry("lib/foo.jar"), notNullValue());
6464
jarFile.close();
6565
}
66+
67+
@Test
68+
public void repackageWithRuntimeProjectDependency() throws Exception {
69+
ProjectConnection project = new ProjectCreator()
70+
.createProject("multi-project-runtime-project-dependency");
71+
project.newBuild().forTasks("clean", "build")
72+
.withArguments("-PbootVersion=" + BOOT_VERSION).run();
73+
File buildLibs = new File(
74+
"target/multi-project-runtime-project-dependency/projectA/build/libs");
75+
JarFile jarFile = new JarFile(new File(buildLibs, "projectA.jar"));
76+
assertThat(jarFile.getEntry("lib/projectB.jar"), notNullValue());
77+
jarFile.close();
78+
}
6679
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
buildscript {
2+
repositories {
3+
mavenLocal()
4+
}
5+
dependencies {
6+
classpath "org.springframework.boot:spring-boot-gradle-plugin:${project.bootVersion}"
7+
}
8+
}
9+
10+
project(':projectA') {
11+
apply plugin: 'spring-boot'
12+
dependencies {
13+
runtime project(':projectB')
14+
}
15+
bootRepackage {
16+
mainClass 'com.foo.Bar'
17+
}
18+
}
19+
20+
project(':projectB') {
21+
apply plugin: 'java'
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include 'projectA', 'projectB'

spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackagePluginFeatures.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,9 +22,12 @@
2222
import org.gradle.api.Action;
2323
import org.gradle.api.Project;
2424
import org.gradle.api.Task;
25+
import org.gradle.api.artifacts.Configuration;
2526
import org.gradle.api.artifacts.Dependency;
2627
import org.gradle.api.logging.Logger;
2728
import org.gradle.api.plugins.BasePlugin;
29+
import org.gradle.api.plugins.JavaPlugin;
30+
import org.gradle.api.tasks.TaskDependency;
2831
import org.gradle.api.tasks.bundling.Jar;
2932
import org.springframework.boot.gradle.PluginFeatures;
3033
import org.springframework.boot.gradle.SpringBootPluginExtension;
@@ -37,6 +40,7 @@
3740
*
3841
* @author Phillip Webb
3942
* @author Dave Syer
43+
* @author Andy Wilkinson
4044
*/
4145
public class RepackagePluginFeatures implements PluginFeatures {
4246

@@ -55,9 +59,14 @@ private void addRepackageTask(Project project) {
5559
+ "archives so that they can be executed from the command "
5660
+ "line using 'java -jar'");
5761
task.setGroup(BasePlugin.BUILD_GROUP);
58-
task.dependsOn(project.getConfigurations()
59-
.getByName(Dependency.ARCHIVES_CONFIGURATION).getAllArtifacts()
60-
.getBuildDependencies());
62+
Configuration runtimeConfiguration = project.getConfigurations().getByName(
63+
JavaPlugin.RUNTIME_CONFIGURATION_NAME);
64+
TaskDependency runtimeProjectDependencyJarTasks = runtimeConfiguration
65+
.getTaskDependencyFromProjectDependency(true, JavaPlugin.JAR_TASK_NAME);
66+
task.dependsOn(
67+
project.getConfigurations().getByName(Dependency.ARCHIVES_CONFIGURATION)
68+
.getAllArtifacts().getBuildDependencies(),
69+
runtimeProjectDependencyJarTasks);
6170
registerOutput(project, task);
6271
ensureTaskRunsOnAssembly(project, task);
6372
}
@@ -121,7 +130,7 @@ public void execute(Jar jarTask) {
121130
private void setupInputOutputs(Jar jarTask, String classifier) {
122131
Logger logger = this.project.getLogger();
123132
logger.debug("Using classifier: " + classifier + " for task "
124-
+ task.getName());
133+
+ this.task.getName());
125134
File inputFile = jarTask.getArchivePath();
126135
String outputName = inputFile.getName();
127136
outputName = StringUtils.stripFilenameExtension(outputName) + "-"

0 commit comments

Comments
 (0)