Skip to content

Commit 7913d9b

Browse files
committed
Add methods to BootJar for adding content to BOOT-INF
Closes gh-13000
1 parent d9d7499 commit 7913d9b

File tree

2 files changed

+65
-3
lines changed
  • spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src

2 files changed

+65
-3
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: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class BootJar extends Jar implements BootArchive {
4040
private final BootArchiveSupport support = new BootArchiveSupport(
4141
"org.springframework.boot.loader.JarLauncher", this::resolveZipCompression);
4242

43+
private final CopySpec bootInf;
44+
4345
private FileCollection classpath;
4446

4547
private String mainClassName;
@@ -48,8 +50,10 @@ public class BootJar extends Jar implements BootArchive {
4850
* Creates a new {@code BootJar} task.
4951
*/
5052
public BootJar() {
51-
into("BOOT-INF/classes", classpathFiles(File::isDirectory));
52-
into("BOOT-INF/lib", classpathFiles(File::isFile));
53+
this.bootInf = getProject().copySpec().into("BOOT-INF");
54+
getMainSpec().with(this.bootInf);
55+
this.bootInf.into("classes", classpathFiles(File::isDirectory));
56+
this.bootInf.into("lib", classpathFiles(File::isFile));
5357
}
5458

5559
private Action<CopySpec> classpathFiles(Spec<File> filter) {
@@ -128,6 +132,32 @@ public void setExcludeDevtools(boolean excludeDevtools) {
128132
this.support.setExcludeDevtools(excludeDevtools);
129133
}
130134

135+
/**
136+
* Returns a {@code CopySpec} that can be used to add content to the {@code BOOT-INF}
137+
* directory of the jar.
138+
* @return a {@code CopySpec} for {@code BOOT-INF}
139+
* @since 2.0.3
140+
*/
141+
public CopySpec getBootInf() {
142+
CopySpec child = getProject().copySpec();
143+
this.bootInf.with(child);
144+
return child;
145+
}
146+
147+
/**
148+
* Calls the given {@code action} to add content to the {@code BOOT-INF} directory of
149+
* the jar.
150+
* @param action the {@code Action} to call
151+
* @return the {@code CopySpec} for {@code BOOT-INF} that was passed to the
152+
* {@code Action}
153+
* @since 2.0.3
154+
*/
155+
public CopySpec bootInf(Action<CopySpec> action) {
156+
CopySpec bootInf = getBootInf();
157+
action.execute(bootInf);
158+
return bootInf;
159+
}
160+
131161
/**
132162
* Returns the {@link ZipCompression} that should be used when adding the file
133163
* represented by the given {@code details} to the jar.

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,14 @@
1616

1717
package org.springframework.boot.gradle.tasks.bundling;
1818

19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.util.jar.JarFile;
22+
23+
import org.junit.Test;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
1927
/**
2028
* Tests for {@link BootJar}.
2129
*
@@ -28,4 +36,28 @@ public BootJarTests() {
2836
"BOOT-INF/lib", "BOOT-INF/classes");
2937
}
3038

39+
@Test
40+
public void contentCanBeAddedToBootInfUsingCopySpecFromGetter() throws IOException {
41+
BootJar bootJar = getTask();
42+
bootJar.setMainClassName("com.example.Application");
43+
bootJar.getBootInf().into("test")
44+
.from(new File("build.gradle").getAbsolutePath());
45+
bootJar.execute();
46+
try (JarFile jarFile = new JarFile(bootJar.getArchivePath())) {
47+
assertThat(jarFile.getJarEntry("BOOT-INF/test/build.gradle")).isNotNull();
48+
}
49+
}
50+
51+
@Test
52+
public void contentCanBeAddedToBootInfUsingCopySpecAction() throws IOException {
53+
BootJar bootJar = getTask();
54+
bootJar.setMainClassName("com.example.Application");
55+
bootJar.bootInf((copySpec) -> copySpec.into("test")
56+
.from(new File("build.gradle").getAbsolutePath()));
57+
bootJar.execute();
58+
try (JarFile jarFile = new JarFile(bootJar.getArchivePath())) {
59+
assertThat(jarFile.getJarEntry("BOOT-INF/test/build.gradle")).isNotNull();
60+
}
61+
}
62+
3163
}

0 commit comments

Comments
 (0)