Polish Gradle repackage

pull/1165/head
Phillip Webb 11 years ago
parent 965c16d93f
commit 72a2e5bc02

@ -22,6 +22,7 @@ import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.logging.Logger;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.tasks.bundling.Jar;
import org.springframework.boot.gradle.PluginFeatures;
@ -32,6 +33,7 @@ import org.springframework.util.StringUtils;
* {@link PluginFeatures} to add repackage support.
*
* @author Phillip Webb
* @author Dave Syer
*/
public class RepackagePluginFeatures implements PluginFeatures {
@ -58,10 +60,10 @@ public class RepackagePluginFeatures implements PluginFeatures {
private void registerOutput(Project project, final RepackageTask task) {
project.afterEvaluate(new Action<Project>() {
@Override
public void execute(Project project) {
project.getTasks().withType(Jar.class, new OutputAction(task));
project.getTasks().withType(Jar.class,
new RegisterInputsOutputsAction(task));
Object withJar = task.getWithJarTask();
if (withJar!=null) {
task.dependsOn(withJar);
@ -82,50 +84,49 @@ public class RepackagePluginFeatures implements PluginFeatures {
RepackageTask.class);
}
private class OutputAction implements Action<Jar> {
/**
* Register task input/outputs when classifiers are used
*/
private static class RegisterInputsOutputsAction implements Action<Jar> {
private final RepackageTask task;
private RepackageTask task;
private final Project project;
public OutputAction(RepackageTask task) {
public RegisterInputsOutputsAction(RepackageTask task) {
this.task = task;
this.project = task.getProject();
}
@Override
public void execute(Jar archive) {
if ("".equals(archive.getClassifier())) {
setClassifier(archive);
File file = archive.getArchivePath();
public void execute(Jar jarTask) {
if ("".equals(jarTask.getClassifier())) {
String classifier = this.task.getClassifier();
if (classifier == null) {
SpringBootPluginExtension extension = this.project.getExtensions()
.getByType(SpringBootPluginExtension.class);
classifier = extension.getClassifier();
this.task.setClassifier(classifier);
}
if (classifier != null) {
this.task.getInputs().file(archive);
task.getInputs().file(task.getDependencies());
String withClassifer = file.getName();
withClassifer = StringUtils.stripFilenameExtension(withClassifer)
+ "-" + classifier + "."
+ StringUtils.getFilenameExtension(withClassifer);
File out = new File(file.getParentFile(), withClassifer);
file = out;
this.task.getOutputs().file(file);
this.task.setOutputFile(file);
setupInputOutputs(jarTask, classifier);
}
}
}
private void setClassifier(Jar archive) {
Project project = task.getProject();
String classifier = null;
SpringBootPluginExtension extension = project.getExtensions().getByType(
SpringBootPluginExtension.class);
if (task.getClassifier() != null) {
classifier = task.getClassifier();
} else if (extension.getClassifier() != null) {
classifier = extension.getClassifier();
}
if (classifier != null) {
project.getLogger().info("Setting classifier: " + classifier);
task.setClassifier(classifier);
}
private void setupInputOutputs(Jar jarTask, String classifier) {
Logger logger = this.project.getLogger();
logger.debug("Using classifier: " + classifier + " for task "
+ task.getName());
File inputFile = jarTask.getArchivePath();
String outputName = inputFile.getName();
outputName = StringUtils.stripFilenameExtension(outputName) + "-"
+ classifier + "." + StringUtils.getFilenameExtension(outputName);
File outputFile = new File(inputFile.getParentFile(), outputName);
this.task.getInputs().file(jarTask);
this.task.getInputs().file(this.task.getDependencies());
this.task.getOutputs().file(outputFile);
this.task.setOutputFile(outputFile);
}
}

@ -26,6 +26,7 @@ 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.LibraryCallback;
@ -105,14 +106,13 @@ public class RepackageTask extends DefaultTask {
final List<File> files = new ArrayList<File>();
try {
libraries.doWithLibraries(new LibraryCallback() {
@Override
public void library(File file, LibraryScope scope) throws IOException {
files.add(file);
}
});
} catch (IOException e) {
throw new IllegalStateException("Cannot retrieve dependencies", e);
} catch (IOException ex) {
throw new IllegalStateException("Cannot retrieve dependencies", ex);
}
return files.toArray(new File[files.size()]);
}
@ -133,6 +133,9 @@ public class RepackageTask extends DefaultTask {
return libraries;
}
/**
* Action to repackage JARs.
*/
private class RepackageAction implements Action<Jar> {
private final SpringBootPluginExtension extension;
@ -146,50 +149,63 @@ public class RepackageTask extends DefaultTask {
}
@Override
public void execute(Jar archive) {
if (!enabled) {
public void execute(Jar jarTask) {
if (!RepackageTask.this.enabled) {
getLogger().info("Repackage disabled");
return;
}
// if withJarTask is set, compare tasks and bail out if we didn't match
if (RepackageTask.this.withJarTask != null
&& !(archive.equals(RepackageTask.this.withJarTask)
|| archive.equals(getProject().getTasks().findByName(
RepackageTask.this.withJarTask.toString())))) {
Object withJarTask = RepackageTask.this.withJarTask;
if (isTaskMatch(jarTask, withJarTask)) {
getLogger().info(
"Jar task not repackaged (didn't match withJarTask): " + archive);
"Jar task not repackaged (didn't match withJarTask): " + jarTask);
return;
}
if ("".equals(archive.getClassifier())
if ("".equals(jarTask.getClassifier())
|| RepackageTask.this.withJarTask != null) {
File file = archive.getArchivePath();
File file = jarTask.getArchivePath();
if (file.exists()) {
Repackager repackager = new LoggingRepackager(file);
File out = RepackageTask.this.outputFile;
if (out != null && !file.equals(out)) {
try {
FileCopyUtils.copy(file, out);
} catch (IOException ex) {
throw new IllegalStateException(ex.getMessage(), ex);
}
file = out;
}
RepackageTask.this.outputFile = file;
setMainClass(repackager);
if (this.extension.convertLayout() != null) {
repackager.setLayout(this.extension.convertLayout());
}
repackager.setBackupSource(this.extension.isBackupSource());
try {
repackager.repackage(file, this.libraries);
} catch (IOException ex) {
throw new IllegalStateException(ex.getMessage(), ex);
}
repackage(file);
}
}
}
private boolean isTaskMatch(Jar task, Object compare) {
if (compare == null) {
return false;
}
TaskContainer tasks = getProject().getTasks();
return task.equals(compare) || task.equals(tasks.findByName(task.toString()));
}
private void repackage(File file) {
File outputFile = RepackageTask.this.outputFile;
if (outputFile != null && !file.equals(outputFile)) {
copy(file, outputFile);
file = outputFile;
}
Repackager repackager = new LoggingRepackager(file);
setMainClass(repackager);
if (this.extension.convertLayout() != null) {
repackager.setLayout(this.extension.convertLayout());
}
repackager.setBackupSource(this.extension.isBackupSource());
try {
repackager.repackage(file, this.libraries);
}
catch (IOException ex) {
throw new IllegalStateException(ex.getMessage(), ex);
}
}
private void copy(File source, File dest) {
try {
FileCopyUtils.copy(source, dest);
}
catch (IOException ex) {
throw new IllegalStateException(ex.getMessage(), ex);
}
}
private void setMainClass(Repackager repackager) {
String mainClass = (String) getProject().property("mainClassName");
if (RepackageTask.this.mainClass != null) {
@ -205,6 +221,9 @@ public class RepackageTask extends DefaultTask {
}
}
/**
* {@link Repackager} that also logs when searching takes too long.
*/
private class LoggingRepackager extends Repackager {
public LoggingRepackager(File source) {

Loading…
Cancel
Save