diff --git a/spring-boot-cli/src/it/java/org/springframework/boot/cli/WarCommandIT.java b/spring-boot-cli/src/it/java/org/springframework/boot/cli/WarCommandIT.java index ca6c87d17f5..40046b9549b 100644 --- a/spring-boot-cli/src/it/java/org/springframework/boot/cli/WarCommandIT.java +++ b/spring-boot-cli/src/it/java/org/springframework/boot/cli/WarCommandIT.java @@ -16,24 +16,23 @@ package org.springframework.boot.cli; -import java.io.File; - import org.junit.Test; - import org.springframework.boot.cli.command.archive.WarCommand; import org.springframework.boot.cli.infrastructure.CommandLineInvoker; import org.springframework.boot.cli.infrastructure.CommandLineInvoker.Invocation; import org.springframework.boot.loader.tools.JavaExecutable; import org.springframework.util.SocketUtils; -import static org.hamcrest.Matchers.containsString; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import java.io.File; +import java.util.zip.ZipFile; + +import static org.assertj.core.api.Assertions.assertThat; /** * Integration test for {@link WarCommand}. * * @author Andrey Stolyarov + * @author Henri Kerola */ public class WarCommandIT { @@ -47,19 +46,38 @@ public class WarCommandIT { Invocation invocation = this.cli.invoke("war", war.getAbsolutePath(), "war.groovy"); invocation.await(); - assertTrue(war.exists()); + assertThat(war.exists()).isTrue(); Process process = new JavaExecutable() .processBuilder("-jar", war.getAbsolutePath(), "--server.port=" + port) .start(); invocation = new Invocation(process); invocation.await(); - assertThat(invocation.getOutput(), containsString("onStart error")); - assertThat(invocation.getOutput(), containsString("Tomcat started")); - assertThat(invocation.getOutput(), - containsString("/WEB-INF/lib-provided/tomcat-embed-core")); - assertThat(invocation.getOutput(), - containsString("/WEB-INF/lib-provided/tomcat-embed-core")); + assertThat(invocation.getOutput()).contains("onStart error"); + assertThat(invocation.getOutput()).contains("Tomcat started"); + assertThat(invocation.getOutput()) + .contains("/WEB-INF/lib-provided/tomcat-embed-core"); + assertThat(invocation.getOutput()) + .contains("/WEB-INF/lib-provided/tomcat-embed-core"); process.destroy(); } + @Test + public void resourcesAreCopiedToWebInfClasses() throws Exception { + File war = new File("target/test-app.war"); + Invocation invocation = this.cli.invoke("war", war.getAbsolutePath(), + "war.groovy"); + invocation.await(); + assertThat(war.exists()).isTrue(); + + ZipFile warFile = new ZipFile(war.getAbsolutePath()); + try { + assertThat(warFile.getEntry("application.properties")).isNull(); + assertThat(warFile.getEntry("WEB-INF/classes/application.properties")).isNotNull(); + } + finally { + warFile.close(); + } + + } + } diff --git a/spring-boot-cli/src/it/resources/war-command/application.properties b/spring-boot-cli/src/it/resources/war-command/application.properties new file mode 100644 index 00000000000..e69de29bb2d diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/archive/ArchiveCommand.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/archive/ArchiveCommand.java index 2986ae4b375..03448643e2c 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/archive/ArchiveCommand.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/archive/ArchiveCommand.java @@ -17,7 +17,6 @@ package org.springframework.boot.cli.command.archive; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -73,6 +72,7 @@ import org.springframework.util.Assert; * @author Andy Wilkinson * @author Phillip Webb * @author Andrey Stolyarov + * @author Henri Kerola */ abstract class ArchiveCommand extends OptionParsingCommand { @@ -93,7 +93,7 @@ abstract class ArchiveCommand extends OptionParsingCommand { private final String type; - private final Layout layout; + protected final Layout layout; private OptionSpec includeOption; @@ -278,13 +278,15 @@ abstract class ArchiveCommand extends OptionParsingCommand { libraries.add(new Library(entry.getFile(), LibraryScope.COMPILE)); } else { - writer.writeEntry(entry.getName(), - new FileInputStream(entry.getFile())); + writeClasspathEntry(writer, entry); } } return libraries; } + protected abstract void writeClasspathEntry(JarWriter writer, + MatchedResource entry) throws IOException; + protected abstract LibraryScope getLibraryScope(File file); } diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/archive/JarCommand.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/archive/JarCommand.java index 179e9c92989..bd151cf6a8b 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/archive/JarCommand.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/archive/JarCommand.java @@ -17,8 +17,11 @@ package org.springframework.boot.cli.command.archive; import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import org.springframework.boot.cli.command.Command; +import org.springframework.boot.loader.tools.JarWriter; import org.springframework.boot.loader.tools.Layouts; import org.springframework.boot.loader.tools.LibraryScope; @@ -27,6 +30,7 @@ import org.springframework.boot.loader.tools.LibraryScope; * * @author Andy Wilkinson * @author Phillip Webb + * @author Henri Kerola */ public class JarCommand extends ArchiveCommand { @@ -46,6 +50,13 @@ public class JarCommand extends ArchiveCommand { return LibraryScope.COMPILE; } + @Override + protected void writeClasspathEntry(JarWriter writer, + ResourceMatcher.MatchedResource entry) throws IOException { + writer.writeEntry(entry.getName(), + new FileInputStream(entry.getFile())); + } + } } diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/archive/WarCommand.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/archive/WarCommand.java index 80f0de42d69..fec3fafc6db 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/archive/WarCommand.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/archive/WarCommand.java @@ -17,6 +17,7 @@ package org.springframework.boot.cli.command.archive; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import org.springframework.boot.cli.command.Command; @@ -29,6 +30,7 @@ import org.springframework.boot.loader.tools.LibraryScope; * * @author Andrey Stolyarov * @author Phillip Webb + * @author Henri Kerola * @since 1.3.0 */ public class WarCommand extends ArchiveCommand { @@ -61,6 +63,12 @@ public class WarCommand extends ArchiveCommand { super.addCliClasses(writer); } + @Override + protected void writeClasspathEntry(JarWriter writer, + ResourceMatcher.MatchedResource entry) throws IOException { + writer.writeEntry(this.layout.getClassesLocation() + entry.getName(), + new FileInputStream(entry.getFile())); + } } }