Skip to content

Commit ce0a3d0

Browse files
committed
Add missing classpath setters to BootJar and BootWar
Closes gh-15087
1 parent fd6dea7 commit ce0a3d0

File tree

5 files changed

+96
-2
lines changed

5 files changed

+96
-2
lines changed

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchive.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,26 @@ public interface BootArchive extends Task {
9393

9494
/**
9595
* Adds files to the classpath to include in the archive. The given {@code classpath}
96-
* are evaluated as per {@link Project#files(Object...)}.
96+
* is evaluated as per {@link Project#files(Object...)}.
9797
* @param classpath the additions to the classpath
9898
*/
9999
void classpath(Object... classpath);
100100

101+
/**
102+
* Sets the classpath to include in the archive. The given {@code classpath} is
103+
* evaluated as per {@link Project#files(Object...)}.
104+
* @param classpath the classpath
105+
* @since 2.0.7
106+
*/
107+
void setClasspath(Object classpath);
108+
109+
/**
110+
* Sets the classpath to include in the archive.
111+
* @param classpath the classpath
112+
* @since 2.0.7
113+
*/
114+
void setClasspath(FileCollection classpath);
115+
101116
/**
102117
* Returns {@code true} if the Devtools jar should be excluded, otherwise
103118
* {@code false}.

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ public void classpath(Object... classpath) {
129129
classpath);
130130
}
131131

132+
public void setClasspath(Object classpath) {
133+
this.classpath = getProject().files(classpath);
134+
}
135+
136+
public void setClasspath(FileCollection classpath) {
137+
this.classpath = getProject().files(classpath);
138+
}
139+
132140
@Override
133141
public boolean isExcludeDevtools() {
134142
return this.support.isExcludeDevtools();

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootWar.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public FileCollection getProvidedClasspath() {
120120

121121
/**
122122
* Adds files to the provided classpath to include in the {@code WEB-INF/lib-provided}
123-
* directory of the war. The given {@code classpath} are evaluated as per
123+
* directory of the war. The given {@code classpath} is evaluated as per
124124
* {@link Project#files(Object...)}.
125125
* @param classpath the additions to the classpath
126126
*/
@@ -131,6 +131,27 @@ public void providedClasspath(Object... classpath) {
131131
classpath);
132132
}
133133

134+
/**
135+
* Sets the provided classpath to include in the {@code WEB-INF/lib-provided}
136+
* directory of the war.
137+
* @param classpath the classpath
138+
* @since 2.0.7
139+
*/
140+
public void setProvidedClasspath(FileCollection classpath) {
141+
this.providedClasspath = getProject().files(classpath);
142+
}
143+
144+
/**
145+
* Sets the provided classpath to include in the {@code WEB-INF/lib-provided}
146+
* directory of the war. The given {@code classpath} is evaluated as per
147+
* {@link Project#files(Object...)}.
148+
* @param classpath the classpath
149+
* @since 2.0.7
150+
*/
151+
public void setProvidedClasspath(Object classpath) {
152+
this.providedClasspath = getProject().files(classpath);
153+
}
154+
134155
@Override
135156
public boolean isExcludeDevtools() {
136157
return this.support.isExcludeDevtools();

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,31 @@ public void classpathFoldersArePackagedBeneathClassesPath() throws IOException {
128128
}
129129
}
130130

131+
@Test
132+
public void classpathCanBeSetUsingAFileCollection() throws IOException {
133+
this.task.setMainClassName("com.example.Main");
134+
this.task.classpath(this.temp.newFile("one.jar"));
135+
this.task
136+
.setClasspath(this.task.getProject().files(this.temp.newFile("two.jar")));
137+
this.task.execute();
138+
try (JarFile jarFile = new JarFile(this.task.getArchivePath())) {
139+
assertThat(jarFile.getEntry(this.libPath + "/one.jar")).isNull();
140+
assertThat(jarFile.getEntry(this.libPath + "/two.jar")).isNotNull();
141+
}
142+
}
143+
144+
@Test
145+
public void classpathCanBeSetUsingAnObject() throws IOException {
146+
this.task.setMainClassName("com.example.Main");
147+
this.task.classpath(this.temp.newFile("one.jar"));
148+
this.task.setClasspath(this.temp.newFile("two.jar"));
149+
this.task.execute();
150+
try (JarFile jarFile = new JarFile(this.task.getArchivePath())) {
151+
assertThat(jarFile.getEntry(this.libPath + "/one.jar")).isNull();
152+
assertThat(jarFile.getEntry(this.libPath + "/two.jar")).isNotNull();
153+
}
154+
}
155+
131156
@Test
132157
public void loaderIsWrittenToTheRootOfTheJar() throws IOException {
133158
this.task.setMainClassName("com.example.Main");

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootWarTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,31 @@ public void providedClasspathJarsArePackagedInWebInfLibProvided() throws IOExcep
4848
}
4949
}
5050

51+
@Test
52+
public void providedClasspathCanBeSetUsingAFileCollection() throws IOException {
53+
getTask().setMainClassName("com.example.Main");
54+
getTask().providedClasspath(this.temp.newFile("one.jar"));
55+
getTask().setProvidedClasspath(
56+
getTask().getProject().files(this.temp.newFile("two.jar")));
57+
getTask().execute();
58+
try (JarFile jarFile = new JarFile(getTask().getArchivePath())) {
59+
assertThat(jarFile.getEntry("WEB-INF/lib-provided/one.jar")).isNull();
60+
assertThat(jarFile.getEntry("WEB-INF/lib-provided/two.jar")).isNotNull();
61+
}
62+
}
63+
64+
@Test
65+
public void providedClasspathCanBeSetUsingAnObject() throws IOException {
66+
getTask().setMainClassName("com.example.Main");
67+
getTask().providedClasspath(this.temp.newFile("one.jar"));
68+
getTask().setProvidedClasspath(this.temp.newFile("two.jar"));
69+
getTask().execute();
70+
try (JarFile jarFile = new JarFile(getTask().getArchivePath())) {
71+
assertThat(jarFile.getEntry("WEB-INF/lib-provided/one.jar")).isNull();
72+
assertThat(jarFile.getEntry("WEB-INF/lib-provided/two.jar")).isNotNull();
73+
}
74+
}
75+
5176
@Test
5277
public void devtoolsJarIsExcludedByDefaultWhenItsOnTheProvidedClasspath()
5378
throws IOException {

0 commit comments

Comments
 (0)