From f761916b518e30fab61fb85ff01ac9784f82f6c1 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 4 Mar 2015 13:42:38 +0000 Subject: [PATCH] Honor unpack for war files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously repackaging of an archive was performed in three steps: 1. Write the manifest 2. Write entries from the source archive into the destination 3. Write any libraries into the destination if they’re not already there This worked fine for jar files, but not for war files. In the war file case the libraries are already in the source archive’s WEB-INF/lib directory so they’re copied into the destination in step 2. This means that step 3 largely becomes a no-op and, crucially, the UNPACK comment is not applied to any libraries that require it. This commit reorders steps 2 and 3 so that the libraries are copied into the destination first (allowing the UNPACK comment to be written, if required) and then any entries in the source are written into the destination if they’re not already there. Fixes gh-2588 --- .../boot/loader/tools/Repackager.java | 6 +- .../boot/loader/tools/RepackagerTests.java | 4 +- .../src/it/jar-with-unpack/verify.groovy | 4 +- .../src/it/war-with-unpack/pom.xml | 62 +++++++++++++++++++ .../main/java/org/test/SampleApplication.java | 24 +++++++ .../src/main/webapp/index.html | 1 + .../src/it/war-with-unpack/verify.groovy | 29 +++++++++ .../springframework/boot/maven/Verify.java | 17 ++++- 8 files changed, 138 insertions(+), 9 deletions(-) create mode 100644 spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/pom.xml create mode 100644 spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/src/main/java/org/test/SampleApplication.java create mode 100644 spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/src/main/webapp/index.html create mode 100644 spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/verify.groovy diff --git a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java index 72c583eb02..97da6ffc71 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -30,6 +30,7 @@ import java.util.jar.Manifest; * '{@literal java -jar}'. * * @author Phillip Webb + * @author Andy Wilkinson */ public class Repackager { @@ -157,7 +158,6 @@ public class Repackager { try { final Set seen = new HashSet(); writer.writeManifest(buildManifest(sourceJar)); - writer.writeEntries(sourceJar); libraries.doWithLibraries(new LibraryCallback() { @Override public void library(Library library) throws IOException { @@ -176,7 +176,7 @@ public class Repackager { } } }); - + writer.writeEntries(sourceJar); if (this.layout.isExecutable()) { writer.writeLoaderClasses(); } diff --git a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java index 377c7c44fb..a8a92de05e 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -280,6 +280,8 @@ public class RepackagerTests { final 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("lib/" + libJarFileToUnpack.getName(), + libJarFileToUnpack); File file = this.testJarFile.getFile(); Repackager repackager = new Repackager(file); repackager.repackage(new Libraries() { diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-with-unpack/verify.groovy b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-with-unpack/verify.groovy index 9246765312..f16e86e741 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-with-unpack/verify.groovy +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/jar-with-unpack/verify.groovy @@ -22,7 +22,7 @@ new Verify.JarArchiveVerification(f, Verify.SAMPLE_APP) { @Override protected void verifyZipEntries(Verify.ArchiveVerifier verifier) throws Exception { super.verifyZipEntries(verifier) - verifier.hasUnpackEntry("lib/spring-core-") - verifier.hasNonUnpackEntry("lib/spring-context-") + verifier.assertHasUnpackEntry("lib/spring-core-") + verifier.assertHasNonUnpackEntry("lib/spring-context-") } }.verify(); diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/pom.xml b/spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/pom.xml new file mode 100644 index 0000000000..02d0aa838e --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + org.springframework.boot.maven.it + war + 0.0.1.BUILD-SNAPSHOT + war + + UTF-8 + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + repackage + + + + + org.springframework + spring-core + + + + + + + + org.apache.maven.plugins + maven-war-plugin + 2.3 + + false + + + Foo + + + + + + + + + org.springframework + spring-context + @spring.version@ + + + javax.servlet + javax.servlet-api + @servlet-api.version@ + provided + + + diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/src/main/java/org/test/SampleApplication.java b/spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/src/main/java/org/test/SampleApplication.java new file mode 100644 index 0000000000..cf59ed0ae1 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/src/main/java/org/test/SampleApplication.java @@ -0,0 +1,24 @@ +/* + * Copyright 2012-2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.test; + +public class SampleApplication { + + public static void main(String[] args) { + } + +} diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/src/main/webapp/index.html b/spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/src/main/webapp/index.html new file mode 100644 index 0000000000..18ecdcb795 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/src/main/webapp/index.html @@ -0,0 +1 @@ + diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/verify.groovy b/spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/verify.groovy new file mode 100644 index 0000000000..fd0d220f83 --- /dev/null +++ b/spring-boot-tools/spring-boot-maven-plugin/src/it/war-with-unpack/verify.groovy @@ -0,0 +1,29 @@ +/* + * Copyright 2012-2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.*; +import org.springframework.boot.maven.*; + +File f = new File( basedir, "target/war-0.0.1.BUILD-SNAPSHOT.war") +new Verify.WarArchiveVerification(f) { + @Override + protected void verifyZipEntries(Verify.ArchiveVerifier verifier) throws Exception { + super.verifyZipEntries(verifier) + verifier.assertHasUnpackEntry("WEB-INF/lib/spring-core-") + verifier.assertHasNonUnpackEntry("WEB-INF/lib/spring-context-") + } +}.verify() + diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/Verify.java b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/Verify.java index d4a86375fa..f55377de2c 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/Verify.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/Verify.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2015 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. @@ -34,6 +34,7 @@ import static org.junit.Assert.assertTrue; * Verification utility for use with maven-invoker-plugin verification scripts. * * @author Phillip Webb + * @author Andy Wilkinson */ public class Verify { @@ -92,11 +93,21 @@ public class Verify { } } - public boolean hasNonUnpackEntry(String entryName) { + public void assertHasNonUnpackEntry(String entryName) { + assertTrue("Entry starting with " + entryName + " was an UNPACK entry", + hasNonUnpackEntry(entryName)); + } + + public void assertHasUnpackEntry(String entryName) { + assertTrue("Entry starting with " + entryName + " was not an UNPACK entry", + hasUnpackEntry(entryName)); + } + + private boolean hasNonUnpackEntry(String entryName) { return !hasUnpackEntry(entryName); } - public boolean hasUnpackEntry(String entryName) { + private boolean hasUnpackEntry(String entryName) { String comment = getEntryStartingWith(entryName).getComment(); return comment != null && comment.startsWith("UNPACK:"); }