From d07f689446f7fccad51e0894c78b1ae51afeaf9c Mon Sep 17 00:00:00 2001 From: Henri Kerola Date: Sun, 10 Jul 2016 11:17:13 -0700 Subject: [PATCH 1/2] "spring war" should copy resources to WEB-INF/classes Closes gh-6351 --- .../boot/cli/WarCommandIT.java | 44 +++++++++++++------ .../war-command/application.properties | 0 .../cli/command/archive/ArchiveCommand.java | 10 +++-- .../boot/cli/command/archive/JarCommand.java | 11 +++++ .../boot/cli/command/archive/WarCommand.java | 8 ++++ 5 files changed, 56 insertions(+), 17 deletions(-) create mode 100644 spring-boot-cli/src/it/resources/war-command/application.properties 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 ca6c87d17f..40046b9549 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 0000000000..e69de29bb2 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 2986ae4b37..03448643e2 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 179e9c9298..bd151cf6a8 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 80f0de42d6..fec3fafc6d 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())); + } } } From 2d75cc79c038598989b8fe4f75ff86af231ba16c Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 5 Oct 2016 13:35:10 +0200 Subject: [PATCH 2/2] Polish contribution Closes gh-6367 --- .../boot/cli/JarCommandIT.java | 15 +++++++---- .../boot/cli/WarCommandIT.java | 27 +++---------------- .../src/it/resources/jar-command/jar.groovy | 1 + .../root.properties} | 0 .../it/resources/war-command/root.properties | 0 .../src/it/resources/war-command/war.groovy | 1 + .../cli/command/archive/ArchiveCommand.java | 16 ++++++++--- .../boot/cli/command/archive/JarCommand.java | 11 -------- .../boot/cli/command/archive/WarCommand.java | 5 ++-- 9 files changed, 31 insertions(+), 45 deletions(-) rename spring-boot-cli/src/it/resources/{war-command/application.properties => jar-command/root.properties} (100%) create mode 100644 spring-boot-cli/src/it/resources/war-command/root.properties diff --git a/spring-boot-cli/src/it/java/org/springframework/boot/cli/JarCommandIT.java b/spring-boot-cli/src/it/java/org/springframework/boot/cli/JarCommandIT.java index 5f98eae3a9..12326a12a5 100644 --- a/spring-boot-cli/src/it/java/org/springframework/boot/cli/JarCommandIT.java +++ b/spring-boot-cli/src/it/java/org/springframework/boot/cli/JarCommandIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 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. @@ -36,6 +36,7 @@ import static org.junit.Assert.assertTrue; * Integration test for {@link JarCommand}. * * @author Andy Wilkinson + * @author Stephane Nicoll */ public class JarCommandIT { @@ -98,12 +99,16 @@ public class JarCommandIT { assertThat(invocation.getErrorOutput(), equalTo("")); assertThat(invocation.getStandardOutput(), containsString("Hello World!")); - assertThat(invocation.getStandardOutput(), containsString("/public/public.txt")); assertThat(invocation.getStandardOutput(), - containsString("/resources/resource.txt")); - assertThat(invocation.getStandardOutput(), containsString("/static/static.txt")); + containsString("/BOOT-INF/classes!/public/public.txt")); assertThat(invocation.getStandardOutput(), - containsString("/templates/template.txt")); + containsString("/BOOT-INF/classes!/resources/resource.txt")); + assertThat(invocation.getStandardOutput(), + containsString("/BOOT-INF/classes!/static/static.txt")); + assertThat(invocation.getStandardOutput(), + containsString("/BOOT-INF/classes!/templates/template.txt")); + assertThat(invocation.getStandardOutput(), + containsString("/BOOT-INF/classes!/root.properties")); assertThat(invocation.getStandardOutput(), containsString("Goodbye Mama")); } 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 40046b9549..6d5856df01 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,16 +16,16 @@ 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 java.io.File; -import java.util.zip.ZipFile; - import static org.assertj.core.api.Assertions.assertThat; /** @@ -57,27 +57,8 @@ public class WarCommandIT { assertThat(invocation.getOutput()) .contains("/WEB-INF/lib-provided/tomcat-embed-core"); assertThat(invocation.getOutput()) - .contains("/WEB-INF/lib-provided/tomcat-embed-core"); + .contains("WEB-INF/classes!/root.properties"); 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/jar-command/jar.groovy b/spring-boot-cli/src/it/resources/jar-command/jar.groovy index ee912748d0..1f385b4f33 100644 --- a/spring-boot-cli/src/it/resources/jar-command/jar.groovy +++ b/spring-boot-cli/src/it/resources/jar-command/jar.groovy @@ -13,6 +13,7 @@ class Example implements CommandLineRunner { println getClass().getResource('/resources/resource.txt') println getClass().getResource('/static/static.txt') println getClass().getResource('/templates/template.txt') + println getClass().getResource('/root.properties') println template('template.txt', [world:'Mama']) } } diff --git a/spring-boot-cli/src/it/resources/war-command/application.properties b/spring-boot-cli/src/it/resources/jar-command/root.properties similarity index 100% rename from spring-boot-cli/src/it/resources/war-command/application.properties rename to spring-boot-cli/src/it/resources/jar-command/root.properties diff --git a/spring-boot-cli/src/it/resources/war-command/root.properties b/spring-boot-cli/src/it/resources/war-command/root.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-boot-cli/src/it/resources/war-command/war.groovy b/spring-boot-cli/src/it/resources/war-command/war.groovy index 1a8ca70eca..b1a4c75cf2 100644 --- a/spring-boot-cli/src/it/resources/war-command/war.groovy +++ b/spring-boot-cli/src/it/resources/war-command/war.groovy @@ -10,6 +10,7 @@ class WarExample implements CommandLineRunner { void run(String... args) { println getClass().getResource('/org/apache/tomcat/InstanceManager.class') + println getClass().getResource('/root.properties') throw new RuntimeException("onStart error") } 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 03448643e2..45e2b5bd33 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 @@ -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. @@ -17,6 +17,7 @@ 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; @@ -93,7 +94,7 @@ abstract class ArchiveCommand extends OptionParsingCommand { private final String type; - protected final Layout layout; + private final Layout layout; private OptionSpec includeOption; @@ -104,6 +105,10 @@ abstract class ArchiveCommand extends OptionParsingCommand { this.layout = layout; } + protected Layout getLayout() { + return this.layout; + } + @Override protected void doOptions() { this.includeOption = option("include", @@ -284,8 +289,11 @@ abstract class ArchiveCommand extends OptionParsingCommand { return libraries; } - protected abstract void writeClasspathEntry(JarWriter writer, - MatchedResource entry) throws IOException; + protected void writeClasspathEntry(JarWriter writer, + MatchedResource entry) throws IOException { + writer.writeEntry(entry.getName(), + new FileInputStream(entry.getFile())); + } 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 bd151cf6a8..179e9c9298 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,11 +17,8 @@ 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; @@ -30,7 +27,6 @@ import org.springframework.boot.loader.tools.LibraryScope; * * @author Andy Wilkinson * @author Phillip Webb - * @author Henri Kerola */ public class JarCommand extends ArchiveCommand { @@ -50,13 +46,6 @@ 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 fec3fafc6d..08ae910302 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 @@ -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. @@ -66,9 +66,10 @@ public class WarCommand extends ArchiveCommand { @Override protected void writeClasspathEntry(JarWriter writer, ResourceMatcher.MatchedResource entry) throws IOException { - writer.writeEntry(this.layout.getClassesLocation() + entry.getName(), + writer.writeEntry(getLayout().getClassesLocation() + entry.getName(), new FileInputStream(entry.getFile())); } + } }