diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchive.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchive.java index 96d73628748b..86f4ef23f2de 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchive.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchive.java @@ -93,11 +93,26 @@ public interface BootArchive extends Task { /** * Adds files to the classpath to include in the archive. The given {@code classpath} - * are evaluated as per {@link Project#files(Object...)}. + * is evaluated as per {@link Project#files(Object...)}. * @param classpath the additions to the classpath */ void classpath(Object... classpath); + /** + * Sets the classpath to include in the archive. The given {@code classpath} is + * evaluated as per {@link Project#files(Object...)}. + * @param classpath the classpath + * @since 2.0.7 + */ + void setClasspath(Object classpath); + + /** + * Sets the classpath to include in the archive. + * @param classpath the classpath + * @since 2.0.7 + */ + void setClasspath(FileCollection classpath); + /** * Returns {@code true} if the Devtools jar should be excluded, otherwise * {@code false}. diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java index 9e26f4e6aa33..76ce216c2a1e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java @@ -129,6 +129,14 @@ public void classpath(Object... classpath) { classpath); } + public void setClasspath(Object classpath) { + this.classpath = getProject().files(classpath); + } + + public void setClasspath(FileCollection classpath) { + this.classpath = getProject().files(classpath); + } + @Override public boolean isExcludeDevtools() { return this.support.isExcludeDevtools(); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootWar.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootWar.java index fec816a0ca19..d97c995f2b6e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootWar.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootWar.java @@ -120,7 +120,7 @@ public FileCollection getProvidedClasspath() { /** * Adds files to the provided classpath to include in the {@code WEB-INF/lib-provided} - * directory of the war. The given {@code classpath} are evaluated as per + * directory of the war. The given {@code classpath} is evaluated as per * {@link Project#files(Object...)}. * @param classpath the additions to the classpath */ @@ -131,6 +131,27 @@ public void providedClasspath(Object... classpath) { classpath); } + /** + * Sets the provided classpath to include in the {@code WEB-INF/lib-provided} + * directory of the war. + * @param classpath the classpath + * @since 2.0.7 + */ + public void setProvidedClasspath(FileCollection classpath) { + this.providedClasspath = getProject().files(classpath); + } + + /** + * Sets the provided classpath to include in the {@code WEB-INF/lib-provided} + * directory of the war. The given {@code classpath} is evaluated as per + * {@link Project#files(Object...)}. + * @param classpath the classpath + * @since 2.0.7 + */ + public void setProvidedClasspath(Object classpath) { + this.providedClasspath = getProject().files(classpath); + } + @Override public boolean isExcludeDevtools() { return this.support.isExcludeDevtools(); 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 7b4682a3e6e3..6461014cd0df 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 @@ -133,6 +133,31 @@ public void classpathFoldersArePackagedBeneathClassesPath() throws IOException { } } + @Test + public void classpathCanBeSetUsingAFileCollection() throws IOException { + this.task.setMainClassName("com.example.Main"); + this.task.classpath(this.temp.newFile("one.jar")); + this.task + .setClasspath(this.task.getProject().files(this.temp.newFile("two.jar"))); + this.task.execute(); + try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { + assertThat(jarFile.getEntry(this.libPath + "/one.jar")).isNull(); + assertThat(jarFile.getEntry(this.libPath + "/two.jar")).isNotNull(); + } + } + + @Test + 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(); + try (JarFile jarFile = new JarFile(this.task.getArchivePath())) { + assertThat(jarFile.getEntry(this.libPath + "/one.jar")).isNull(); + assertThat(jarFile.getEntry(this.libPath + "/two.jar")).isNotNull(); + } + } + @Test public void loaderIsWrittenToTheRootOfTheJar() throws IOException { this.task.setMainClassName("com.example.Main"); 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 bc97dbfabf0e..bafd44408480 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 @@ -48,6 +48,31 @@ public void providedClasspathJarsArePackagedInWebInfLibProvided() throws IOExcep } } + @Test + public void providedClasspathCanBeSetUsingAFileCollection() throws IOException { + getTask().setMainClassName("com.example.Main"); + getTask().providedClasspath(this.temp.newFile("one.jar")); + getTask().setProvidedClasspath( + getTask().getProject().files(this.temp.newFile("two.jar"))); + getTask().execute(); + 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(); + } + } + + @Test + 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(); + 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(); + } + } + @Test public void devtoolsJarIsExcludedByDefaultWhenItsOnTheProvidedClasspath() throws IOException {