Provide sensible defaults for launch script properties when using Gradle

Closes gh-4458
This commit is contained in:
Andy Wilkinson
2018-06-22 12:25:35 +01:00
parent b01efb2392
commit a097f923c1
6 changed files with 211 additions and 36 deletions

View File

@@ -184,7 +184,7 @@ public class BootJar extends Jar implements BootArchive {
private LaunchScriptConfiguration enableLaunchScriptIfNecessary() {
LaunchScriptConfiguration launchScript = this.support.getLaunchScript();
if (launchScript == null) {
launchScript = new LaunchScriptConfiguration();
launchScript = new LaunchScriptConfiguration(this);
this.support.setLaunchScript(launchScript);
}
return launchScript;

View File

@@ -162,7 +162,7 @@ public class BootWar extends War implements BootArchive {
private LaunchScriptConfiguration enableLaunchScriptIfNecessary() {
LaunchScriptConfiguration launchScript = this.support.getLaunchScript();
if (launchScript == null) {
launchScript = new LaunchScriptConfiguration();
launchScript = new LaunchScriptConfiguration(this);
this.support.setLaunchScript(launchScript);
}
return launchScript;

View File

@@ -22,6 +22,9 @@ import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.gradle.api.Project;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.springframework.boot.loader.tools.FileUtils;
/**
@@ -37,6 +40,19 @@ public class LaunchScriptConfiguration implements Serializable {
private File script;
public LaunchScriptConfiguration() {
}
LaunchScriptConfiguration(AbstractArchiveTask archiveTask) {
Project project = archiveTask.getProject();
putIfMissing(this.properties, "initInfoProvides", archiveTask.getBaseName());
putIfMissing(this.properties, "initInfoShortDescription",
removeLineBreaks(project.getDescription()), archiveTask.getBaseName());
putIfMissing(this.properties, "initInfoDescription",
augmentLineBreaks(project.getDescription()), archiveTask.getBaseName());
}
/**
* Returns the properties that are applied to the launch script when it's being
* including in the executable archive.
@@ -121,4 +137,24 @@ public class LaunchScriptConfiguration implements Serializable {
}
}
private String removeLineBreaks(String string) {
return (string != null ? string.replaceAll("\\s+", " ") : null);
}
private String augmentLineBreaks(String string) {
return (string != null ? string.replaceAll("\n", "\n# ") : null);
}
private void putIfMissing(Map<String, String> properties, String key,
String... valueCandidates) {
if (!properties.containsKey(key)) {
for (String candidate : valueCandidates) {
if (candidate != null && !candidate.isEmpty()) {
properties.put(key, candidate);
return;
}
}
}
}
}