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

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