Support for custom layout types to change loader classes
A layout can also optionally change the loader jar that is unpacked in the root of the repackaged archive by implementing a new method in Layout.
This commit is contained in:
@@ -53,8 +53,6 @@ import org.springframework.lang.UsesJava7;
|
||||
*/
|
||||
public class JarWriter {
|
||||
|
||||
private static final String NESTED_LOADER_JAR = "META-INF/loader/spring-boot-loader.jar";
|
||||
|
||||
private static final int BUFFER_SIZE = 32 * 1024;
|
||||
|
||||
private final JarOutputStream jarOutput;
|
||||
@@ -206,9 +204,20 @@ public class JarWriter {
|
||||
/**
|
||||
* Write the required spring-boot-loader classes to the JAR.
|
||||
* @throws IOException if the classes cannot be written
|
||||
* @deprecated us {@link #writeLoaderClasses(String)} instead
|
||||
*/
|
||||
@Deprecated
|
||||
public void writeLoaderClasses() throws IOException {
|
||||
URL loaderJar = getClass().getClassLoader().getResource(NESTED_LOADER_JAR);
|
||||
writeLoaderClasses(Layouts.DEFAULT_LOADER_JAR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the required spring-boot-loader classes to the JAR.
|
||||
* @param loaderJarPath the path to the loader jar (in the classpath)
|
||||
* @throws IOException if the classes cannot be written
|
||||
*/
|
||||
public void writeLoaderClasses(String loaderJarPath) throws IOException {
|
||||
URL loaderJar = getClass().getClassLoader().getResource(loaderJarPath);
|
||||
JarInputStream inputStream = new JarInputStream(
|
||||
new BufferedInputStream(loaderJar.openStream()));
|
||||
JarEntry entry;
|
||||
|
||||
@@ -40,15 +40,25 @@ public interface Layout {
|
||||
String getLibraryDestination(String libraryName, LibraryScope scope);
|
||||
|
||||
/**
|
||||
* Returns the location of classes within the archive.
|
||||
* Returns the location of classes within the archive. Empty if the location is the
|
||||
* root path, otherwise ends with a slash ('/').
|
||||
* @return the classes location
|
||||
*/
|
||||
String getClassesLocation();
|
||||
|
||||
/**
|
||||
* Returns if loader classes should be included to make the archive executable.
|
||||
* Returns if loader classes should be included to make the archive executable. If
|
||||
* true, then {@link #getLoaderJarPath()} should point to a valid jar file that
|
||||
* contains the loader classes.
|
||||
* @return if the layout is executable
|
||||
*/
|
||||
boolean isExecutable();
|
||||
|
||||
/**
|
||||
* Returns the path to a nested jar that contains the loader, and which will be
|
||||
* unpacked into the root of the repackaged jar.
|
||||
* @return the path to a nested jar that contains the loader
|
||||
*/
|
||||
String getLoaderJarPath();
|
||||
|
||||
}
|
||||
|
||||
@@ -18,14 +18,14 @@ package org.springframework.boot.loader.tools;
|
||||
|
||||
/**
|
||||
* Strategy for creating instances of {@link Layout}.
|
||||
*
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public interface LayoutFactory {
|
||||
|
||||
|
||||
Layout getLayout();
|
||||
|
||||
|
||||
String getName();
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
|
||||
/**
|
||||
* Archive layout types.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public enum LayoutType {
|
||||
|
||||
|
||||
@@ -33,6 +33,11 @@ import java.util.Set;
|
||||
*/
|
||||
public final class Layouts {
|
||||
|
||||
/**
|
||||
* Default value for {@link #getLoaderJarPath()}.
|
||||
*/
|
||||
public static final String DEFAULT_LOADER_JAR = "META-INF/loader/spring-boot-loader.jar";
|
||||
|
||||
private Layouts() {
|
||||
}
|
||||
|
||||
@@ -87,6 +92,11 @@ public final class Layouts {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLoaderJarPath() {
|
||||
return DEFAULT_LOADER_JAR;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,6 +126,11 @@ public final class Layouts {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLoaderJarPath() {
|
||||
return DEFAULT_LOADER_JAR;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -154,6 +169,11 @@ public final class Layouts {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLoaderJarPath() {
|
||||
return DEFAULT_LOADER_JAR;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,6 +208,11 @@ public final class Layouts {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLoaderJarPath() {
|
||||
return DEFAULT_LOADER_JAR;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -217,7 +217,7 @@ public class Repackager {
|
||||
}
|
||||
writeNestedLibraries(standardLibraries, seen, writer);
|
||||
if (this.layout.isExecutable()) {
|
||||
writer.writeLoaderClasses();
|
||||
writer.writeLoaderClasses(this.layout.getLoaderJarPath());
|
||||
}
|
||||
}
|
||||
finally {
|
||||
|
||||
@@ -28,14 +28,15 @@ public class LayoutTypeTests {
|
||||
|
||||
@Test
|
||||
public void standardType() {
|
||||
assertThat(LayoutType.layout("DIR")).isEqualTo(LayoutType.valueOf("DIR").layout());
|
||||
assertThat(LayoutType.layout("DIR"))
|
||||
.isEqualTo(LayoutType.valueOf("DIR").layout());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customType() {
|
||||
assertThat(LayoutType.layout("CUSTOM")).isNotNull();
|
||||
}
|
||||
|
||||
|
||||
public static class TestLayoutFactory implements LayoutFactory {
|
||||
|
||||
@Override
|
||||
@@ -46,6 +47,7 @@ public class LayoutTypeTests {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "CUSTOM";
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user