Add manifest entries for location of lib and classes in executable archives

Closes gh-5183
This commit is contained in:
Andy Wilkinson 2016-02-19 15:37:24 +00:00
parent 59e119eb9a
commit 5ed4ef1272
4 changed files with 46 additions and 2 deletions

View File

@ -74,6 +74,8 @@
<attribute name="Main-Class"
value="org.springframework.boot.loader.JarLauncher" />
<attribute name="Start-Class" value="${start-class}" />
<attribute name="Spring-Boot-Classes" value="BOOT-INF/classes/" />
<attribute name="Spring-Boot-Lib" value="BOOT-INF/lib/" />
</manifest>
</jar>
</sequential>

View File

@ -45,6 +45,10 @@ public class Repackager {
private static final String BOOT_VERSION_ATTRIBUTE = "Spring-Boot-Version";
private static final String BOOT_LIB_ATTRIBUTE = "Spring-Boot-Lib";
private static final String BOOT_CLASSES_ATTRIBUTE = "Spring-Boot-Classes";
private static final byte[] ZIP_FILE_HEADER = new byte[] { 'P', 'K', 3, 4 };
private String mainClass;
@ -282,6 +286,12 @@ 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_LIB_ATTRIBUTE,
this.layout.getLibraryDestination("", LibraryScope.COMPILE));
return manifest;
}

View File

@ -370,6 +370,33 @@ public class RepackagerTests {
.containsKey(new Attributes.Name("Spring-Boot-Version"));
}
@Test
public void executableJarLayoutAttributes() throws Exception {
this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
File file = this.testJarFile.getFile();
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/");
}
@Test
public void executableWarLayoutAttributes() throws Exception {
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/");
}
@Test
public void nullCustomLayout() throws Exception {
this.testJarFile.addClass("a/b/C.class", ClassWithoutMainMethod.class);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
@ -31,6 +31,7 @@ import org.zeroturnaround.zip.ZipUtil;
/**
* @author Phillip Webb
* @author Andy Wilkinson
*/
public class TestJarFile {
@ -121,8 +122,12 @@ public class TestJarFile {
}
public File getFile() throws IOException {
return getFile("jar");
}
public File getFile(String extension) throws IOException {
File file = this.temporaryFolder.newFile();
file = new File(file.getParent(), file.getName() + ".jar");
file = new File(file.getParent(), file.getName() + "." + extension);
ZipUtil.pack(this.jarSource, file);
return file;
}