Add methods to BootJar for adding content to BOOT-INF
Closes gh-13000
This commit is contained in:
		
							parent
							
								
									d9d7499ae6
								
							
						
					
					
						commit
						7913d9b599
					
				| 
						 | 
					@ -40,6 +40,8 @@ public class BootJar extends Jar implements BootArchive {
 | 
				
			||||||
	private final BootArchiveSupport support = new BootArchiveSupport(
 | 
						private final BootArchiveSupport support = new BootArchiveSupport(
 | 
				
			||||||
			"org.springframework.boot.loader.JarLauncher", this::resolveZipCompression);
 | 
								"org.springframework.boot.loader.JarLauncher", this::resolveZipCompression);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private final CopySpec bootInf;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private FileCollection classpath;
 | 
						private FileCollection classpath;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private String mainClassName;
 | 
						private String mainClassName;
 | 
				
			||||||
| 
						 | 
					@ -48,8 +50,10 @@ public class BootJar extends Jar implements BootArchive {
 | 
				
			||||||
	 * Creates a new {@code BootJar} task.
 | 
						 * Creates a new {@code BootJar} task.
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public BootJar() {
 | 
						public BootJar() {
 | 
				
			||||||
		into("BOOT-INF/classes", classpathFiles(File::isDirectory));
 | 
							this.bootInf = getProject().copySpec().into("BOOT-INF");
 | 
				
			||||||
		into("BOOT-INF/lib", classpathFiles(File::isFile));
 | 
							getMainSpec().with(this.bootInf);
 | 
				
			||||||
 | 
							this.bootInf.into("classes", classpathFiles(File::isDirectory));
 | 
				
			||||||
 | 
							this.bootInf.into("lib", classpathFiles(File::isFile));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private Action<CopySpec> classpathFiles(Spec<File> filter) {
 | 
						private Action<CopySpec> classpathFiles(Spec<File> filter) {
 | 
				
			||||||
| 
						 | 
					@ -128,6 +132,32 @@ public class BootJar extends Jar implements BootArchive {
 | 
				
			||||||
		this.support.setExcludeDevtools(excludeDevtools);
 | 
							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<CopySpec> action) {
 | 
				
			||||||
 | 
							CopySpec bootInf = getBootInf();
 | 
				
			||||||
 | 
							action.execute(bootInf);
 | 
				
			||||||
 | 
							return bootInf;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Returns the {@link ZipCompression} that should be used when adding the file
 | 
						 * Returns the {@link ZipCompression} that should be used when adding the file
 | 
				
			||||||
	 * represented by the given {@code details} to the jar.
 | 
						 * represented by the given {@code details} to the jar.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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");
 | 
					 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||||
 * you may not use this file except in compliance with 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;
 | 
					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}.
 | 
					 * Tests for {@link BootJar}.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
| 
						 | 
					@ -28,4 +36,28 @@ public class BootJarTests extends AbstractBootArchiveTests<BootJar> {
 | 
				
			||||||
				"BOOT-INF/lib", "BOOT-INF/classes");
 | 
									"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();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue