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
This commit is contained in:
Andy Wilkinson 2014-07-03 11:24:11 +01:00
parent 60e60227ad
commit 4be688aa78
3 changed files with 179 additions and 16 deletions

View File

@ -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());
}
}

View File

@ -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'
}

View File

@ -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) {