From 4be688aa7858aca1e962fe8b57fac67ec04d2576 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 3 Jul 2014 11:24:11 +0100 Subject: [PATCH] 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 --- .../boot/gradle/RepackagingTests.java | 114 ++++++++++++++++++ .../src/test/resources/repackage.gradle | 49 ++++++++ .../boot/gradle/repackage/RepackageTask.java | 32 ++--- 3 files changed, 179 insertions(+), 16 deletions(-) create mode 100644 spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/RepackagingTests.java create mode 100644 spring-boot-integration-tests/src/test/resources/repackage.gradle diff --git a/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/RepackagingTests.java b/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/RepackagingTests.java new file mode 100644 index 0000000000..0a6cea36cf --- /dev/null +++ b/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/RepackagingTests.java @@ -0,0 +1,114 @@ +/* + * Copyright 2012-2014 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.gradle; + +import java.io.File; +import java.io.IOException; + +import org.gradle.tooling.ProjectConnection; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.boot.dependency.tools.ManagedDependencies; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class RepackagingTests { + + private static final String BOOT_VERSION = ManagedDependencies.get() + .find("spring-boot").getVersion(); + + private static ProjectConnection project; + + @BeforeClass + public static void createProject() throws IOException { + project = new ProjectCreator().createProject("repackage"); + } + + @Test + public void repackagingEnabled() { + project.newBuild().forTasks("clean", "build") + .withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=true").run(); + File buildLibs = new File("target/repackage/build/libs"); + assertTrue(new File(buildLibs, "repackage.jar").exists()); + assertTrue(new File(buildLibs, "repackage.jar.original").exists()); + assertFalse(new File(buildLibs, "repackage-sources.jar.original").exists()); + } + + @Test + public void repackagingDisabled() { + project.newBuild().forTasks("clean", "build") + .withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=false") + .run(); + File buildLibs = new File("target/repackage/build/libs"); + assertTrue(new File(buildLibs, "repackage.jar").exists()); + assertFalse(new File(buildLibs, "repackage.jar.original").exists()); + assertFalse(new File(buildLibs, "repackage-sources.jar.original").exists()); + } + + @Test + public void repackagingDisabledWithCustomRepackagedJar() { + project.newBuild().forTasks("clean", "build", "customRepackagedJar") + .withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=false") + .run(); + File buildLibs = new File("target/repackage/build/libs"); + assertTrue(new File(buildLibs, "repackage.jar").exists()); + assertFalse(new File(buildLibs, "repackage.jar.original").exists()); + assertFalse(new File(buildLibs, "repackage-sources.jar.original").exists()); + assertTrue(new File(buildLibs, "custom.jar").exists()); + assertTrue(new File(buildLibs, "custom.jar.original").exists()); + } + + @Test + public void repackagingDisabledWithCustomRepackagedJarUsingStringJarTaskReference() { + project.newBuild() + .forTasks("clean", "build", "customRepackagedJarWithStringReference") + .withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=false") + .run(); + File buildLibs = new File("target/repackage/build/libs"); + assertTrue(new File(buildLibs, "repackage.jar").exists()); + assertFalse(new File(buildLibs, "repackage.jar.original").exists()); + assertFalse(new File(buildLibs, "repackage-sources.jar.original").exists()); + assertTrue(new File(buildLibs, "custom.jar").exists()); + assertTrue(new File(buildLibs, "custom.jar.original").exists()); + } + + @Test + public void repackagingEnabledWithCustomRepackagedJar() { + project.newBuild().forTasks("clean", "build", "customRepackagedJar") + .withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=true").run(); + File buildLibs = new File("target/repackage/build/libs"); + assertTrue(new File(buildLibs, "repackage.jar").exists()); + assertTrue(new File(buildLibs, "repackage.jar.original").exists()); + assertFalse(new File(buildLibs, "repackage-sources.jar.original").exists()); + assertTrue(new File(buildLibs, "custom.jar").exists()); + assertTrue(new File(buildLibs, "custom.jar.original").exists()); + } + + @Test + public void repackagingEnableWithCustomRepackagedJarUsingStringJarTaskReference() { + project.newBuild() + .forTasks("clean", "build", "customRepackagedJarWithStringReference") + .withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=true").run(); + File buildLibs = new File("target/repackage/build/libs"); + assertTrue(new File(buildLibs, "repackage.jar").exists()); + assertTrue(new File(buildLibs, "repackage.jar.original").exists()); + assertFalse(new File(buildLibs, "repackage-sources.jar.original").exists()); + assertTrue(new File(buildLibs, "custom.jar").exists()); + assertTrue(new File(buildLibs, "custom.jar.original").exists()); + } +} diff --git a/spring-boot-integration-tests/src/test/resources/repackage.gradle b/spring-boot-integration-tests/src/test/resources/repackage.gradle new file mode 100644 index 0000000000..f7ea1b3d22 --- /dev/null +++ b/spring-boot-integration-tests/src/test/resources/repackage.gradle @@ -0,0 +1,49 @@ +buildscript { + repositories { + mavenLocal() + } + dependencies { + classpath "org.springframework.boot:spring-boot-gradle-plugin:${project.bootVersion}" + } +} + +repositories { + mavenLocal() + mavenCentral() +} + +apply plugin: 'spring-boot' +apply plugin: 'java' + +dependencies { + compile 'org.springframework.boot:spring-boot-starter-freemarker' + compile "org.springframework.boot:spring-boot-starter-web" +} + +springBoot { + mainClass = 'foo.bar.Baz' +} + +bootRepackage.enabled = Boolean.valueOf(project.repackage) + +task customJar(type: Jar) { + archiveName = 'custom.jar' + from sourceSets.main.output +} + +task sourcesJar(type: Jar, dependsOn: classes) { + classifier = 'sources' + from sourceSets.main.allSource +} + +artifacts { + archives sourcesJar +} + +task customRepackagedJar(type: BootRepackage, dependsOn: customJar) { + withJarTask = customJar +} + +task customRepackagedJarWithStringReference(type: BootRepackage, dependsOn: customJar) { + withJarTask = 'customJar' +} \ No newline at end of file diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackageTask.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackageTask.java index 3e9271a775..9125875590 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackageTask.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackageTask.java @@ -24,7 +24,6 @@ import org.gradle.api.Action; import org.gradle.api.DefaultTask; import org.gradle.api.Project; import org.gradle.api.tasks.TaskAction; -import org.gradle.api.tasks.TaskContainer; import org.gradle.api.tasks.bundling.Jar; import org.springframework.boot.gradle.SpringBootPluginExtension; import org.springframework.boot.loader.tools.Repackager; @@ -35,6 +34,7 @@ import org.springframework.util.FileCopyUtils; * * @author Phillip Webb * @author Janne Valkealahti + * @author Andy Wilkinson */ public class RepackageTask extends DefaultTask { @@ -55,7 +55,7 @@ public class RepackageTask extends DefaultTask { } public Object getWithJarTask() { - return withJarTask; + return this.withJarTask; } public void setWithJarTask(Object withJarTask) { @@ -67,11 +67,11 @@ public class RepackageTask extends DefaultTask { } public String getMainClass() { - return mainClass; + return this.mainClass; } public String getClassifier() { - return classifier; + return this.classifier; } public void setClassifier(String classifier) { @@ -126,26 +126,26 @@ public class RepackageTask extends DefaultTask { return; } Object withJarTask = RepackageTask.this.withJarTask; - if (isTaskMatch(jarTask, withJarTask)) { + if (!isTaskMatch(jarTask, withJarTask)) { getLogger().info( "Jar task not repackaged (didn't match withJarTask): " + jarTask); return; } - if ("".equals(jarTask.getClassifier()) - || RepackageTask.this.withJarTask != null) { - File file = jarTask.getArchivePath(); - if (file.exists()) { - repackage(file); - } + File file = jarTask.getArchivePath(); + if (file.exists()) { + repackage(file); } } - private boolean isTaskMatch(Jar task, Object compare) { - if (compare == null) { - return false; + private boolean isTaskMatch(Jar task, Object withJarTask) { + if (withJarTask == null) { + return isDefaultJarTask(task); } - TaskContainer tasks = getProject().getTasks(); - return task.equals(compare) || task.equals(tasks.findByName(task.toString())); + return task.equals(withJarTask) || task.getName().equals(withJarTask); + } + + private boolean isDefaultJarTask(Jar jarTask) { + return "jar".equals(jarTask.getName()); } private void repackage(File file) {