Skip to content

Commit 01a0a54

Browse files
committed
Include layertools in layered jars build with Gradle
Closes gh-19867
1 parent 288f5ce commit 01a0a54

File tree

2 files changed

+18
-0
lines changed
  • spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src

2 files changed

+18
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import java.io.BufferedWriter;
2020
import java.io.File;
21+
import java.io.FileOutputStream;
2122
import java.io.IOException;
23+
import java.io.InputStream;
2224
import java.io.StringWriter;
2325
import java.util.ArrayList;
2426
import java.util.Collections;
@@ -43,6 +45,7 @@
4345
import org.springframework.boot.loader.tools.Layer;
4446
import org.springframework.boot.loader.tools.Layers;
4547
import org.springframework.boot.loader.tools.Library;
48+
import org.springframework.util.FileCopyUtils;
4649

4750
/**
4851
* A custom {@link Jar} task that produces a Spring Boot executable jar.
@@ -177,6 +180,15 @@ public void launchScript(Action<LaunchScriptConfiguration> action) {
177180
*/
178181
public void layered() {
179182
this.layers = Layers.IMPLICIT;
183+
this.bootInf.into("lib", (spec) -> spec.from((Callable<File>) () -> {
184+
String jarName = "spring-boot-jarmode-layertools.jar";
185+
InputStream stream = getClass().getClassLoader().getResourceAsStream("META-INF/jarmode/" + jarName);
186+
File taskTmp = new File(getProject().getBuildDir(), "tmp/" + getName());
187+
taskTmp.mkdirs();
188+
File layerToolsJar = new File(taskTmp, jarName);
189+
FileCopyUtils.copy(stream, new FileOutputStream(layerToolsJar));
190+
return layerToolsJar;
191+
}));
180192
this.bootInf.eachFile((details) -> {
181193
Layer layer = layerForFileDetails(details);
182194
if (layer != null) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ void whenJarIsLayeredClasspathIndexPointsToLayeredLibs() throws IOException {
123123
}
124124
}
125125

126+
@Test
127+
void whenJarIsLayeredThenLayerToolsAreAddedToTheJar() throws IOException {
128+
List<String> entryNames = getEntryNames(createLayeredJar());
129+
assertThat(entryNames).contains("BOOT-INF/layers/dependencies/lib/spring-boot-jarmode-layertools.jar");
130+
}
131+
126132
@Test
127133
void classpathIndexPointsToBootInfLibs() throws IOException {
128134
try (JarFile jarFile = new JarFile(createPopulatedJar())) {

0 commit comments

Comments
 (0)