Skip to content

Commit c808de0

Browse files
committed
Allow custom repackage task to be used without a global main class
Closes gh-5956
1 parent 76cd45e commit c808de0

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ public void repackagingEnabledExcludeDevtools() throws IOException {
159159
assertFalse(isDevToolsJarIncluded(repackageFile));
160160
}
161161

162+
@Test
163+
public void customRepackagingTaskWithOwnMainClassNameAnNoGlobalMainClassName() {
164+
project.newBuild().forTasks("clean", "customRepackagedJarWithOwnMainClass")
165+
.withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=true",
166+
"-PexcludeDevtools=false", "-PnoMainClass=true")
167+
.run();
168+
File buildLibs = new File("target/repackage/build/libs");
169+
assertTrue(new File(buildLibs, "custom.jar").exists());
170+
assertTrue(new File(buildLibs, "custom.jar.original").exists());
171+
}
172+
162173
private boolean isDevToolsJarIncluded(File repackageFile) throws IOException {
163174
JarFile jarFile = new JarFile(repackageFile);
164175
try {

spring-boot-integration-tests/spring-boot-gradle-tests/src/test/resources/repackage.gradle

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ dependencies {
2323
}
2424

2525
springBoot {
26-
mainClass = 'foo.bar.Baz'
26+
if (!project.hasProperty("noMainClass")) {
27+
mainClass = 'foo.bar.Baz'
28+
}
2729
excludeDevtools = Boolean.valueOf(project.excludeDevtools)
2830
}
2931

@@ -50,3 +52,8 @@ task customRepackagedJar(type: BootRepackage, dependsOn: customJar) {
5052
task customRepackagedJarWithStringReference(type: BootRepackage, dependsOn: customJar) {
5153
withJarTask = 'customJar'
5254
}
55+
56+
task customRepackagedJarWithOwnMainClass(type: BootRepackage, dependsOn: customJar) {
57+
withJarTask = customJar
58+
mainClass = 'foo.bar.Baz'
59+
}

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,16 @@ private void copy(File source, File dest) {
240240
}
241241

242242
private void setMainClass(Repackager repackager) {
243-
String mainClass;
243+
String mainClass = null;
244244
if (getProject().hasProperty("mainClassName")) {
245245
mainClass = (String) getProject().property("mainClassName");
246246
}
247247
else {
248248
ExtraPropertiesExtension extraProperties = (ExtraPropertiesExtension) getProject()
249249
.getExtensions().getByName("ext");
250-
mainClass = (String) extraProperties.get("mainClassName");
250+
if (extraProperties.has("mainClassName")) {
251+
mainClass = (String) extraProperties.get("mainClassName");
252+
}
251253
}
252254
if (RepackageTask.this.mainClass != null) {
253255
mainClass = RepackageTask.this.mainClass;
@@ -262,8 +264,13 @@ else if (this.extension.getMainClass() != null) {
262264
.property("main");
263265
}
264266
}
265-
getLogger().info("Setting mainClass: " + mainClass);
266-
repackager.setMainClass(mainClass);
267+
if (mainClass != null) {
268+
getLogger().info("Setting mainClass: " + mainClass);
269+
repackager.setMainClass(mainClass);
270+
}
271+
else {
272+
getLogger().info("No mainClass configured");
273+
}
267274
}
268275

269276
private LaunchScript getLaunchScript() throws IOException {

0 commit comments

Comments
 (0)