Polish Gradle repackage
This commit is contained in:
parent
965c16d93f
commit
72a2e5bc02
|
|
@ -22,6 +22,7 @@ import org.gradle.api.Action;
|
||||||
import org.gradle.api.Project;
|
import org.gradle.api.Project;
|
||||||
import org.gradle.api.Task;
|
import org.gradle.api.Task;
|
||||||
import org.gradle.api.artifacts.Dependency;
|
import org.gradle.api.artifacts.Dependency;
|
||||||
|
import org.gradle.api.logging.Logger;
|
||||||
import org.gradle.api.plugins.BasePlugin;
|
import org.gradle.api.plugins.BasePlugin;
|
||||||
import org.gradle.api.tasks.bundling.Jar;
|
import org.gradle.api.tasks.bundling.Jar;
|
||||||
import org.springframework.boot.gradle.PluginFeatures;
|
import org.springframework.boot.gradle.PluginFeatures;
|
||||||
|
|
@ -32,6 +33,7 @@ import org.springframework.util.StringUtils;
|
||||||
* {@link PluginFeatures} to add repackage support.
|
* {@link PluginFeatures} to add repackage support.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Dave Syer
|
||||||
*/
|
*/
|
||||||
public class RepackagePluginFeatures implements PluginFeatures {
|
public class RepackagePluginFeatures implements PluginFeatures {
|
||||||
|
|
||||||
|
|
@ -58,10 +60,10 @@ public class RepackagePluginFeatures implements PluginFeatures {
|
||||||
|
|
||||||
private void registerOutput(Project project, final RepackageTask task) {
|
private void registerOutput(Project project, final RepackageTask task) {
|
||||||
project.afterEvaluate(new Action<Project>() {
|
project.afterEvaluate(new Action<Project>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(Project project) {
|
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();
|
Object withJar = task.getWithJarTask();
|
||||||
if (withJar!=null) {
|
if (withJar!=null) {
|
||||||
task.dependsOn(withJar);
|
task.dependsOn(withJar);
|
||||||
|
|
@ -82,51 +84,50 @@ public class RepackagePluginFeatures implements PluginFeatures {
|
||||||
RepackageTask.class);
|
RepackageTask.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class OutputAction implements Action<Jar> {
|
/**
|
||||||
|
* Register task input/outputs when classifiers are used
|
||||||
|
*/
|
||||||
|
private static class RegisterInputsOutputsAction implements Action<Jar> {
|
||||||
|
|
||||||
private RepackageTask task;
|
private final RepackageTask task;
|
||||||
|
|
||||||
public OutputAction(RepackageTask task) {
|
private final Project project;
|
||||||
|
|
||||||
|
public RegisterInputsOutputsAction(RepackageTask task) {
|
||||||
this.task = task;
|
this.task = task;
|
||||||
|
this.project = task.getProject();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(Jar archive) {
|
public void execute(Jar jarTask) {
|
||||||
if ("".equals(archive.getClassifier())) {
|
if ("".equals(jarTask.getClassifier())) {
|
||||||
setClassifier(archive);
|
|
||||||
File file = archive.getArchivePath();
|
|
||||||
String classifier = this.task.getClassifier();
|
String classifier = this.task.getClassifier();
|
||||||
if (classifier != null) {
|
if (classifier == null) {
|
||||||
this.task.getInputs().file(archive);
|
SpringBootPluginExtension extension = this.project.getExtensions()
|
||||||
task.getInputs().file(task.getDependencies());
|
.getByType(SpringBootPluginExtension.class);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
classifier = extension.getClassifier();
|
||||||
|
this.task.setClassifier(classifier);
|
||||||
}
|
}
|
||||||
if (classifier != null) {
|
if (classifier != null) {
|
||||||
project.getLogger().info("Setting classifier: " + classifier);
|
setupInputOutputs(jarTask, 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.DefaultTask;
|
||||||
import org.gradle.api.Project;
|
import org.gradle.api.Project;
|
||||||
import org.gradle.api.tasks.TaskAction;
|
import org.gradle.api.tasks.TaskAction;
|
||||||
|
import org.gradle.api.tasks.TaskContainer;
|
||||||
import org.gradle.api.tasks.bundling.Jar;
|
import org.gradle.api.tasks.bundling.Jar;
|
||||||
import org.springframework.boot.gradle.SpringBootPluginExtension;
|
import org.springframework.boot.gradle.SpringBootPluginExtension;
|
||||||
import org.springframework.boot.loader.tools.LibraryCallback;
|
import org.springframework.boot.loader.tools.LibraryCallback;
|
||||||
|
|
@ -105,14 +106,13 @@ public class RepackageTask extends DefaultTask {
|
||||||
final List<File> files = new ArrayList<File>();
|
final List<File> files = new ArrayList<File>();
|
||||||
try {
|
try {
|
||||||
libraries.doWithLibraries(new LibraryCallback() {
|
libraries.doWithLibraries(new LibraryCallback() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void library(File file, LibraryScope scope) throws IOException {
|
public void library(File file, LibraryScope scope) throws IOException {
|
||||||
files.add(file);
|
files.add(file);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (IOException e) {
|
} catch (IOException ex) {
|
||||||
throw new IllegalStateException("Cannot retrieve dependencies", e);
|
throw new IllegalStateException("Cannot retrieve dependencies", ex);
|
||||||
}
|
}
|
||||||
return files.toArray(new File[files.size()]);
|
return files.toArray(new File[files.size()]);
|
||||||
}
|
}
|
||||||
|
|
@ -133,6 +133,9 @@ public class RepackageTask extends DefaultTask {
|
||||||
return libraries;
|
return libraries;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Action to repackage JARs.
|
||||||
|
*/
|
||||||
private class RepackageAction implements Action<Jar> {
|
private class RepackageAction implements Action<Jar> {
|
||||||
|
|
||||||
private final SpringBootPluginExtension extension;
|
private final SpringBootPluginExtension extension;
|
||||||
|
|
@ -146,36 +149,41 @@ public class RepackageTask extends DefaultTask {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(Jar archive) {
|
public void execute(Jar jarTask) {
|
||||||
if (!enabled) {
|
if (!RepackageTask.this.enabled) {
|
||||||
getLogger().info("Repackage disabled");
|
getLogger().info("Repackage disabled");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// if withJarTask is set, compare tasks and bail out if we didn't match
|
Object withJarTask = RepackageTask.this.withJarTask;
|
||||||
if (RepackageTask.this.withJarTask != null
|
if (isTaskMatch(jarTask, withJarTask)) {
|
||||||
&& !(archive.equals(RepackageTask.this.withJarTask)
|
|
||||||
|| archive.equals(getProject().getTasks().findByName(
|
|
||||||
RepackageTask.this.withJarTask.toString())))) {
|
|
||||||
getLogger().info(
|
getLogger().info(
|
||||||
"Jar task not repackaged (didn't match withJarTask): " + archive);
|
"Jar task not repackaged (didn't match withJarTask): " + jarTask);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if ("".equals(jarTask.getClassifier())
|
||||||
if ("".equals(archive.getClassifier())
|
|
||||||
|| RepackageTask.this.withJarTask != null) {
|
|| RepackageTask.this.withJarTask != null) {
|
||||||
File file = archive.getArchivePath();
|
File file = jarTask.getArchivePath();
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
|
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);
|
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);
|
setMainClass(repackager);
|
||||||
if (this.extension.convertLayout() != null) {
|
if (this.extension.convertLayout() != null) {
|
||||||
repackager.setLayout(this.extension.convertLayout());
|
repackager.setLayout(this.extension.convertLayout());
|
||||||
|
|
@ -183,10 +191,18 @@ public class RepackageTask extends DefaultTask {
|
||||||
repackager.setBackupSource(this.extension.isBackupSource());
|
repackager.setBackupSource(this.extension.isBackupSource());
|
||||||
try {
|
try {
|
||||||
repackager.repackage(file, this.libraries);
|
repackager.repackage(file, this.libraries);
|
||||||
} catch (IOException ex) {
|
}
|
||||||
|
catch (IOException ex) {
|
||||||
throw new IllegalStateException(ex.getMessage(), 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -205,6 +221,9 @@ public class RepackageTask extends DefaultTask {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link Repackager} that also logs when searching takes too long.
|
||||||
|
*/
|
||||||
private class LoggingRepackager extends Repackager {
|
private class LoggingRepackager extends Repackager {
|
||||||
|
|
||||||
public LoggingRepackager(File source) {
|
public LoggingRepackager(File source) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue