Skip to content

Commit 4be688a

Browse files
committed
Fix Gradle repackaging so it is only performed on the desired jars
The logic that determined whether or not the repackaging action should be applied to a particular jar task was broken and caused problems when a custom RepackageTask was used in a project's build. This commit updates the logic so that repackaging will be applied: - To the default jar task if RepackageTask.withJarTask is null - To a jar task if it is equal to RepackageTask.withJarTask - To a jar task if its name is equal to RepackageTask.withJarTask Repackaging is not applied if: - RepackageTask.enabled is false Numerous integration tests have been added to verify the repackaging behaviour. Fixes #1204
1 parent 60e6022 commit 4be688a

File tree

3 files changed

+179
-16
lines changed

3 files changed

+179
-16
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.gradle;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
import org.gradle.tooling.ProjectConnection;
23+
import org.junit.BeforeClass;
24+
import org.junit.Test;
25+
import org.springframework.boot.dependency.tools.ManagedDependencies;
26+
27+
import static org.junit.Assert.assertFalse;
28+
import static org.junit.Assert.assertTrue;
29+
30+
public class RepackagingTests {
31+
32+
private static final String BOOT_VERSION = ManagedDependencies.get()
33+
.find("spring-boot").getVersion();
34+
35+
private static ProjectConnection project;
36+
37+
@BeforeClass
38+
public static void createProject() throws IOException {
39+
project = new ProjectCreator().createProject("repackage");
40+
}
41+
42+
@Test
43+
public void repackagingEnabled() {
44+
project.newBuild().forTasks("clean", "build")
45+
.withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=true").run();
46+
File buildLibs = new File("target/repackage/build/libs");
47+
assertTrue(new File(buildLibs, "repackage.jar").exists());
48+
assertTrue(new File(buildLibs, "repackage.jar.original").exists());
49+
assertFalse(new File(buildLibs, "repackage-sources.jar.original").exists());
50+
}
51+
52+
@Test
53+
public void repackagingDisabled() {
54+
project.newBuild().forTasks("clean", "build")
55+
.withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=false")
56+
.run();
57+
File buildLibs = new File("target/repackage/build/libs");
58+
assertTrue(new File(buildLibs, "repackage.jar").exists());
59+
assertFalse(new File(buildLibs, "repackage.jar.original").exists());
60+
assertFalse(new File(buildLibs, "repackage-sources.jar.original").exists());
61+
}
62+
63+
@Test
64+
public void repackagingDisabledWithCustomRepackagedJar() {
65+
project.newBuild().forTasks("clean", "build", "customRepackagedJar")
66+
.withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=false")
67+
.run();
68+
File buildLibs = new File("target/repackage/build/libs");
69+
assertTrue(new File(buildLibs, "repackage.jar").exists());
70+
assertFalse(new File(buildLibs, "repackage.jar.original").exists());
71+
assertFalse(new File(buildLibs, "repackage-sources.jar.original").exists());
72+
assertTrue(new File(buildLibs, "custom.jar").exists());
73+
assertTrue(new File(buildLibs, "custom.jar.original").exists());
74+
}
75+
76+
@Test
77+
public void repackagingDisabledWithCustomRepackagedJarUsingStringJarTaskReference() {
78+
project.newBuild()
79+
.forTasks("clean", "build", "customRepackagedJarWithStringReference")
80+
.withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=false")
81+
.run();
82+
File buildLibs = new File("target/repackage/build/libs");
83+
assertTrue(new File(buildLibs, "repackage.jar").exists());
84+
assertFalse(new File(buildLibs, "repackage.jar.original").exists());
85+
assertFalse(new File(buildLibs, "repackage-sources.jar.original").exists());
86+
assertTrue(new File(buildLibs, "custom.jar").exists());
87+
assertTrue(new File(buildLibs, "custom.jar.original").exists());
88+
}
89+
90+
@Test
91+
public void repackagingEnabledWithCustomRepackagedJar() {
92+
project.newBuild().forTasks("clean", "build", "customRepackagedJar")
93+
.withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=true").run();
94+
File buildLibs = new File("target/repackage/build/libs");
95+
assertTrue(new File(buildLibs, "repackage.jar").exists());
96+
assertTrue(new File(buildLibs, "repackage.jar.original").exists());
97+
assertFalse(new File(buildLibs, "repackage-sources.jar.original").exists());
98+
assertTrue(new File(buildLibs, "custom.jar").exists());
99+
assertTrue(new File(buildLibs, "custom.jar.original").exists());
100+
}
101+
102+
@Test
103+
public void repackagingEnableWithCustomRepackagedJarUsingStringJarTaskReference() {
104+
project.newBuild()
105+
.forTasks("clean", "build", "customRepackagedJarWithStringReference")
106+
.withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=true").run();
107+
File buildLibs = new File("target/repackage/build/libs");
108+
assertTrue(new File(buildLibs, "repackage.jar").exists());
109+
assertTrue(new File(buildLibs, "repackage.jar.original").exists());
110+
assertFalse(new File(buildLibs, "repackage-sources.jar.original").exists());
111+
assertTrue(new File(buildLibs, "custom.jar").exists());
112+
assertTrue(new File(buildLibs, "custom.jar.original").exists());
113+
}
114+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
buildscript {
2+
repositories {
3+
mavenLocal()
4+
}
5+
dependencies {
6+
classpath "org.springframework.boot:spring-boot-gradle-plugin:${project.bootVersion}"
7+
}
8+
}
9+
10+
repositories {
11+
mavenLocal()
12+
mavenCentral()
13+
}
14+
15+
apply plugin: 'spring-boot'
16+
apply plugin: 'java'
17+
18+
dependencies {
19+
compile 'org.springframework.boot:spring-boot-starter-freemarker'
20+
compile "org.springframework.boot:spring-boot-starter-web"
21+
}
22+
23+
springBoot {
24+
mainClass = 'foo.bar.Baz'
25+
}
26+
27+
bootRepackage.enabled = Boolean.valueOf(project.repackage)
28+
29+
task customJar(type: Jar) {
30+
archiveName = 'custom.jar'
31+
from sourceSets.main.output
32+
}
33+
34+
task sourcesJar(type: Jar, dependsOn: classes) {
35+
classifier = 'sources'
36+
from sourceSets.main.allSource
37+
}
38+
39+
artifacts {
40+
archives sourcesJar
41+
}
42+
43+
task customRepackagedJar(type: BootRepackage, dependsOn: customJar) {
44+
withJarTask = customJar
45+
}
46+
47+
task customRepackagedJarWithStringReference(type: BootRepackage, dependsOn: customJar) {
48+
withJarTask = 'customJar'
49+
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.gradle.api.DefaultTask;
2525
import org.gradle.api.Project;
2626
import org.gradle.api.tasks.TaskAction;
27-
import org.gradle.api.tasks.TaskContainer;
2827
import org.gradle.api.tasks.bundling.Jar;
2928
import org.springframework.boot.gradle.SpringBootPluginExtension;
3029
import org.springframework.boot.loader.tools.Repackager;
@@ -35,6 +34,7 @@
3534
*
3635
* @author Phillip Webb
3736
* @author Janne Valkealahti
37+
* @author Andy Wilkinson
3838
*/
3939
public class RepackageTask extends DefaultTask {
4040

@@ -55,7 +55,7 @@ public void setCustomConfiguration(String customConfiguration) {
5555
}
5656

5757
public Object getWithJarTask() {
58-
return withJarTask;
58+
return this.withJarTask;
5959
}
6060

6161
public void setWithJarTask(Object withJarTask) {
@@ -67,11 +67,11 @@ public void setMainClass(String mainClass) {
6767
}
6868

6969
public String getMainClass() {
70-
return mainClass;
70+
return this.mainClass;
7171
}
7272

7373
public String getClassifier() {
74-
return classifier;
74+
return this.classifier;
7575
}
7676

7777
public void setClassifier(String classifier) {
@@ -126,26 +126,26 @@ public void execute(Jar jarTask) {
126126
return;
127127
}
128128
Object withJarTask = RepackageTask.this.withJarTask;
129-
if (isTaskMatch(jarTask, withJarTask)) {
129+
if (!isTaskMatch(jarTask, withJarTask)) {
130130
getLogger().info(
131131
"Jar task not repackaged (didn't match withJarTask): " + jarTask);
132132
return;
133133
}
134-
if ("".equals(jarTask.getClassifier())
135-
|| RepackageTask.this.withJarTask != null) {
136-
File file = jarTask.getArchivePath();
137-
if (file.exists()) {
138-
repackage(file);
139-
}
134+
File file = jarTask.getArchivePath();
135+
if (file.exists()) {
136+
repackage(file);
140137
}
141138
}
142139

143-
private boolean isTaskMatch(Jar task, Object compare) {
144-
if (compare == null) {
145-
return false;
140+
private boolean isTaskMatch(Jar task, Object withJarTask) {
141+
if (withJarTask == null) {
142+
return isDefaultJarTask(task);
146143
}
147-
TaskContainer tasks = getProject().getTasks();
148-
return task.equals(compare) || task.equals(tasks.findByName(task.toString()));
144+
return task.equals(withJarTask) || task.getName().equals(withJarTask);
145+
}
146+
147+
private boolean isDefaultJarTask(Jar jarTask) {
148+
return "jar".equals(jarTask.getName());
149149
}
150150

151151
private void repackage(File file) {

0 commit comments

Comments
 (0)