diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java index 6461014cd0df..673a17a8f135 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java @@ -95,7 +95,7 @@ public void createTask() { @Test public void basicArchiveCreation() throws IOException { this.task.setMainClassName("com.example.Main"); - this.task.execute(); + executeTask(); assertThat(this.task.getArchivePath()).exists(); try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { assertThat(jarFile.getManifest().getMainAttributes().getValue("Main-Class")) @@ -109,7 +109,7 @@ public void basicArchiveCreation() throws IOException { public void classpathJarsArePackagedBeneathLibPath() throws IOException { this.task.setMainClassName("com.example.Main"); this.task.classpath(this.temp.newFile("one.jar"), this.temp.newFile("two.jar")); - this.task.execute(); + executeTask(); try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { assertThat(jarFile.getEntry(this.libPath + "/one.jar")).isNotNull(); assertThat(jarFile.getEntry(this.libPath + "/two.jar")).isNotNull(); @@ -125,7 +125,7 @@ public void classpathFoldersArePackagedBeneathClassesPath() throws IOException { applicationClass.getParentFile().mkdirs(); applicationClass.createNewFile(); this.task.classpath(classpathFolder); - this.task.execute(); + executeTask(); try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { assertThat( jarFile.getEntry(this.classesPath + "/com/example/Application.class")) @@ -139,7 +139,7 @@ public void classpathCanBeSetUsingAFileCollection() throws IOException { this.task.classpath(this.temp.newFile("one.jar")); this.task .setClasspath(this.task.getProject().files(this.temp.newFile("two.jar"))); - this.task.execute(); + executeTask(); try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { assertThat(jarFile.getEntry(this.libPath + "/one.jar")).isNull(); assertThat(jarFile.getEntry(this.libPath + "/two.jar")).isNotNull(); @@ -151,7 +151,7 @@ public void classpathCanBeSetUsingAnObject() throws IOException { this.task.setMainClassName("com.example.Main"); this.task.classpath(this.temp.newFile("one.jar")); this.task.setClasspath(this.temp.newFile("two.jar")); - this.task.execute(); + executeTask(); try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { assertThat(jarFile.getEntry(this.libPath + "/one.jar")).isNull(); assertThat(jarFile.getEntry(this.libPath + "/two.jar")).isNotNull(); @@ -161,7 +161,7 @@ public void classpathCanBeSetUsingAnObject() throws IOException { @Test public void loaderIsWrittenToTheRootOfTheJar() throws IOException { this.task.setMainClassName("com.example.Main"); - this.task.execute(); + executeTask(); try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { assertThat(jarFile.getEntry( "org/springframework/boot/loader/LaunchedURLClassLoader.class")) @@ -174,7 +174,7 @@ public void loaderIsWrittenToTheRootOfTheJar() throws IOException { public void loaderIsWrittenToTheRootOfTheJarWhenUsingThePropertiesLauncher() throws IOException { this.task.setMainClassName("com.example.Main"); - this.task.execute(); + executeTask(); this.task.getManifest().getAttributes().put("Main-Class", "org.springframework.boot.loader.PropertiesLauncher"); try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { @@ -190,7 +190,7 @@ public void unpackCommentIsAddedToEntryIdentifiedByAPattern() throws IOException this.task.setMainClassName("com.example.Main"); this.task.classpath(this.temp.newFile("one.jar"), this.temp.newFile("two.jar")); this.task.requiresUnpack("**/one.jar"); - this.task.execute(); + executeTask(); try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { assertThat(jarFile.getEntry(this.libPath + "/one.jar").getComment()) .startsWith("UNPACK:"); @@ -203,7 +203,7 @@ public void unpackCommentIsAddedToEntryIdentifiedByASpec() throws IOException { this.task.setMainClassName("com.example.Main"); this.task.classpath(this.temp.newFile("one.jar"), this.temp.newFile("two.jar")); this.task.requiresUnpack((element) -> element.getName().endsWith("two.jar")); - this.task.execute(); + executeTask(); try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { assertThat(jarFile.getEntry(this.libPath + "/two.jar").getComment()) .startsWith("UNPACK:"); @@ -215,7 +215,7 @@ public void unpackCommentIsAddedToEntryIdentifiedByASpec() throws IOException { public void launchScriptCanBePrepended() throws IOException { this.task.setMainClassName("com.example.Main"); this.task.launchScript(); - this.task.execute(); + executeTask(); Map properties = new HashMap<>(); properties.put("initInfoProvides", this.task.getBaseName()); properties.put("initInfoShortDescription", this.project.getDescription()); @@ -239,7 +239,7 @@ public void customLaunchScriptCanBePrepended() throws IOException { Files.write(customScript.toPath(), Arrays.asList("custom script"), StandardOpenOption.CREATE); this.task.launchScript((configuration) -> configuration.setScript(customScript)); - this.task.execute(); + executeTask(); assertThat(Files.readAllBytes(this.task.getArchivePath().toPath())) .startsWith("custom script".getBytes()); } @@ -253,7 +253,7 @@ public void launchScriptInitInfoPropertiesCanBeCustomized() throws IOException { "short description"); configuration.getProperties().put("initInfoDescription", "description"); }); - this.task.execute(); + executeTask(); byte[] bytes = Files.readAllBytes(this.task.getArchivePath().toPath()); assertThat(bytes).containsSequence("Provides: provides".getBytes()); assertThat(bytes) @@ -266,7 +266,7 @@ public void customMainClassInTheManifestIsHonored() throws IOException { this.task.setMainClassName("com.example.Main"); this.task.getManifest().getAttributes().put("Main-Class", "com.example.CustomLauncher"); - this.task.execute(); + executeTask(); assertThat(this.task.getArchivePath()).exists(); try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { assertThat(jarFile.getManifest().getMainAttributes().getValue("Main-Class")) @@ -284,7 +284,7 @@ public void customStartClassInTheManifestIsHonored() throws IOException { this.task.setMainClassName("com.example.Main"); this.task.getManifest().getAttributes().put("Start-Class", "com.example.CustomMain"); - this.task.execute(); + executeTask(); assertThat(this.task.getArchivePath()).exists(); try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { assertThat(jarFile.getManifest().getMainAttributes().getValue("Main-Class")) @@ -298,7 +298,7 @@ public void customStartClassInTheManifestIsHonored() throws IOException { public void fileTimestampPreservationCanBeDisabled() throws IOException { this.task.setMainClassName("com.example.Main"); this.task.setPreserveFileTimestamps(false); - this.task.execute(); + executeTask(); assertThat(this.task.getArchivePath()).exists(); try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { Enumeration entries = jarFile.entries(); @@ -316,7 +316,7 @@ public void reproducibleOrderingCanBeEnabled() throws IOException { this.task.from(this.temp.newFile("bravo.txt"), this.temp.newFile("alpha.txt"), this.temp.newFile("charlie.txt")); this.task.setReproducibleFileOrder(true); - this.task.execute(); + executeTask(); assertThat(this.task.getArchivePath()).exists(); List textFiles = new ArrayList<>(); try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { @@ -335,7 +335,7 @@ public void reproducibleOrderingCanBeEnabled() throws IOException { public void devtoolsJarIsExcludedByDefault() throws IOException { this.task.setMainClassName("com.example.Main"); this.task.classpath(this.temp.newFile("spring-boot-devtools-0.1.2.jar")); - this.task.execute(); + executeTask(); assertThat(this.task.getArchivePath()).exists(); try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { assertThat(jarFile.getEntry(this.libPath + "/spring-boot-devtools-0.1.2.jar")) @@ -348,7 +348,7 @@ public void devtoolsJarCanBeIncluded() throws IOException { this.task.setMainClassName("com.example.Main"); this.task.classpath(this.temp.newFile("spring-boot-devtools-0.1.2.jar")); this.task.setExcludeDevtools(false); - this.task.execute(); + executeTask(); assertThat(this.task.getArchivePath()).exists(); try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { assertThat(jarFile.getEntry(this.libPath + "/spring-boot-devtools-0.1.2.jar")) @@ -365,7 +365,7 @@ public void allEntriesUseUnixPlatformAndUtf8NameEncoding() throws IOException { resource.getParentFile().mkdirs(); resource.createNewFile(); this.task.classpath(classpathFolder); - this.task.execute(); + executeTask(); File archivePath = this.task.getArchivePath(); try (ZipFile zip = new ZipFile(archivePath)) { Enumeration entries = zip.getEntries(); @@ -390,7 +390,7 @@ public void loaderIsWrittenFirstThenApplicationClassesThenLibraries() this.temp.newFile("second-library.jar"), this.temp.newFile("third-library.jar")); this.task.requiresUnpack("second-library.jar"); - this.task.execute(); + executeTask(); assertThat(getEntryNames(this.task.getArchivePath())).containsSubsequence( "org/springframework/boot/loader/", this.classesPath + "/com/example/Application.class", @@ -405,6 +405,8 @@ private T configure(T task) throws IOException { return task; } + protected abstract void executeTask(); + protected T getTask() { return this.task; } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java index 08568d9a6cc7..fd0ef1173587 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootJarTests.java @@ -42,7 +42,7 @@ public void contentCanBeAddedToBootInfUsingCopySpecFromGetter() throws IOExcepti bootJar.setMainClassName("com.example.Application"); bootJar.getBootInf().into("test") .from(new File("build.gradle").getAbsolutePath()); - bootJar.execute(); + bootJar.copy(); try (JarFile jarFile = new JarFile(bootJar.getArchivePath())) { assertThat(jarFile.getJarEntry("BOOT-INF/test/build.gradle")).isNotNull(); } @@ -54,10 +54,15 @@ public void contentCanBeAddedToBootInfUsingCopySpecAction() throws IOException { bootJar.setMainClassName("com.example.Application"); bootJar.bootInf((copySpec) -> copySpec.into("test") .from(new File("build.gradle").getAbsolutePath())); - bootJar.execute(); + bootJar.copy(); try (JarFile jarFile = new JarFile(bootJar.getArchivePath())) { assertThat(jarFile.getJarEntry("BOOT-INF/test/build.gradle")).isNotNull(); } } + @Override + protected void executeTask() { + getTask().copy(); + } + } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootWarTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootWarTests.java index bafd44408480..0452d2ed829f 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootWarTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootWarTests.java @@ -41,7 +41,7 @@ public void providedClasspathJarsArePackagedInWebInfLibProvided() throws IOExcep getTask().setMainClassName("com.example.Main"); getTask().providedClasspath(this.temp.newFile("one.jar"), this.temp.newFile("two.jar")); - getTask().execute(); + executeTask(); try (JarFile jarFile = new JarFile(getTask().getArchivePath())) { assertThat(jarFile.getEntry("WEB-INF/lib-provided/one.jar")).isNotNull(); assertThat(jarFile.getEntry("WEB-INF/lib-provided/two.jar")).isNotNull(); @@ -54,7 +54,7 @@ public void providedClasspathCanBeSetUsingAFileCollection() throws IOException { getTask().providedClasspath(this.temp.newFile("one.jar")); getTask().setProvidedClasspath( getTask().getProject().files(this.temp.newFile("two.jar"))); - getTask().execute(); + executeTask(); try (JarFile jarFile = new JarFile(getTask().getArchivePath())) { assertThat(jarFile.getEntry("WEB-INF/lib-provided/one.jar")).isNull(); assertThat(jarFile.getEntry("WEB-INF/lib-provided/two.jar")).isNotNull(); @@ -66,7 +66,7 @@ public void providedClasspathCanBeSetUsingAnObject() throws IOException { getTask().setMainClassName("com.example.Main"); getTask().providedClasspath(this.temp.newFile("one.jar")); getTask().setProvidedClasspath(this.temp.newFile("two.jar")); - getTask().execute(); + executeTask(); try (JarFile jarFile = new JarFile(getTask().getArchivePath())) { assertThat(jarFile.getEntry("WEB-INF/lib-provided/one.jar")).isNull(); assertThat(jarFile.getEntry("WEB-INF/lib-provided/two.jar")).isNotNull(); @@ -78,7 +78,7 @@ public void devtoolsJarIsExcludedByDefaultWhenItsOnTheProvidedClasspath() throws IOException { getTask().setMainClassName("com.example.Main"); getTask().providedClasspath(this.temp.newFile("spring-boot-devtools-0.1.2.jar")); - getTask().execute(); + executeTask(); assertThat(getTask().getArchivePath()).exists(); try (JarFile jarFile = new JarFile(getTask().getArchivePath())) { assertThat(jarFile @@ -93,7 +93,7 @@ public void devtoolsJarCanBeIncludedWhenItsOnTheProvidedClasspath() getTask().setMainClassName("com.example.Main"); getTask().providedClasspath(this.temp.newFile("spring-boot-devtools-0.1.2.jar")); getTask().setExcludeDevtools(false); - getTask().execute(); + executeTask(); assertThat(getTask().getArchivePath()).exists(); try (JarFile jarFile = new JarFile(getTask().getArchivePath())) { assertThat(jarFile @@ -111,7 +111,7 @@ public void webappResourcesInDirectoriesThatOverlapWithLoaderCanBePackaged() new File(orgFolder, "foo.txt").createNewFile(); getTask().from(webappFolder); getTask().setMainClassName("com.example.Main"); - getTask().execute(); + executeTask(); assertThat(getTask().getArchivePath()).exists(); try (JarFile jarFile = new JarFile(getTask().getArchivePath())) { assertThat(jarFile.getEntry("org/")).isNotNull(); @@ -124,9 +124,14 @@ public void libProvidedEntriesAreWrittenAfterLibEntries() throws IOException { getTask().setMainClassName("com.example.Main"); getTask().classpath(this.temp.newFile("library.jar")); getTask().providedClasspath(this.temp.newFile("provided-library.jar")); - getTask().execute(); + executeTask(); assertThat(getEntryNames(getTask().getArchivePath())).containsSubsequence( "WEB-INF/lib/library.jar", "WEB-INF/lib-provided/provided-library.jar"); } + @Override + protected void executeTask() { + getTask().copy(); + } + }