diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/packaging/boot-jar-custom-launch-script.gradle b/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/packaging/boot-jar-custom-launch-script.gradle index c813b9a134f..9bfd8d6cb69 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/packaging/boot-jar-custom-launch-script.gradle +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/packaging/boot-jar-custom-launch-script.gradle @@ -14,7 +14,6 @@ bootJar { // tag::custom-launch-script[] bootJar { launchScript { - included = true script = file('src/custom.script') } } diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/packaging/boot-jar-include-launch-script.gradle b/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/packaging/boot-jar-include-launch-script.gradle index 6df82aeb198..df09f842dec 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/packaging/boot-jar-include-launch-script.gradle +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/packaging/boot-jar-include-launch-script.gradle @@ -13,8 +13,6 @@ bootJar { // tag::include-launch-script[] bootJar { - launchScript { - included = true - } + launchScript() } // end::include-launch-script[] diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/packaging/boot-jar-launch-script-properties.gradle b/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/packaging/boot-jar-launch-script-properties.gradle index 1de01e52b5b..dd1aec1cc64 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/packaging/boot-jar-launch-script-properties.gradle +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/gradle/packaging/boot-jar-launch-script-properties.gradle @@ -14,7 +14,6 @@ bootJar { // tag::launch-script-properties[] bootJar { launchScript { - included = true properties 'logFilename': 'example-app.log' } } diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchive.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchive.java index 88996acd780..d2facdc820c 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchive.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchive.java @@ -67,17 +67,24 @@ public interface BootArchive extends Task { void requiresUnpack(Spec spec); /** - * Returns the {@link LaunchScriptConfiguration} that will control the script, if any, - * that is prepended to the archive. + * Returns the {@link LaunchScriptConfiguration} that will control the script that is + * prepended to the archive. * - * @return the launch script configuration + * @return the launch script configuration, or {@code null} if the launch script has + * not been configured. */ @Input + @Optional LaunchScriptConfiguration getLaunchScript(); /** - * Applies the given {@code action} to the {@link LaunchScriptConfiguration} of this - * archive. + * Configures the archive to have a prepended launch script. + */ + void launchScript(); + + /** + * Configures the archive to have a prepended launch script, customizing its + * configuration using the given {@code action}. * * @param action the action to apply */ diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchiveSupport.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchiveSupport.java index 10876ea74cf..a88437e31e2 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchiveSupport.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchiveSupport.java @@ -61,7 +61,7 @@ class BootArchiveSupport { private final String loaderMainClass; - private LaunchScriptConfiguration launchScript = new LaunchScriptConfiguration(); + private LaunchScriptConfiguration launchScript; private boolean excludeDevtools = true; diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java index fdd1b4e3b66..17cce41b4be 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java @@ -97,9 +97,14 @@ public class BootJar extends Jar implements BootArchive { return this.support.getLaunchScript(); } + @Override + public void launchScript() { + enableLaunchScriptIfNecessary(); + } + @Override public void launchScript(Action action) { - action.execute(getLaunchScript()); + action.execute(enableLaunchScriptIfNecessary()); } @Override @@ -142,4 +147,13 @@ public class BootJar extends Jar implements BootArchive { return ZipCompression.DEFLATED; } + private LaunchScriptConfiguration enableLaunchScriptIfNecessary() { + LaunchScriptConfiguration launchScript = this.support.getLaunchScript(); + if (launchScript == null) { + launchScript = new LaunchScriptConfiguration(); + this.support.setLaunchScript(launchScript); + } + return launchScript; + } + } diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootWar.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootWar.java index 558361ad18f..76da1058eb8 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootWar.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootWar.java @@ -91,9 +91,14 @@ public class BootWar extends War implements BootArchive { return this.support.getLaunchScript(); } + @Override + public void launchScript() { + enableLaunchScriptIfNecessary(); + } + @Override public void launchScript(Action action) { - action.execute(getLaunchScript()); + action.execute(enableLaunchScriptIfNecessary()); } /** @@ -150,4 +155,13 @@ public class BootWar extends War implements BootArchive { return ZipCompression.DEFLATED; } + private LaunchScriptConfiguration enableLaunchScriptIfNecessary() { + LaunchScriptConfiguration launchScript = this.support.getLaunchScript(); + if (launchScript == null) { + launchScript = new LaunchScriptConfiguration(); + this.support.setLaunchScript(launchScript); + } + return launchScript; + } + } diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java index 9ce874dfc3d..5f93420c680 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootZipCopyAction.java @@ -183,7 +183,7 @@ class BootZipCopyAction implements CopyAction { private void writeLaunchScriptIfNecessary(FileOutputStream fileStream) { try { - if (this.launchScript.isIncluded()) { + if (this.launchScript != null) { fileStream.write(new DefaultLaunchScript(this.launchScript.getScript(), this.launchScript.getProperties()).toByteArray()); this.output.setExecutable(true); diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/LaunchScriptConfiguration.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/LaunchScriptConfiguration.java index 8aa4cae905e..1813448c744 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/LaunchScriptConfiguration.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/LaunchScriptConfiguration.java @@ -32,30 +32,10 @@ import org.springframework.boot.loader.tools.FileUtils; */ public class LaunchScriptConfiguration implements Serializable { - private boolean included = false; - private final Map properties = new HashMap<>(); private File script; - /** - * Returns whether the launch script is included. Defaults to {@code false}. - * - * @return {@code true} is the script is included, otherwise {@code false}. - */ - public boolean isIncluded() { - return this.included; - } - - /** - * Sets whether the launch script is included. Defaults to {@code false}. - * - * @param included {@code true} is the script is included, otherwise {@code false}. - */ - public void setIncluded(boolean included) { - this.included = included; - } - /** * Returns the properties that are applied to the launch script when it's being * including in the executable archive. @@ -100,7 +80,6 @@ public class LaunchScriptConfiguration implements Serializable { public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + (this.included ? 1231 : 1237); result = prime * result + ((this.properties == null) ? 0 : this.properties.hashCode()); result = prime * result + ((this.script == null) ? 0 : this.script.hashCode()); @@ -119,9 +98,6 @@ public class LaunchScriptConfiguration implements Serializable { return false; } LaunchScriptConfiguration other = (LaunchScriptConfiguration) obj; - if (this.included != other.included) { - return false; - } if (!this.properties.equals(other.properties)) { return false; } diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java index 8e769d15858..47071cc9933 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java @@ -184,7 +184,7 @@ public abstract class AbstractBootArchiveTests { @Test public void launchScriptCanBePrepended() throws IOException { this.task.setMainClass("com.example.Main"); - this.task.getLaunchScript().setIncluded(true); + this.task.launchScript(); this.task.execute(); assertThat(Files.readAllBytes(this.task.getArchivePath().toPath())) .startsWith(new DefaultLaunchScript(null, null).toByteArray()); @@ -201,12 +201,10 @@ public abstract class AbstractBootArchiveTests { @Test public void customLaunchScriptCanBePrepended() throws IOException { this.task.setMainClass("com.example.Main"); - LaunchScriptConfiguration launchScript = this.task.getLaunchScript(); - launchScript.setIncluded(true); File customScript = this.temp.newFile("custom.script"); Files.write(customScript.toPath(), Arrays.asList("custom script"), StandardOpenOption.CREATE); - launchScript.setScript(customScript); + this.task.launchScript((configuration) -> configuration.setScript(customScript)); this.task.execute(); assertThat(Files.readAllBytes(this.task.getArchivePath().toPath())) .startsWith("custom script".getBytes()); @@ -215,9 +213,8 @@ public abstract class AbstractBootArchiveTests { @Test public void launchScriptPropertiesAreReplaced() throws IOException { this.task.setMainClass("com.example.Main"); - LaunchScriptConfiguration launchScript = this.task.getLaunchScript(); - launchScript.setIncluded(true); - launchScript.getProperties().put("initInfoProvides", "test property value"); + this.task.launchScript((configuration) -> configuration.getProperties() + .put("initInfoProvides", "test property value")); this.task.execute(); assertThat(Files.readAllBytes(this.task.getArchivePath().toPath())) .containsSequence("test property value".getBytes()); diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/BootJarIntegrationTests.gradle b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/BootJarIntegrationTests.gradle index b54d7a8ecb8..f40163e7b24 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/BootJarIntegrationTests.gradle +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/BootJarIntegrationTests.gradle @@ -9,8 +9,9 @@ apply plugin: 'org.springframework.boot' bootJar { mainClass = 'com.example.Application' - launchScript { - included = project.hasProperty('includeLaunchScript') ? includeLaunchScript : false - properties 'prop' : project.hasProperty('launchScriptProperty') ? launchScriptProperty : 'default' + if (project.hasProperty('includeLaunchScript') ? includeLaunchScript : false) { + launchScript { + properties 'prop' : project.hasProperty('launchScriptProperty') ? launchScriptProperty : 'default' + } } } diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/BootWarIntegrationTests.gradle b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/BootWarIntegrationTests.gradle index 3461c7fe001..1d59feef6a6 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/BootWarIntegrationTests.gradle +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/tasks/bundling/BootWarIntegrationTests.gradle @@ -9,8 +9,9 @@ apply plugin: 'org.springframework.boot' bootWar { mainClass = 'com.example.Application' - launchScript { - included = project.hasProperty('includeLaunchScript') ? includeLaunchScript : false - properties 'prop' : project.hasProperty('launchScriptProperty') ? launchScriptProperty : 'default' + if (project.hasProperty('includeLaunchScript') ? includeLaunchScript : false) { + launchScript { + properties 'prop' : project.hasProperty('launchScriptProperty') ? launchScriptProperty : 'default' + } } }