Merge branch '2.0.x' into 2.1.x

Closes gh-17078
This commit is contained in:
Andy Wilkinson
2019-06-07 10:50:34 +01:00
2691 changed files with 27746 additions and 46049 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -58,12 +58,11 @@ public final class BuildPropertiesWriter {
}
File parent = file.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IllegalStateException("Cannot create parent directory for '"
+ this.outputFile.getAbsolutePath() + "'");
throw new IllegalStateException(
"Cannot create parent directory for '" + this.outputFile.getAbsolutePath() + "'");
}
if (!file.createNewFile()) {
throw new IllegalStateException("Cannot create target file '"
+ this.outputFile.getAbsolutePath() + "'");
throw new IllegalStateException("Cannot create target file '" + this.outputFile.getAbsolutePath() + "'");
}
}
@@ -74,12 +73,10 @@ public final class BuildPropertiesWriter {
properties.put("build.name", project.getName());
properties.put("build.version", project.getVersion());
if (project.getTime() != null) {
properties.put("build.time",
DateTimeFormatter.ISO_INSTANT.format(project.getTime()));
properties.put("build.time", DateTimeFormatter.ISO_INSTANT.format(project.getTime()));
}
if (project.getAdditionalProperties() != null) {
project.getAdditionalProperties()
.forEach((name, value) -> properties.put("build." + name, value));
project.getAdditionalProperties().forEach((name, value) -> properties.put("build." + name, value));
}
return properties;
}
@@ -101,8 +98,8 @@ public final class BuildPropertiesWriter {
private final Map<String, String> additionalProperties;
public ProjectDetails(String group, String artifact, String version, String name,
Instant time, Map<String, String> additionalProperties) {
public ProjectDetails(String group, String artifact, String version, String name, Instant time,
Map<String, String> additionalProperties) {
this.group = group;
this.artifact = artifact;
this.name = name;
@@ -112,8 +109,7 @@ public final class BuildPropertiesWriter {
this.additionalProperties = additionalProperties;
}
private static void validateAdditionalProperties(
Map<String, String> additionalProperties) {
private static void validateAdditionalProperties(Map<String, String> additionalProperties) {
if (additionalProperties != null) {
additionalProperties.forEach((name, value) -> {
if (value == null) {
@@ -152,8 +148,7 @@ public final class BuildPropertiesWriter {
/**
* Exception thrown when an additional property with a null value is encountered.
*/
public static class NullAdditionalPropertyValueException
extends IllegalArgumentException {
public static class NullAdditionalPropertyValueException extends IllegalArgumentException {
public NullAdditionalPropertyValueException(String name) {
super("Additional property '" + name + "' is illegal as its value is null");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -42,8 +42,7 @@ public class DefaultLaunchScript implements LaunchScript {
private static final int BUFFER_SIZE = 4096;
private static final Pattern PLACEHOLDER_PATTERN = Pattern
.compile("\\{\\{(\\w+)(:.*?)?\\}\\}(?!\\})");
private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\{\\{(\\w+)(:.*?)?\\}\\}(?!\\})");
private static final Set<String> FILE_PATH_KEYS = Collections
.unmodifiableSet(Collections.singleton("inlinedConfScript"));
@@ -79,8 +78,7 @@ public class DefaultLaunchScript implements LaunchScript {
}
}
private void copy(InputStream inputStream, OutputStream outputStream)
throws IOException {
private void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
@@ -89,8 +87,7 @@ public class DefaultLaunchScript implements LaunchScript {
outputStream.flush();
}
private String expandPlaceholders(String content, Map<?, ?> properties)
throws IOException {
private String expandPlaceholders(String content, Map<?, ?> properties) throws IOException {
StringBuffer expanded = new StringBuffer();
Matcher matcher = PLACEHOLDER_PATTERN.matcher(content);
while (matcher.find()) {
@@ -107,8 +104,7 @@ public class DefaultLaunchScript implements LaunchScript {
}
}
else {
value = (defaultValue != null) ? defaultValue.substring(1)
: matcher.group(0);
value = (defaultValue != null) ? defaultValue.substring(1) : matcher.group(0);
}
matcher.appendReplacement(expanded, value.replace("$", "\\$"));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -38,8 +38,7 @@ public abstract class FileUtils {
* @param outputDirectory the output directory
* @param originDirectory the origin directory
*/
public static void removeDuplicatesFromOutputDirectory(File outputDirectory,
File originDirectory) {
public static void removeDuplicatesFromOutputDirectory(File outputDirectory, File originDirectory) {
if (originDirectory.isDirectory()) {
for (String name : originDirectory.list()) {
File targetFile = new File(outputDirectory, name);
@@ -48,8 +47,7 @@ public abstract class FileUtils {
targetFile.delete();
}
else {
FileUtils.removeDuplicatesFromOutputDirectory(targetFile,
new File(originDirectory, name));
FileUtils.removeDuplicatesFromOutputDirectory(targetFile, new File(originDirectory, name));
}
}
}
@@ -64,8 +62,8 @@ public abstract class FileUtils {
*/
public static String sha1Hash(File file) throws IOException {
try {
try (DigestInputStream inputStream = new DigestInputStream(
new FileInputStream(file), MessageDigest.getInstance("SHA-1"))) {
try (DigestInputStream inputStream = new DigestInputStream(new FileInputStream(file),
MessageDigest.getInstance("SHA-1"))) {
byte[] buffer = new byte[4098];
while (inputStream.read(buffer) != -1) {
// Read the entire stream

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -82,8 +82,7 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable {
* @throws IOException if the file cannot be opened
* @throws FileNotFoundException if the file cannot be found
*/
public JarWriter(File file, LaunchScript launchScript)
throws FileNotFoundException, IOException {
public JarWriter(File file, LaunchScript launchScript) throws FileNotFoundException, IOException {
FileOutputStream fileOutputStream = new FileOutputStream(file);
if (launchScript != null) {
fileOutputStream.write(launchScript.toByteArray());
@@ -96,8 +95,7 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable {
private void setExecutableFilePermission(File file) {
try {
Path path = file.toPath();
Set<PosixFilePermission> permissions = new HashSet<>(
Files.getPosixFilePermissions(path));
Set<PosixFilePermission> permissions = new HashSet<>(Files.getPosixFilePermissions(path));
permissions.add(PosixFilePermission.OWNER_EXECUTE);
Files.setPosixFilePermissions(path, permissions);
}
@@ -129,14 +127,13 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable {
this.writeEntries(jarFile, new IdentityEntryTransformer(), unpackHandler);
}
void writeEntries(JarFile jarFile, EntryTransformer entryTransformer,
UnpackHandler unpackHandler) throws IOException {
void writeEntries(JarFile jarFile, EntryTransformer entryTransformer, UnpackHandler unpackHandler)
throws IOException {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarArchiveEntry entry = new JarArchiveEntry(entries.nextElement());
setUpEntry(jarFile, entry);
try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream(
jarFile.getInputStream(entry))) {
try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream(jarFile.getInputStream(entry))) {
EntryWriter entryWriter = new InputStreamEntryWriter(inputStream, true);
JarArchiveEntry transformedEntry = entryTransformer.transform(entry);
if (transformedEntry != null) {
@@ -147,8 +144,7 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable {
}
private void setUpEntry(JarFile jarFile, JarArchiveEntry entry) throws IOException {
try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream(
jarFile.getInputStream(entry))) {
try (ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream(jarFile.getInputStream(entry))) {
if (inputStream.hasZipHeader() && entry.getMethod() != ZipEntry.STORED) {
new CrcAndSize(inputStream).setupStoredEntry(entry);
}
@@ -176,8 +172,7 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable {
* @param library the library
* @throws IOException if the write fails
*/
public void writeNestedLibrary(String destination, Library library)
throws IOException {
public void writeNestedLibrary(String destination, Library library) throws IOException {
File file = library.getFile();
JarArchiveEntry entry = new JarArchiveEntry(destination + library.getName());
entry.setTime(getNestedLibraryTime(file));
@@ -222,13 +217,11 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable {
@Override
public void writeLoaderClasses(String loaderJarResourceName) throws IOException {
URL loaderJar = getClass().getClassLoader().getResource(loaderJarResourceName);
try (JarInputStream inputStream = new JarInputStream(
new BufferedInputStream(loaderJar.openStream()))) {
try (JarInputStream inputStream = new JarInputStream(new BufferedInputStream(loaderJar.openStream()))) {
JarEntry entry;
while ((entry = inputStream.getNextJarEntry()) != null) {
if (entry.getName().endsWith(".class")) {
writeEntry(new JarArchiveEntry(entry),
new InputStreamEntryWriter(inputStream, false));
writeEntry(new JarArchiveEntry(entry), new InputStreamEntryWriter(inputStream, false));
}
}
}
@@ -243,8 +236,7 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable {
this.jarOutput.close();
}
private void writeEntry(JarArchiveEntry entry, EntryWriter entryWriter)
throws IOException {
private void writeEntry(JarArchiveEntry entry, EntryWriter entryWriter) throws IOException {
writeEntry(entry, entryWriter, NEVER_UNPACK);
}
@@ -256,8 +248,8 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable {
* @param unpackHandler handles possible unpacking for the entry
* @throws IOException in case of I/O errors
*/
private void writeEntry(JarArchiveEntry entry, EntryWriter entryWriter,
UnpackHandler unpackHandler) throws IOException {
private void writeEntry(JarArchiveEntry entry, EntryWriter entryWriter, UnpackHandler unpackHandler)
throws IOException {
String parent = entry.getName();
if (parent.endsWith("/")) {
parent = parent.substring(0, parent.length() - 1);
@@ -283,16 +275,15 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable {
}
}
private EntryWriter addUnpackCommentIfNecessary(JarArchiveEntry entry,
EntryWriter entryWriter, UnpackHandler unpackHandler) throws IOException {
private EntryWriter addUnpackCommentIfNecessary(JarArchiveEntry entry, EntryWriter entryWriter,
UnpackHandler unpackHandler) throws IOException {
if (entryWriter == null || !unpackHandler.requiresUnpack(entry.getName())) {
return entryWriter;
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
entryWriter.write(output);
entry.setComment("UNPACK:" + unpackHandler.sha1Hash(entry.getName()));
return new InputStreamEntryWriter(new ByteArrayInputStream(output.toByteArray()),
true);
return new InputStreamEntryWriter(new ByteArrayInputStream(output.toByteArray()), true);
}
/**
@@ -357,8 +348,7 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable {
super(in);
this.header = new byte[4];
this.headerLength = in.read(this.header);
this.headerStream = new ByteArrayInputStream(this.header, 0,
this.headerLength);
this.headerStream = new ByteArrayInputStream(this.header, 0, this.headerLength);
}
@Override
@@ -381,8 +371,7 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable {
@Override
public int read(byte[] b, int off, int len) throws IOException {
int read = (this.headerStream != null) ? this.headerStream.read(b, off, len)
: -1;
int read = (this.headerStream != null) ? this.headerStream.read(b, off, len) : -1;
if (read <= 0) {
return readRemainder(b, off, len);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -35,8 +35,7 @@ public class JavaExecutable {
public JavaExecutable() {
String javaHome = System.getProperty("java.home");
Assert.state(StringUtils.hasLength(javaHome),
"Unable to find java executable due to missing 'java.home'");
Assert.state(StringUtils.hasLength(javaHome), "Unable to find java executable due to missing 'java.home'");
this.file = findInJavaHome(javaHome);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -57,8 +57,7 @@ public abstract class MainClassFinder {
private static final Type STRING_ARRAY_TYPE = Type.getType(String[].class);
private static final Type MAIN_METHOD_TYPE = Type.getMethodType(Type.VOID_TYPE,
STRING_ARRAY_TYPE);
private static final Type MAIN_METHOD_TYPE = Type.getMethodType(Type.VOID_TYPE, STRING_ARRAY_TYPE);
private static final String MAIN_METHOD_NAME = "main";
@@ -104,8 +103,7 @@ public abstract class MainClassFinder {
* @return the main class or {@code null}
* @throws IOException if the folder cannot be read
*/
public static String findSingleMainClass(File rootFolder, String annotationName)
throws IOException {
public static String findSingleMainClass(File rootFolder, String annotationName) throws IOException {
SingleMainClassCallback callback = new SingleMainClassCallback(annotationName);
MainClassFinder.doWithMainClasses(rootFolder, callback);
return callback.getMainClassName();
@@ -120,14 +118,12 @@ public abstract class MainClassFinder {
* @return the first callback result or {@code null}
* @throws IOException in case of I/O errors
*/
static <T> T doWithMainClasses(File rootFolder, MainClassCallback<T> callback)
throws IOException {
static <T> T doWithMainClasses(File rootFolder, MainClassCallback<T> callback) throws IOException {
if (!rootFolder.exists()) {
return null; // nothing to do
}
if (!rootFolder.isDirectory()) {
throw new IllegalArgumentException(
"Invalid root folder '" + rootFolder + "'");
throw new IllegalArgumentException("Invalid root folder '" + rootFolder + "'");
}
String prefix = rootFolder.getAbsolutePath() + "/";
Deque<File> stack = new ArrayDeque<>();
@@ -138,10 +134,8 @@ public abstract class MainClassFinder {
try (InputStream inputStream = new FileInputStream(file)) {
ClassDescriptor classDescriptor = createClassDescriptor(inputStream);
if (classDescriptor != null && classDescriptor.isMainMethodFound()) {
String className = convertToClassName(file.getAbsolutePath(),
prefix);
T result = callback.doWith(new MainClass(className,
classDescriptor.getAnnotationNames()));
String className = convertToClassName(file.getAbsolutePath(), prefix);
T result = callback.doWith(new MainClass(className, classDescriptor.getAnnotationNames()));
if (result != null) {
return result;
}
@@ -170,8 +164,7 @@ public abstract class MainClassFinder {
* @return the main class or {@code null}
* @throws IOException if the jar file cannot be read
*/
public static String findMainClass(JarFile jarFile, String classesLocation)
throws IOException {
public static String findMainClass(JarFile jarFile, String classesLocation) throws IOException {
return doWithMainClasses(jarFile, classesLocation, MainClass::getName);
}
@@ -182,8 +175,7 @@ public abstract class MainClassFinder {
* @return the main class or {@code null}
* @throws IOException if the jar file cannot be read
*/
public static String findSingleMainClass(JarFile jarFile, String classesLocation)
throws IOException {
public static String findSingleMainClass(JarFile jarFile, String classesLocation) throws IOException {
return findSingleMainClass(jarFile, classesLocation, null);
}
@@ -198,8 +190,8 @@ public abstract class MainClassFinder {
* @return the main class or {@code null}
* @throws IOException if the jar file cannot be read
*/
public static String findSingleMainClass(JarFile jarFile, String classesLocation,
String annotationName) throws IOException {
public static String findSingleMainClass(JarFile jarFile, String classesLocation, String annotationName)
throws IOException {
SingleMainClassCallback callback = new SingleMainClassCallback(annotationName);
MainClassFinder.doWithMainClasses(jarFile, classesLocation, callback);
return callback.getMainClassName();
@@ -214,19 +206,16 @@ public abstract class MainClassFinder {
* @return the first callback result or {@code null}
* @throws IOException in case of I/O errors
*/
static <T> T doWithMainClasses(JarFile jarFile, String classesLocation,
MainClassCallback<T> callback) throws IOException {
static <T> T doWithMainClasses(JarFile jarFile, String classesLocation, MainClassCallback<T> callback)
throws IOException {
List<JarEntry> classEntries = getClassEntries(jarFile, classesLocation);
classEntries.sort(new ClassEntryComparator());
for (JarEntry entry : classEntries) {
try (InputStream inputStream = new BufferedInputStream(
jarFile.getInputStream(entry))) {
try (InputStream inputStream = new BufferedInputStream(jarFile.getInputStream(entry))) {
ClassDescriptor classDescriptor = createClassDescriptor(inputStream);
if (classDescriptor != null && classDescriptor.isMainMethodFound()) {
String className = convertToClassName(entry.getName(),
classesLocation);
T result = callback.doWith(new MainClass(className,
classDescriptor.getAnnotationNames()));
String className = convertToClassName(entry.getName(), classesLocation);
T result = callback.doWith(new MainClass(className, classDescriptor.getAnnotationNames()));
if (result != null) {
return result;
}
@@ -246,15 +235,13 @@ public abstract class MainClassFinder {
return name;
}
private static List<JarEntry> getClassEntries(JarFile source,
String classesLocation) {
private static List<JarEntry> getClassEntries(JarFile source, String classesLocation) {
classesLocation = (classesLocation != null) ? classesLocation : "";
Enumeration<JarEntry> sourceEntries = source.entries();
List<JarEntry> classEntries = new ArrayList<>();
while (sourceEntries.hasMoreElements()) {
JarEntry entry = sourceEntries.nextElement();
if (entry.getName().startsWith(classesLocation)
&& entry.getName().endsWith(DOT_CLASS)) {
if (entry.getName().startsWith(classesLocation) && entry.getName().endsWith(DOT_CLASS)) {
classEntries.add(entry);
}
}
@@ -309,10 +296,8 @@ public abstract class MainClassFinder {
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc,
String signature, String[] exceptions) {
if (isAccess(access, Opcodes.ACC_PUBLIC, Opcodes.ACC_STATIC)
&& MAIN_METHOD_NAME.equals(name)
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
if (isAccess(access, Opcodes.ACC_PUBLIC, Opcodes.ACC_STATIC) && MAIN_METHOD_NAME.equals(name)
&& MAIN_METHOD_TYPE.getDescriptor().equals(desc)) {
this.mainMethodFound = true;
}
@@ -372,8 +357,7 @@ public abstract class MainClassFinder {
*/
MainClass(String name, Set<String> annotationNames) {
this.name = name;
this.annotationNames = Collections
.unmodifiableSet(new HashSet<>(annotationNames));
this.annotationNames = Collections.unmodifiableSet(new HashSet<>(annotationNames));
}
String getName() {
@@ -418,8 +402,7 @@ public abstract class MainClassFinder {
* Find a single main class, throwing an {@link IllegalStateException} if multiple
* candidates exist.
*/
private static final class SingleMainClassCallback
implements MainClassCallback<Object> {
private static final class SingleMainClassCallback implements MainClassCallback<Object> {
private final Set<MainClass> mainClasses = new LinkedHashSet<>();
@@ -449,11 +432,9 @@ public abstract class MainClassFinder {
}
if (matchingMainClasses.size() > 1) {
throw new IllegalStateException(
"Unable to find a single main class from the following candidates "
+ matchingMainClasses);
"Unable to find a single main class from the following candidates " + matchingMainClasses);
}
return (matchingMainClasses.isEmpty() ? null
: matchingMainClasses.iterator().next().getName());
return (matchingMainClasses.isEmpty() ? null : matchingMainClasses.iterator().next().getName());
}
}

View File

@@ -84,8 +84,8 @@ public class Repackager {
throw new IllegalArgumentException("Source file must be provided");
}
if (!source.exists() || !source.isFile()) {
throw new IllegalArgumentException("Source must refer to an existing file, "
+ "got " + source.getAbsolutePath());
throw new IllegalArgumentException(
"Source must refer to an existing file, " + "got " + source.getAbsolutePath());
}
this.source = source.getAbsoluteFile();
this.layoutFactory = layoutFactory;
@@ -96,8 +96,7 @@ public class Repackager {
* main class takes too long.
* @param listener the listener to add
*/
public void addMainClassTimeoutWarningListener(
MainClassTimeoutWarningListener listener) {
public void addMainClassTimeoutWarningListener(MainClassTimeoutWarningListener listener) {
this.mainClassTimeoutListeners.add(listener);
}
@@ -168,8 +167,7 @@ public class Repackager {
* @throws IOException if the file cannot be repackaged
* @since 1.3.0
*/
public void repackage(File destination, Libraries libraries,
LaunchScript launchScript) throws IOException {
public void repackage(File destination, Libraries libraries, LaunchScript launchScript) throws IOException {
if (destination == null || destination.isDirectory()) {
throw new IllegalArgumentException("Invalid destination");
}
@@ -206,8 +204,7 @@ public class Repackager {
if (this.layoutFactory != null) {
return this.layoutFactory;
}
List<LayoutFactory> factories = SpringFactoriesLoader
.loadFactories(LayoutFactory.class, null);
List<LayoutFactory> factories = SpringFactoriesLoader.loadFactories(LayoutFactory.class, null);
if (factories.isEmpty()) {
return new DefaultLayoutFactory();
}
@@ -226,20 +223,19 @@ public class Repackager {
private boolean alreadyRepackaged() throws IOException {
try (JarFile jarFile = new JarFile(this.source)) {
Manifest manifest = jarFile.getManifest();
return (manifest != null && manifest.getMainAttributes()
.getValue(BOOT_VERSION_ATTRIBUTE) != null);
return (manifest != null && manifest.getMainAttributes().getValue(BOOT_VERSION_ATTRIBUTE) != null);
}
}
private void repackage(JarFile sourceJar, File destination, Libraries libraries,
LaunchScript launchScript) throws IOException {
private void repackage(JarFile sourceJar, File destination, Libraries libraries, LaunchScript launchScript)
throws IOException {
WritableLibraries writeableLibraries = new WritableLibraries(libraries);
try (JarWriter writer = new JarWriter(destination, launchScript)) {
writer.writeManifest(buildManifest(sourceJar));
writeLoaderClasses(writer);
if (this.layout instanceof RepackagingLayout) {
writer.writeEntries(sourceJar, new RenamingEntryTransformer(
((RepackagingLayout) this.layout).getRepackagedClassesLocation()),
writer.writeEntries(sourceJar,
new RenamingEntryTransformer(((RepackagingLayout) this.layout).getRepackagedClassesLocation()),
writeableLibraries);
}
else {
@@ -294,8 +290,7 @@ public class Repackager {
}
String launcherClassName = this.layout.getLauncherClassName();
if (launcherClassName != null) {
manifest.getMainAttributes().putValue(MAIN_CLASS_ATTRIBUTE,
launcherClassName);
manifest.getMainAttributes().putValue(MAIN_CLASS_ATTRIBUTE, launcherClassName);
if (startClass == null) {
throw new IllegalStateException("Unable to find main class");
}
@@ -306,10 +301,8 @@ public class Repackager {
}
String bootVersion = getClass().getPackage().getImplementationVersion();
manifest.getMainAttributes().putValue(BOOT_VERSION_ATTRIBUTE, bootVersion);
manifest.getMainAttributes().putValue(BOOT_CLASSES_ATTRIBUTE,
(this.layout instanceof RepackagingLayout)
? ((RepackagingLayout) this.layout).getRepackagedClassesLocation()
: this.layout.getClassesLocation());
manifest.getMainAttributes().putValue(BOOT_CLASSES_ATTRIBUTE, (this.layout instanceof RepackagingLayout)
? ((RepackagingLayout) this.layout).getRepackagedClassesLocation() : this.layout.getClassesLocation());
String lib = this.layout.getLibraryDestination("", LibraryScope.COMPILE);
if (StringUtils.hasLength(lib)) {
manifest.getMainAttributes().putValue(BOOT_LIB_ATTRIBUTE, lib);
@@ -330,14 +323,13 @@ public class Repackager {
}
protected String findMainMethod(JarFile source) throws IOException {
return MainClassFinder.findSingleMainClass(source,
this.layout.getClassesLocation(), SPRING_BOOT_APPLICATION_CLASS_NAME);
return MainClassFinder.findSingleMainClass(source, this.layout.getClassesLocation(),
SPRING_BOOT_APPLICATION_CLASS_NAME);
}
private void renameFile(File file, File dest) {
if (!file.renameTo(dest)) {
throw new IllegalStateException(
"Unable to rename '" + file + "' to '" + dest + "'");
throw new IllegalStateException("Unable to rename '" + file + "' to '" + dest + "'");
}
}
@@ -379,15 +371,12 @@ public class Repackager {
if (entry.getName().equals("META-INF/INDEX.LIST")) {
return null;
}
if ((entry.getName().startsWith("META-INF/")
&& !entry.getName().equals("META-INF/aop.xml")
&& !entry.getName().endsWith(".kotlin_module"))
|| entry.getName().startsWith("BOOT-INF/")
if ((entry.getName().startsWith("META-INF/") && !entry.getName().equals("META-INF/aop.xml")
&& !entry.getName().endsWith(".kotlin_module")) || entry.getName().startsWith("BOOT-INF/")
|| entry.getName().equals("module-info.class")) {
return entry;
}
JarArchiveEntry renamedEntry = new JarArchiveEntry(
this.namePrefix + entry.getName());
JarArchiveEntry renamedEntry = new JarArchiveEntry(this.namePrefix + entry.getName());
renamedEntry.setTime(entry.getTime());
renamedEntry.setSize(entry.getSize());
renamedEntry.setMethod(entry.getMethod());
@@ -424,14 +413,13 @@ public class Repackager {
private WritableLibraries(Libraries libraries) throws IOException {
libraries.doWithLibraries((library) -> {
if (isZip(library.getFile())) {
String libraryDestination = Repackager.this.layout
.getLibraryDestination(library.getName(), library.getScope());
String libraryDestination = Repackager.this.layout.getLibraryDestination(library.getName(),
library.getScope());
if (libraryDestination != null) {
Library existing = this.libraryEntryNames.putIfAbsent(
libraryDestination + library.getName(), library);
Library existing = this.libraryEntryNames.putIfAbsent(libraryDestination + library.getName(),
library);
if (existing != null) {
throw new IllegalStateException(
"Duplicate library " + library.getName());
throw new IllegalStateException("Duplicate library " + library.getName());
}
}
}
@@ -448,16 +436,14 @@ public class Repackager {
public String sha1Hash(String name) throws IOException {
Library library = this.libraryEntryNames.get(name);
if (library == null) {
throw new IllegalArgumentException(
"No library found for entry name '" + name + "'");
throw new IllegalArgumentException("No library found for entry name '" + name + "'");
}
return FileUtils.sha1Hash(library.getFile());
}
private void write(JarWriter writer) throws IOException {
for (Entry<String, Library> entry : this.libraryEntryNames.entrySet()) {
writer.writeNestedLibrary(
entry.getKey().substring(0, entry.getKey().lastIndexOf('/') + 1),
writer.writeNestedLibrary(entry.getKey().substring(0, entry.getKey().lastIndexOf('/') + 1),
entry.getValue());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -69,8 +69,8 @@ public class RunProcess {
return run(waitForProcess, Arrays.asList(args), Collections.emptyMap());
}
public int run(boolean waitForProcess, Collection<String> args,
Map<String, String> environmentVariables) throws IOException {
public int run(boolean waitForProcess, Collection<String> args, Map<String, String> environmentVariables)
throws IOException {
ProcessBuilder builder = new ProcessBuilder(this.command);
builder.directory(this.workingDirectory);
builder.command().addAll(args);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -170,8 +170,7 @@ public class DefaultLaunchScriptTests {
public void expandVariables() throws Exception {
File file = this.temporaryFolder.newFile();
FileCopyUtils.copy("h{{a}}ll{{b}}".getBytes(), file);
DefaultLaunchScript script = new DefaultLaunchScript(file,
createProperties("a:e", "b:o"));
DefaultLaunchScript script = new DefaultLaunchScript(file, createProperties("a:e", "b:o"));
String content = new String(script.toByteArray());
assertThat(content).isEqualTo("hello");
}
@@ -180,8 +179,7 @@ public class DefaultLaunchScriptTests {
public void expandVariablesMultiLine() throws Exception {
File file = this.temporaryFolder.newFile();
FileCopyUtils.copy("h{{a}}l\nl{{b}}".getBytes(), file);
DefaultLaunchScript script = new DefaultLaunchScript(file,
createProperties("a:e", "b:o"));
DefaultLaunchScript script = new DefaultLaunchScript(file, createProperties("a:e", "b:o"));
String content = new String(script.toByteArray());
assertThat(content).isEqualTo("hel\nlo");
}
@@ -208,8 +206,7 @@ public class DefaultLaunchScriptTests {
public void expandVariablesWithDefaultsOverride() throws Exception {
File file = this.temporaryFolder.newFile();
FileCopyUtils.copy("h{{a:e}}ll{{b:o}}".getBytes(), file);
DefaultLaunchScript script = new DefaultLaunchScript(file,
createProperties("a:a"));
DefaultLaunchScript script = new DefaultLaunchScript(file, createProperties("a:a"));
String content = new String(script.toByteArray());
assertThat(content).isEqualTo("hallo");
}
@@ -224,8 +221,7 @@ public class DefaultLaunchScriptTests {
}
private void assertThatPlaceholderCanBeReplaced(String placeholder) throws Exception {
DefaultLaunchScript script = new DefaultLaunchScript(null,
createProperties(placeholder + ":__test__"));
DefaultLaunchScript script = new DefaultLaunchScript(null, createProperties(placeholder + ":__test__"));
String content = new String(script.toByteArray());
assertThat(content).contains("__test__");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@@ -60,8 +60,7 @@ public class FileUtilsTests {
File file = new File(this.outputDirectory, "logback.xml");
file.createNewFile();
new File(this.originDirectory, "logback.xml").createNewFile();
FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory,
this.originDirectory);
FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory, this.originDirectory);
assertThat(file.exists()).isFalse();
}
@@ -72,8 +71,7 @@ public class FileUtilsTests {
File file = new File(this.outputDirectory, "sub/logback.xml");
file.createNewFile();
new File(this.originDirectory, "sub/logback.xml").createNewFile();
FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory,
this.originDirectory);
FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory, this.originDirectory);
assertThat(file.exists()).isFalse();
}
@@ -84,8 +82,7 @@ public class FileUtilsTests {
File file = new File(this.outputDirectory, "sub/logback.xml");
file.createNewFile();
new File(this.originDirectory, "sub/different.xml").createNewFile();
FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory,
this.originDirectory);
FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory, this.originDirectory);
assertThat(file.exists()).isTrue();
}
@@ -94,8 +91,7 @@ public class FileUtilsTests {
File file = new File(this.outputDirectory, "logback.xml");
file.createNewFile();
new File(this.originDirectory, "different.xml").createNewFile();
FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory,
this.originDirectory);
FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory, this.originDirectory);
assertThat(file.exists()).isTrue();
}
@@ -105,8 +101,7 @@ public class FileUtilsTests {
try (OutputStream outputStream = new FileOutputStream(file)) {
outputStream.write(new byte[] { 1, 2, 3 });
}
assertThat(FileUtils.sha1Hash(file))
.isEqualTo("7037807198c22a7d2b0807371d763779a84fdfcf");
assertThat(FileUtils.sha1Hash(file)).isEqualTo("7037807198c22a7d2b0807371d763779a84fdfcf");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -36,8 +36,7 @@ public class LayoutsTests {
assertThat(Layouts.forFile(new File("test.jar"))).isInstanceOf(Layouts.Jar.class);
assertThat(Layouts.forFile(new File("test.JAR"))).isInstanceOf(Layouts.Jar.class);
assertThat(Layouts.forFile(new File("test.jAr"))).isInstanceOf(Layouts.Jar.class);
assertThat(Layouts.forFile(new File("te.st.jar")))
.isInstanceOf(Layouts.Jar.class);
assertThat(Layouts.forFile(new File("te.st.jar"))).isInstanceOf(Layouts.Jar.class);
}
@Test
@@ -45,41 +44,31 @@ public class LayoutsTests {
assertThat(Layouts.forFile(new File("test.war"))).isInstanceOf(Layouts.War.class);
assertThat(Layouts.forFile(new File("test.WAR"))).isInstanceOf(Layouts.War.class);
assertThat(Layouts.forFile(new File("test.wAr"))).isInstanceOf(Layouts.War.class);
assertThat(Layouts.forFile(new File("te.st.war")))
.isInstanceOf(Layouts.War.class);
assertThat(Layouts.forFile(new File("te.st.war"))).isInstanceOf(Layouts.War.class);
}
@Test
public void unknownFile() {
assertThatIllegalStateException()
.isThrownBy(() -> Layouts.forFile(new File("test.txt")))
assertThatIllegalStateException().isThrownBy(() -> Layouts.forFile(new File("test.txt")))
.withMessageContaining("Unable to deduce layout for 'test.txt'");
}
@Test
public void jarLayout() {
Layout layout = new Layouts.Jar();
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.COMPILE))
.isEqualTo("BOOT-INF/lib/");
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.CUSTOM))
.isEqualTo("BOOT-INF/lib/");
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.PROVIDED))
.isEqualTo("BOOT-INF/lib/");
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.RUNTIME))
.isEqualTo("BOOT-INF/lib/");
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.COMPILE)).isEqualTo("BOOT-INF/lib/");
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.CUSTOM)).isEqualTo("BOOT-INF/lib/");
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.PROVIDED)).isEqualTo("BOOT-INF/lib/");
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.RUNTIME)).isEqualTo("BOOT-INF/lib/");
}
@Test
public void warLayout() {
Layout layout = new Layouts.War();
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.COMPILE))
.isEqualTo("WEB-INF/lib/");
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.CUSTOM))
.isEqualTo("WEB-INF/lib/");
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.PROVIDED))
.isEqualTo("WEB-INF/lib-provided/");
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.RUNTIME))
.isEqualTo("WEB-INF/lib/");
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.COMPILE)).isEqualTo("WEB-INF/lib/");
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.CUSTOM)).isEqualTo("WEB-INF/lib/");
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.PROVIDED)).isEqualTo("WEB-INF/lib-provided/");
assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.RUNTIME)).isEqualTo("WEB-INF/lib/");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -81,18 +81,16 @@ public class MainClassFinderTests {
this.testJarFile.addClass("a/B.class", ClassWithMainMethod.class);
this.testJarFile.addClass("a/b/c/E.class", ClassWithMainMethod.class);
assertThatIllegalStateException()
.isThrownBy(() -> MainClassFinder
.findSingleMainClass(this.testJarFile.getJarFile(), ""))
.withMessageContaining("Unable to find a single main class "
+ "from the following candidates [a.B, a.b.c.E]");
.isThrownBy(() -> MainClassFinder.findSingleMainClass(this.testJarFile.getJarFile(), ""))
.withMessageContaining(
"Unable to find a single main class " + "from the following candidates [a.B, a.b.c.E]");
}
@Test
public void findSingleJarSearchPrefersAnnotatedMainClass() throws Exception {
this.testJarFile.addClass("a/B.class", ClassWithMainMethod.class);
this.testJarFile.addClass("a/b/c/E.class", AnnotatedClassWithMainMethod.class);
String mainClass = MainClassFinder.findSingleMainClass(
this.testJarFile.getJarFile(), "",
String mainClass = MainClassFinder.findSingleMainClass(this.testJarFile.getJarFile(), "",
"org.springframework.boot.loader.tools.sample.SomeApplication");
assertThat(mainClass).isEqualTo("a.b.c.E");
}
@@ -101,8 +99,7 @@ public class MainClassFinderTests {
public void findMainClassInJarSubLocation() throws Exception {
this.testJarFile.addClass("a/B.class", ClassWithMainMethod.class);
this.testJarFile.addClass("a/b/c/E.class", ClassWithMainMethod.class);
String actual = MainClassFinder.findMainClass(this.testJarFile.getJarFile(),
"a/");
String actual = MainClassFinder.findMainClass(this.testJarFile.getJarFile(), "a/");
assertThat(actual).isEqualTo("B");
}
@@ -137,18 +134,16 @@ public class MainClassFinderTests {
this.testJarFile.addClass("a/B.class", ClassWithMainMethod.class);
this.testJarFile.addClass("a/b/c/E.class", ClassWithMainMethod.class);
assertThatIllegalStateException()
.isThrownBy(() -> MainClassFinder
.findSingleMainClass(this.testJarFile.getJarSource()))
.withMessageContaining("Unable to find a single main class "
+ "from the following candidates [a.B, a.b.c.E]");
.isThrownBy(() -> MainClassFinder.findSingleMainClass(this.testJarFile.getJarSource()))
.withMessageContaining(
"Unable to find a single main class " + "from the following candidates [a.B, a.b.c.E]");
}
@Test
public void findSingleFolderSearchPrefersAnnotatedMainClass() throws Exception {
this.testJarFile.addClass("a/B.class", ClassWithMainMethod.class);
this.testJarFile.addClass("a/b/c/E.class", AnnotatedClassWithMainMethod.class);
String mainClass = MainClassFinder.findSingleMainClass(
this.testJarFile.getJarSource(),
String mainClass = MainClassFinder.findSingleMainClass(this.testJarFile.getJarSource(),
"org.springframework.boot.loader.tools.sample.SomeApplication");
assertThat(mainClass).isEqualTo("a.b.c.E");
}

View File

@@ -96,14 +96,12 @@ public class RepackagerTests {
@Test
public void missingSource() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new Repackager(new File("missing")));
assertThatIllegalArgumentException().isThrownBy(() -> new Repackager(new File("missing")));
}
@Test
public void directorySource() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new Repackager(this.temporaryFolder.getRoot()));
assertThatIllegalArgumentException().isThrownBy(() -> new Repackager(this.temporaryFolder.getRoot()));
}
@Test
@@ -116,8 +114,7 @@ public class RepackagerTests {
Manifest actualManifest = getManifest(file);
assertThat(actualManifest.getMainAttributes().getValue("Main-Class"))
.isEqualTo("org.springframework.boot.loader.JarLauncher");
assertThat(actualManifest.getMainAttributes().getValue("Start-Class"))
.isEqualTo("a.b.C");
assertThat(actualManifest.getMainAttributes().getValue("Start-Class")).isEqualTo("a.b.C");
assertThat(hasLauncherClasses(file)).isTrue();
}
@@ -134,8 +131,7 @@ public class RepackagerTests {
Manifest actualManifest = getManifest(file);
assertThat(actualManifest.getMainAttributes().getValue("Main-Class"))
.isEqualTo("org.springframework.boot.loader.JarLauncher");
assertThat(actualManifest.getMainAttributes().getValue("Start-Class"))
.isEqualTo("a.b.C");
assertThat(actualManifest.getMainAttributes().getValue("Start-Class")).isEqualTo("a.b.C");
assertThat(hasLauncherClasses(file)).isTrue();
}
@@ -148,8 +144,7 @@ public class RepackagerTests {
Manifest actualManifest = getManifest(file);
assertThat(actualManifest.getMainAttributes().getValue("Main-Class"))
.isEqualTo("org.springframework.boot.loader.JarLauncher");
assertThat(actualManifest.getMainAttributes().getValue("Start-Class"))
.isEqualTo("a.b.C");
assertThat(actualManifest.getMainAttributes().getValue("Start-Class")).isEqualTo("a.b.C");
assertThat(hasLauncherClasses(file)).isTrue();
}
@@ -163,8 +158,7 @@ public class RepackagerTests {
Manifest actualManifest = getManifest(file);
assertThat(actualManifest.getMainAttributes().getValue("Main-Class"))
.isEqualTo("org.springframework.boot.loader.JarLauncher");
assertThat(actualManifest.getMainAttributes().getValue("Start-Class"))
.isEqualTo("a.b.C");
assertThat(actualManifest.getMainAttributes().getValue("Start-Class")).isEqualTo("a.b.C");
assertThat(hasLauncherClasses(file)).isTrue();
}
@@ -174,17 +168,15 @@ public class RepackagerTests {
this.testJarFile.addClass("a/b/D.class", ClassWithMainMethod.class);
File file = this.testJarFile.getFile();
Repackager repackager = new Repackager(file);
assertThatIllegalStateException()
.isThrownBy(() -> repackager.repackage(NO_LIBRARIES))
.withMessageContaining("Unable to find a single main class "
+ "from the following candidates [a.b.C, a.b.D]");
assertThatIllegalStateException().isThrownBy(() -> repackager.repackage(NO_LIBRARIES)).withMessageContaining(
"Unable to find a single main class " + "from the following candidates [a.b.C, a.b.D]");
}
@Test
public void noMainClass() throws Exception {
this.testJarFile.addClass("a/b/C.class", ClassWithoutMainMethod.class);
assertThatIllegalStateException().isThrownBy(
() -> new Repackager(this.testJarFile.getFile()).repackage(NO_LIBRARIES))
assertThatIllegalStateException()
.isThrownBy(() -> new Repackager(this.testJarFile.getFile()).repackage(NO_LIBRARIES))
.withMessageContaining("Unable to find main class");
}
@@ -196,8 +188,7 @@ public class RepackagerTests {
repackager.setLayout(new Layouts.None());
repackager.repackage(file, NO_LIBRARIES);
Manifest actualManifest = getManifest(file);
assertThat(actualManifest.getMainAttributes().getValue("Main-Class"))
.isEqualTo("a.b.C");
assertThat(actualManifest.getMainAttributes().getValue("Main-Class")).isEqualTo("a.b.C");
assertThat(hasLauncherClasses(file)).isFalse();
}
@@ -230,8 +221,7 @@ public class RepackagerTests {
Repackager repackager = new Repackager(file);
repackager.setBackupSource(false);
repackager.repackage(NO_LIBRARIES);
assertThat(new File(file.getParent(), file.getName() + ".original"))
.doesNotExist();
assertThat(new File(file.getParent(), file.getName() + ".original")).doesNotExist();
assertThat(hasLauncherClasses(file)).isTrue();
}
@@ -242,8 +232,7 @@ public class RepackagerTests {
File dest = this.temporaryFolder.newFile("different.jar");
Repackager repackager = new Repackager(source);
repackager.repackage(dest, NO_LIBRARIES);
assertThat(new File(source.getParent(), source.getName() + ".original"))
.doesNotExist();
assertThat(new File(source.getParent(), source.getName() + ".original")).doesNotExist();
assertThat(hasLauncherClasses(source)).isFalse();
assertThat(hasLauncherClasses(dest)).isTrue();
}
@@ -252,8 +241,7 @@ public class RepackagerTests {
public void nullDestination() throws Exception {
this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
Repackager repackager = new Repackager(this.testJarFile.getFile());
assertThatIllegalArgumentException()
.isThrownBy(() -> repackager.repackage(null, NO_LIBRARIES))
assertThatIllegalArgumentException().isThrownBy(() -> repackager.repackage(null, NO_LIBRARIES))
.withMessageContaining("Invalid destination");
}
@@ -261,8 +249,8 @@ public class RepackagerTests {
public void destinationIsDirectory() throws Exception {
this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
Repackager repackager = new Repackager(this.testJarFile.getFile());
assertThatIllegalArgumentException().isThrownBy(
() -> repackager.repackage(this.temporaryFolder.getRoot(), NO_LIBRARIES))
assertThatIllegalArgumentException()
.isThrownBy(() -> repackager.repackage(this.temporaryFolder.getRoot(), NO_LIBRARIES))
.withMessageContaining("Invalid destination");
}
@@ -281,8 +269,7 @@ public class RepackagerTests {
this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
File file = this.testJarFile.getFile();
Repackager repackager = new Repackager(file);
assertThatIllegalArgumentException()
.isThrownBy(() -> repackager.repackage(file, null))
assertThatIllegalArgumentException().isThrownBy(() -> repackager.repackage(file, null))
.withMessageContaining("Libraries must not be null");
}
@@ -295,8 +282,7 @@ public class RepackagerTests {
File libNonJarFile = this.temporaryFolder.newFile();
FileCopyUtils.copy(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, libNonJarFile);
this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
this.testJarFile.addFile("BOOT-INF/lib/" + libJarFileToUnpack.getName(),
libJarFileToUnpack);
this.testJarFile.addFile("BOOT-INF/lib/" + libJarFileToUnpack.getName(), libJarFileToUnpack);
File file = this.testJarFile.getFile();
libJarFile.setLastModified(JAN_1_1980);
Repackager repackager = new Repackager(file);
@@ -306,8 +292,7 @@ public class RepackagerTests {
callback.library(new Library(libNonJarFile, LibraryScope.COMPILE));
});
assertThat(hasEntry(file, "BOOT-INF/lib/" + libJarFile.getName())).isTrue();
assertThat(hasEntry(file, "BOOT-INF/lib/" + libJarFileToUnpack.getName()))
.isTrue();
assertThat(hasEntry(file, "BOOT-INF/lib/" + libJarFileToUnpack.getName())).isTrue();
assertThat(hasEntry(file, "BOOT-INF/lib/" + libNonJarFile.getName())).isFalse();
JarEntry entry = getEntry(file, "BOOT-INF/lib/" + libJarFile.getName());
assertThat(entry.getTime()).isEqualTo(JAN_1_1985);
@@ -324,13 +309,10 @@ public class RepackagerTests {
this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
File file = this.testJarFile.getFile();
Repackager repackager = new Repackager(file);
assertThatIllegalStateException()
.isThrownBy(() -> repackager.repackage((callback) -> {
callback.library(
new Library(libJarFile, LibraryScope.COMPILE, false));
callback.library(
new Library(libJarFile, LibraryScope.COMPILE, false));
})).withMessageContaining("Duplicate library");
assertThatIllegalStateException().isThrownBy(() -> repackager.repackage((callback) -> {
callback.library(new Library(libJarFile, LibraryScope.COMPILE, false));
callback.library(new Library(libJarFile, LibraryScope.COMPILE, false));
})).withMessageContaining("Duplicate library");
}
@Test
@@ -345,16 +327,12 @@ public class RepackagerTests {
LibraryScope scope = mock(LibraryScope.class);
given(layout.getLauncherClassName()).willReturn("testLauncher");
given(layout.getLibraryDestination(anyString(), eq(scope))).willReturn("test/");
given(layout.getLibraryDestination(anyString(), eq(LibraryScope.COMPILE)))
.willReturn("test-lib/");
given(layout.getLibraryDestination(anyString(), eq(LibraryScope.COMPILE))).willReturn("test-lib/");
repackager.setLayout(layout);
repackager.repackage(
(callback) -> callback.library(new Library(libJarFile, scope)));
repackager.repackage((callback) -> callback.library(new Library(libJarFile, scope)));
assertThat(hasEntry(file, "test/" + libJarFile.getName())).isTrue();
assertThat(getManifest(file).getMainAttributes().getValue("Spring-Boot-Lib"))
.isEqualTo("test-lib/");
assertThat(getManifest(file).getMainAttributes().getValue("Main-Class"))
.isEqualTo("testLauncher");
assertThat(getManifest(file).getMainAttributes().getValue("Spring-Boot-Lib")).isEqualTo("test-lib/");
assertThat(getManifest(file).getMainAttributes().getValue("Main-Class")).isEqualTo("testLauncher");
}
@Test
@@ -369,12 +347,9 @@ public class RepackagerTests {
LibraryScope scope = mock(LibraryScope.class);
given(layout.getLauncherClassName()).willReturn("testLauncher");
repackager.setLayout(layout);
repackager.repackage(
(callback) -> callback.library(new Library(libJarFile, scope)));
assertThat(getManifest(file).getMainAttributes().getValue("Spring-Boot-Lib"))
.isNull();
assertThat(getManifest(file).getMainAttributes().getValue("Main-Class"))
.isEqualTo("testLauncher");
repackager.repackage((callback) -> callback.library(new Library(libJarFile, scope)));
assertThat(getManifest(file).getMainAttributes().getValue("Spring-Boot-Lib")).isNull();
assertThat(getManifest(file).getMainAttributes().getValue("Main-Class")).isEqualTo("testLauncher");
}
@Test
@@ -384,8 +359,7 @@ public class RepackagerTests {
Repackager repackager = new Repackager(file);
repackager.repackage(NO_LIBRARIES);
Manifest actualManifest = getManifest(file);
assertThat(actualManifest.getMainAttributes())
.containsKey(new Attributes.Name("Spring-Boot-Version"));
assertThat(actualManifest.getMainAttributes()).containsKey(new Attributes.Name("Spring-Boot-Version"));
}
@Test
@@ -395,24 +369,23 @@ public class RepackagerTests {
Repackager repackager = new Repackager(file);
repackager.repackage(NO_LIBRARIES);
Manifest actualManifest = getManifest(file);
assertThat(actualManifest.getMainAttributes())
.containsEntry(new Attributes.Name("Spring-Boot-Lib"), "BOOT-INF/lib/");
assertThat(actualManifest.getMainAttributes()).containsEntry(
new Attributes.Name("Spring-Boot-Classes"), "BOOT-INF/classes/");
assertThat(actualManifest.getMainAttributes()).containsEntry(new Attributes.Name("Spring-Boot-Lib"),
"BOOT-INF/lib/");
assertThat(actualManifest.getMainAttributes()).containsEntry(new Attributes.Name("Spring-Boot-Classes"),
"BOOT-INF/classes/");
}
@Test
public void executableWarLayoutAttributes() throws Exception {
this.testJarFile.addClass("WEB-INF/classes/a/b/C.class",
ClassWithMainMethod.class);
this.testJarFile.addClass("WEB-INF/classes/a/b/C.class", ClassWithMainMethod.class);
File file = this.testJarFile.getFile("war");
Repackager repackager = new Repackager(file);
repackager.repackage(NO_LIBRARIES);
Manifest actualManifest = getManifest(file);
assertThat(actualManifest.getMainAttributes())
.containsEntry(new Attributes.Name("Spring-Boot-Lib"), "WEB-INF/lib/");
assertThat(actualManifest.getMainAttributes()).containsEntry(
new Attributes.Name("Spring-Boot-Classes"), "WEB-INF/classes/");
assertThat(actualManifest.getMainAttributes()).containsEntry(new Attributes.Name("Spring-Boot-Lib"),
"WEB-INF/lib/");
assertThat(actualManifest.getMainAttributes()).containsEntry(new Attributes.Name("Spring-Boot-Classes"),
"WEB-INF/classes/");
}
@Test
@@ -432,15 +405,11 @@ public class RepackagerTests {
this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
File file = this.testJarFile.getFile();
Repackager repackager = new Repackager(file);
repackager.repackage((callback) -> callback
.library(new Library(nestedFile, LibraryScope.COMPILE)));
repackager.repackage((callback) -> callback.library(new Library(nestedFile, LibraryScope.COMPILE)));
try (JarFile jarFile = new JarFile(file)) {
assertThat(
jarFile.getEntry("BOOT-INF/lib/" + nestedFile.getName()).getMethod())
.isEqualTo(ZipEntry.STORED);
assertThat(jarFile.getEntry("BOOT-INF/classes/test/nested.jar").getMethod())
.isEqualTo(ZipEntry.STORED);
assertThat(jarFile.getEntry("BOOT-INF/lib/" + nestedFile.getName()).getMethod()).isEqualTo(ZipEntry.STORED);
assertThat(jarFile.getEntry("BOOT-INF/classes/test/nested.jar").getMethod()).isEqualTo(ZipEntry.STORED);
}
}
@@ -457,8 +426,7 @@ public class RepackagerTests {
assertThat(hasLauncherClasses(source)).isFalse();
assertThat(hasLauncherClasses(dest)).isTrue();
try {
assertThat(Files.getPosixFilePermissions(dest.toPath()))
.contains(PosixFilePermission.OWNER_EXECUTE);
assertThat(Files.getPosixFilePermissions(dest.toPath())).contains(PosixFilePermission.OWNER_EXECUTE);
}
catch (UnsupportedOperationException ex) {
// Probably running the test on Windows
@@ -466,8 +434,7 @@ public class RepackagerTests {
}
@Test
public void unpackLibrariesTakePrecedenceOverExistingSourceEntries()
throws Exception {
public void unpackLibrariesTakePrecedenceOverExistingSourceEntries() throws Exception {
TestJarFile nested = new TestJarFile(this.temporaryFolder);
nested.addClass("a/b/C.class", ClassWithoutMainMethod.class);
File nestedFile = nested.getFile();
@@ -476,21 +443,18 @@ public class RepackagerTests {
this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
File file = this.testJarFile.getFile();
Repackager repackager = new Repackager(file);
repackager.repackage((callback) -> callback
.library(new Library(nestedFile, LibraryScope.COMPILE, true)));
repackager.repackage((callback) -> callback.library(new Library(nestedFile, LibraryScope.COMPILE, true)));
try (JarFile jarFile = new JarFile(file)) {
assertThat(jarFile.getEntry(name).getComment()).startsWith("UNPACK:");
}
}
@Test
public void existingSourceEntriesTakePrecedenceOverStandardLibraries()
throws Exception {
public void existingSourceEntriesTakePrecedenceOverStandardLibraries() throws Exception {
TestJarFile nested = new TestJarFile(this.temporaryFolder);
nested.addClass("a/b/C.class", ClassWithoutMainMethod.class);
File nestedFile = nested.getFile();
this.testJarFile.addFile("BOOT-INF/lib/" + nestedFile.getName(),
nested.getFile());
this.testJarFile.addFile("BOOT-INF/lib/" + nestedFile.getName(), nested.getFile());
this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
File file = this.testJarFile.getFile();
Repackager repackager = new Repackager(file);
@@ -502,16 +466,14 @@ public class RepackagerTests {
callback.library(new Library(nestedFile, LibraryScope.COMPILE));
});
try (JarFile jarFile = new JarFile(file)) {
assertThat(jarFile.getEntry("BOOT-INF/lib/" + nestedFile.getName()).getSize())
.isEqualTo(sourceLength);
assertThat(jarFile.getEntry("BOOT-INF/lib/" + nestedFile.getName()).getSize()).isEqualTo(sourceLength);
}
}
@Test
public void metaInfIndexListIsRemovedFromRepackagedJar() throws Exception {
this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
this.testJarFile.addFile("META-INF/INDEX.LIST",
this.temporaryFolder.newFile("INDEX.LIST"));
this.testJarFile.addFile("META-INF/INDEX.LIST", this.temporaryFolder.newFile("INDEX.LIST"));
File source = this.testJarFile.getFile();
File dest = this.temporaryFolder.newFile("dest.jar");
Repackager repackager = new Repackager(source);
@@ -545,11 +507,9 @@ public class RepackagerTests {
}
@Test
public void metaInfAopXmlIsMovedBeneathBootInfClassesWhenRepackaged()
throws Exception {
public void metaInfAopXmlIsMovedBeneathBootInfClassesWhenRepackaged() throws Exception {
this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
this.testJarFile.addFile("META-INF/aop.xml",
this.temporaryFolder.newFile("aop.xml"));
this.testJarFile.addFile("META-INF/aop.xml", this.temporaryFolder.newFile("aop.xml"));
File source = this.testJarFile.getFile();
File dest = this.temporaryFolder.newFile("dest.jar");
Repackager repackager = new Repackager(source);
@@ -578,10 +538,8 @@ public class RepackagerTests {
}
@Test
public void loaderIsWrittenFirstThenApplicationClassesThenLibraries()
throws IOException {
this.testJarFile.addClass("com/example/Application.class",
ClassWithMainMethod.class);
public void loaderIsWrittenFirstThenApplicationClassesThenLibraries() throws IOException {
this.testJarFile.addClass("com/example/Application.class", ClassWithMainMethod.class);
File source = this.testJarFile.getFile();
File dest = this.temporaryFolder.newFile("dest.jar");
File libraryOne = createLibrary();
@@ -593,31 +551,23 @@ public class RepackagerTests {
callback.library(new Library(libraryTwo, LibraryScope.COMPILE, true));
callback.library(new Library(libraryThree, LibraryScope.COMPILE, false));
});
assertThat(getEntryNames(dest)).containsSubsequence(
"org/springframework/boot/loader/",
"BOOT-INF/classes/com/example/Application.class",
"BOOT-INF/lib/" + libraryOne.getName(),
"BOOT-INF/lib/" + libraryTwo.getName(),
"BOOT-INF/lib/" + libraryThree.getName());
assertThat(getEntryNames(dest)).containsSubsequence("org/springframework/boot/loader/",
"BOOT-INF/classes/com/example/Application.class", "BOOT-INF/lib/" + libraryOne.getName(),
"BOOT-INF/lib/" + libraryTwo.getName(), "BOOT-INF/lib/" + libraryThree.getName());
}
@Test
public void existingEntryThatMatchesUnpackLibraryIsMarkedForUnpack()
throws IOException {
public void existingEntryThatMatchesUnpackLibraryIsMarkedForUnpack() throws IOException {
File library = createLibrary();
this.testJarFile.addClass("WEB-INF/classes/com/example/Application.class",
ClassWithMainMethod.class);
this.testJarFile.addClass("WEB-INF/classes/com/example/Application.class", ClassWithMainMethod.class);
this.testJarFile.addFile("WEB-INF/lib/" + library.getName(), library);
File source = this.testJarFile.getFile("war");
File dest = this.temporaryFolder.newFile("dest.war");
Repackager repackager = new Repackager(source);
repackager.setLayout(new Layouts.War());
repackager.repackage(dest, (callback) -> callback
.library(new Library(library, LibraryScope.COMPILE, true)));
assertThat(getEntryNames(dest)).containsSubsequence(
"org/springframework/boot/loader/",
"WEB-INF/classes/com/example/Application.class",
"WEB-INF/lib/" + library.getName());
repackager.repackage(dest, (callback) -> callback.library(new Library(library, LibraryScope.COMPILE, true)));
assertThat(getEntryNames(dest)).containsSubsequence("org/springframework/boot/loader/",
"WEB-INF/classes/com/example/Application.class", "WEB-INF/lib/" + library.getName());
JarEntry unpackLibrary = getEntry(dest, "WEB-INF/lib/" + library.getName());
assertThat(unpackLibrary.getComment()).startsWith("UNPACK:");
}
@@ -633,15 +583,13 @@ public class RepackagerTests {
Layout layout = mock(Layout.class);
LibraryScope scope = mock(LibraryScope.class);
repackager.setLayout(layout);
repackager.repackage(
(callback) -> callback.library(new Library(libJarFile, scope)));
assertThat(getEntryNames(file)).containsExactly("META-INF/",
"META-INF/MANIFEST.MF", "a/", "a/b/", "a/b/C.class");
repackager.repackage((callback) -> callback.library(new Library(libJarFile, scope)));
assertThat(getEntryNames(file)).containsExactly("META-INF/", "META-INF/MANIFEST.MF", "a/", "a/b/",
"a/b/C.class");
}
@Test
public void jarThatUsesCustomCompressionConfigurationCanBeRepackaged()
throws IOException {
public void jarThatUsesCustomCompressionConfigurationCanBeRepackaged() throws IOException {
File source = this.temporaryFolder.newFile("source.jar");
ZipOutputStream output = new ZipOutputStream(new FileOutputStream(source)) {
{
@@ -676,26 +624,22 @@ public class RepackagerTests {
}
@Test
public void kotlinModuleMetadataMovesBeneathBootInfClassesWhenRepackaged()
throws Exception {
public void kotlinModuleMetadataMovesBeneathBootInfClassesWhenRepackaged() throws Exception {
this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
this.testJarFile.addFile("META-INF/test.kotlin_module",
this.temporaryFolder.newFile("test.kotlin_module"));
this.testJarFile.addFile("META-INF/test.kotlin_module", this.temporaryFolder.newFile("test.kotlin_module"));
File source = this.testJarFile.getFile();
File dest = this.temporaryFolder.newFile("dest.jar");
Repackager repackager = new Repackager(source);
repackager.repackage(dest, NO_LIBRARIES);
try (JarFile jarFile = new JarFile(dest)) {
assertThat(jarFile.getEntry("META-INF/test.kotlin_module")).isNull();
assertThat(jarFile.getEntry("BOOT-INF/classes/META-INF/test.kotlin_module"))
.isNotNull();
assertThat(jarFile.getEntry("BOOT-INF/classes/META-INF/test.kotlin_module")).isNotNull();
}
}
private File createLibrary() throws IOException {
TestJarFile library = new TestJarFile(this.temporaryFolder);
library.addClass("com/example/library/Library.class",
ClassWithoutMainMethod.class);
library.addClass("com/example/library/Library.class", ClassWithoutMainMethod.class);
return library.getFile();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -55,12 +55,11 @@ public class TestJarFile {
addClass(filename, classToCopy, null);
}
public void addClass(String filename, Class<?> classToCopy, Long time)
throws IOException {
public void addClass(String filename, Class<?> classToCopy, Long time) throws IOException {
File file = getFilePath(filename);
file.getParentFile().mkdirs();
InputStream inputStream = getClass().getResourceAsStream(
"/" + classToCopy.getName().replace('.', '/') + ".class");
InputStream inputStream = getClass()
.getResourceAsStream("/" + classToCopy.getName().replace('.', '/') + ".class");
copyToFile(inputStream, file);
if (time != null) {
file.setLastModified(time);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -33,8 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class ZipHeaderPeekInputStreamTests {
@Test
public void hasZipHeaderReturnsTrueWhenStreamStartsWithZipHeader()
throws IOException {
public void hasZipHeaderReturnsTrueWhenStreamStartsWithZipHeader() throws IOException {
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(
new ByteArrayInputStream(new byte[] { 0x50, 0x4b, 0x03, 0x04, 5, 6 }))) {
assertThat(in.hasZipHeader()).isTrue();
@@ -42,8 +41,7 @@ public class ZipHeaderPeekInputStreamTests {
}
@Test
public void hasZipHeaderReturnsFalseWhenStreamDoesNotStartWithZipHeader()
throws IOException {
public void hasZipHeaderReturnsFalseWhenStreamDoesNotStartWithZipHeader() throws IOException {
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(
new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }))) {
assertThat(in.hasZipHeader()).isFalse();
@@ -88,8 +86,7 @@ public class ZipHeaderPeekInputStreamTests {
}
@Test
public void readOfSomeOfTheHeaderThenMoreThanEntireStreamReadsToEndOfStream()
throws IOException {
public void readOfSomeOfTheHeaderThenMoreThanEntireStreamReadsToEndOfStream() throws IOException {
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(
new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }))) {
byte[] bytes = new byte[8];
@@ -101,10 +98,8 @@ public class ZipHeaderPeekInputStreamTests {
}
@Test
public void readMoreThanEntireStreamWhenStreamLengthIsLessThanZipHeaderLength()
throws IOException {
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(
new ByteArrayInputStream(new byte[] { 10 }))) {
public void readMoreThanEntireStreamWhenStreamLengthIsLessThanZipHeaderLength() throws IOException {
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(new ByteArrayInputStream(new byte[] { 10 }))) {
byte[] bytes = new byte[8];
assertThat(in.read(bytes)).isEqualTo(1);
assertThat(bytes).containsExactly(10, 0, 0, 0, 0, 0, 0, 0);
@@ -112,8 +107,7 @@ public class ZipHeaderPeekInputStreamTests {
}
@Test
public void readMoreThanEntireStreamWhenStreamLengthIsSameAsHeaderLength()
throws IOException {
public void readMoreThanEntireStreamWhenStreamLengthIsSameAsHeaderLength() throws IOException {
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(
new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }))) {
byte[] bytes = new byte[8];
@@ -124,8 +118,7 @@ public class ZipHeaderPeekInputStreamTests {
@Test
public void readMoreThanEntireStreamWhenStreamLengthIsZero() throws IOException {
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(
new ByteArrayInputStream(new byte[0]))) {
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(new ByteArrayInputStream(new byte[0]))) {
byte[] bytes = new byte[8];
assertThat(in.read(bytes)).isEqualTo(-1);
assertThat(bytes).containsExactly(0, 0, 0, 0, 0, 0, 0, 0);