Commit 5ed4ef12 authored by Andy Wilkinson's avatar Andy Wilkinson

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

Closes gh-5183
parent 59e119eb
...@@ -74,6 +74,8 @@ ...@@ -74,6 +74,8 @@
<attribute name="Main-Class" <attribute name="Main-Class"
value="org.springframework.boot.loader.JarLauncher" /> value="org.springframework.boot.loader.JarLauncher" />
<attribute name="Start-Class" value="${start-class}" /> <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> </manifest>
</jar> </jar>
</sequential> </sequential>
......
...@@ -45,6 +45,10 @@ public class Repackager { ...@@ -45,6 +45,10 @@ public class Repackager {
private static final String BOOT_VERSION_ATTRIBUTE = "Spring-Boot-Version"; 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 static final byte[] ZIP_FILE_HEADER = new byte[] { 'P', 'K', 3, 4 };
private String mainClass; private String mainClass;
...@@ -282,6 +286,12 @@ public class Repackager { ...@@ -282,6 +286,12 @@ public class Repackager {
} }
String bootVersion = getClass().getPackage().getImplementationVersion(); String bootVersion = getClass().getPackage().getImplementationVersion();
manifest.getMainAttributes().putValue(BOOT_VERSION_ATTRIBUTE, bootVersion); 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; return manifest;
} }
......
...@@ -370,6 +370,33 @@ public class RepackagerTests { ...@@ -370,6 +370,33 @@ public class RepackagerTests {
.containsKey(new Attributes.Name("Spring-Boot-Version")); .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 @Test
public void nullCustomLayout() throws Exception { public void nullCustomLayout() throws Exception {
this.testJarFile.addClass("a/b/C.class", ClassWithoutMainMethod.class); this.testJarFile.addClass("a/b/C.class", ClassWithoutMainMethod.class);
......
/* /*
* 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"); * 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.
...@@ -31,6 +31,7 @@ import org.zeroturnaround.zip.ZipUtil; ...@@ -31,6 +31,7 @@ import org.zeroturnaround.zip.ZipUtil;
/** /**
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson
*/ */
public class TestJarFile { public class TestJarFile {
...@@ -121,8 +122,12 @@ public class TestJarFile { ...@@ -121,8 +122,12 @@ public class TestJarFile {
} }
public File getFile() throws IOException { public File getFile() throws IOException {
return getFile("jar");
}
public File getFile(String extension) throws IOException {
File file = this.temporaryFolder.newFile(); 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); ZipUtil.pack(this.jarSource, file);
return file; return file;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment