From 7913d9b599b8ff8ad2d43d08c36577d0af99f9ae Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 11 May 2018 11:14:14 +0100 Subject: [PATCH] Add methods to BootJar for adding content to BOOT-INF Closes gh-13000 --- .../boot/gradle/tasks/bundling/BootJar.java | 34 +++++++++++++++++-- .../gradle/tasks/bundling/BootJarTests.java | 34 ++++++++++++++++++- 2 files changed, 65 insertions(+), 3 deletions(-) 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 2bfb0bb7ee..da15b24780 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 @@ -40,6 +40,8 @@ public class BootJar extends Jar implements BootArchive { private final BootArchiveSupport support = new BootArchiveSupport( "org.springframework.boot.loader.JarLauncher", this::resolveZipCompression); + private final CopySpec bootInf; + private FileCollection classpath; private String mainClassName; @@ -48,8 +50,10 @@ public class BootJar extends Jar implements BootArchive { * Creates a new {@code BootJar} task. */ public BootJar() { - into("BOOT-INF/classes", classpathFiles(File::isDirectory)); - into("BOOT-INF/lib", classpathFiles(File::isFile)); + this.bootInf = getProject().copySpec().into("BOOT-INF"); + getMainSpec().with(this.bootInf); + this.bootInf.into("classes", classpathFiles(File::isDirectory)); + this.bootInf.into("lib", classpathFiles(File::isFile)); } private Action classpathFiles(Spec filter) { @@ -128,6 +132,32 @@ public class BootJar extends Jar implements BootArchive { this.support.setExcludeDevtools(excludeDevtools); } + /** + * Returns a {@code CopySpec} that can be used to add content to the {@code BOOT-INF} + * directory of the jar. + * @return a {@code CopySpec} for {@code BOOT-INF} + * @since 2.0.3 + */ + public CopySpec getBootInf() { + CopySpec child = getProject().copySpec(); + this.bootInf.with(child); + return child; + } + + /** + * Calls the given {@code action} to add content to the {@code BOOT-INF} directory of + * the jar. + * @param action the {@code Action} to call + * @return the {@code CopySpec} for {@code BOOT-INF} that was passed to the + * {@code Action} + * @since 2.0.3 + */ + public CopySpec bootInf(Action action) { + CopySpec bootInf = getBootInf(); + action.execute(bootInf); + return bootInf; + } + /** * Returns the {@link ZipCompression} that should be used when adding the file * represented by the given {@code details} to the jar. 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 1dda05c1c1..08568d9a6c 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,14 @@ package org.springframework.boot.gradle.tasks.bundling; +import java.io.File; +import java.io.IOException; +import java.util.jar.JarFile; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + /** * Tests for {@link BootJar}. * @@ -28,4 +36,28 @@ public class BootJarTests extends AbstractBootArchiveTests { "BOOT-INF/lib", "BOOT-INF/classes"); } + @Test + public void contentCanBeAddedToBootInfUsingCopySpecFromGetter() throws IOException { + BootJar bootJar = getTask(); + bootJar.setMainClassName("com.example.Application"); + bootJar.getBootInf().into("test") + .from(new File("build.gradle").getAbsolutePath()); + bootJar.execute(); + try (JarFile jarFile = new JarFile(bootJar.getArchivePath())) { + assertThat(jarFile.getJarEntry("BOOT-INF/test/build.gradle")).isNotNull(); + } + } + + @Test + public void contentCanBeAddedToBootInfUsingCopySpecAction() throws IOException { + BootJar bootJar = getTask(); + bootJar.setMainClassName("com.example.Application"); + bootJar.bootInf((copySpec) -> copySpec.into("test") + .from(new File("build.gradle").getAbsolutePath())); + bootJar.execute(); + try (JarFile jarFile = new JarFile(bootJar.getArchivePath())) { + assertThat(jarFile.getJarEntry("BOOT-INF/test/build.gradle")).isNotNull(); + } + } + }