Add spring war command
Add a `war` command to the CLI to generate WAR archives. Fixes gh-925 Closes gh-4168
This commit is contained in:
committed by
Phillip Webb
parent
b1aebe6075
commit
9a63e574b6
@@ -22,12 +22,13 @@ import java.util.List;
|
||||
|
||||
import org.springframework.boot.cli.command.Command;
|
||||
import org.springframework.boot.cli.command.CommandFactory;
|
||||
import org.springframework.boot.cli.command.archive.JarCommand;
|
||||
import org.springframework.boot.cli.command.archive.WarCommand;
|
||||
import org.springframework.boot.cli.command.core.VersionCommand;
|
||||
import org.springframework.boot.cli.command.grab.GrabCommand;
|
||||
import org.springframework.boot.cli.command.init.InitCommand;
|
||||
import org.springframework.boot.cli.command.install.InstallCommand;
|
||||
import org.springframework.boot.cli.command.install.UninstallCommand;
|
||||
import org.springframework.boot.cli.command.jar.JarCommand;
|
||||
import org.springframework.boot.cli.command.run.RunCommand;
|
||||
import org.springframework.boot.cli.command.test.TestCommand;
|
||||
|
||||
@@ -40,7 +41,7 @@ public class DefaultCommandFactory implements CommandFactory {
|
||||
|
||||
private static final List<Command> DEFAULT_COMMANDS = Arrays.<Command>asList(
|
||||
new VersionCommand(), new RunCommand(), new TestCommand(), new GrabCommand(),
|
||||
new JarCommand(), new InstallCommand(), new UninstallCommand(),
|
||||
new JarCommand(), new WarCommand(), new InstallCommand(), new UninstallCommand(),
|
||||
new InitCommand());
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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.springframework.boot.cli.app;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.jar.Manifest;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.context.web.SpringBootServletInitializer;
|
||||
|
||||
/**
|
||||
* {@link SpringBootServletInitializer} for CLI packaged WAR files.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.3.0
|
||||
*/
|
||||
public class SpringApplicationWebApplicationInitializer
|
||||
extends SpringBootServletInitializer {
|
||||
|
||||
/**
|
||||
* The entry containing the source class.
|
||||
*/
|
||||
public static final String SOURCE_ENTRY = "Spring-Application-Source-Classes";
|
||||
|
||||
private String[] sources;
|
||||
|
||||
@Override
|
||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||
try {
|
||||
this.sources = getSources(servletContext);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
super.onStartup(servletContext);
|
||||
}
|
||||
|
||||
private String[] getSources(ServletContext servletContext) throws IOException {
|
||||
Manifest manifest = getManifest(servletContext);
|
||||
if (manifest == null) {
|
||||
throw new IllegalStateException("Unable to read manifest");
|
||||
}
|
||||
String sources = manifest.getMainAttributes().getValue(SOURCE_ENTRY);
|
||||
return sources.split(",");
|
||||
}
|
||||
|
||||
private Manifest getManifest(ServletContext servletContext) throws IOException {
|
||||
InputStream stream = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF");
|
||||
Manifest manifest = (stream == null ? null : new Manifest(stream));
|
||||
return manifest;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
|
||||
try {
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
Class<?>[] sourceClasses = new Class<?>[this.sources.length];
|
||||
for (int i = 0; i < this.sources.length; i++) {
|
||||
sourceClasses[i] = classLoader.loadClass(this.sources[i]);
|
||||
}
|
||||
return builder.sources(sourceClasses)
|
||||
.properties("spring.groovy.template.check-template-location=false");
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.cli.jar;
|
||||
package org.springframework.boot.cli.archive;
|
||||
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
@@ -16,6 +16,6 @@
|
||||
|
||||
/**
|
||||
* Class that are packaged as part of CLI generated JARs.
|
||||
* @see org.springframework.boot.cli.command.jar.JarCommand
|
||||
* @see org.springframework.boot.cli.command.archive.JarCommand
|
||||
*/
|
||||
package org.springframework.boot.cli.jar;
|
||||
package org.springframework.boot.cli.archive;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.cli.command.jar;
|
||||
package org.springframework.boot.cli.command.archive;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
@@ -38,10 +38,12 @@ import org.codehaus.groovy.ast.expr.ConstantExpression;
|
||||
import org.codehaus.groovy.control.SourceUnit;
|
||||
import org.codehaus.groovy.transform.ASTTransformation;
|
||||
import org.springframework.boot.cli.app.SpringApplicationLauncher;
|
||||
import org.springframework.boot.cli.archive.PackagedSpringApplicationLauncher;
|
||||
import org.springframework.boot.cli.command.Command;
|
||||
import org.springframework.boot.cli.command.OptionParsingCommand;
|
||||
import org.springframework.boot.cli.command.jar.ResourceMatcher.MatchedResource;
|
||||
import org.springframework.boot.cli.command.archive.ResourceMatcher.MatchedResource;
|
||||
import org.springframework.boot.cli.command.options.CompilerOptionHandler;
|
||||
import org.springframework.boot.cli.command.options.OptionHandler;
|
||||
import org.springframework.boot.cli.command.options.OptionSetGroovyCompilerConfiguration;
|
||||
import org.springframework.boot.cli.command.options.SourceOptions;
|
||||
import org.springframework.boot.cli.command.status.ExitStatus;
|
||||
@@ -49,8 +51,8 @@ import org.springframework.boot.cli.compiler.GroovyCompiler;
|
||||
import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration;
|
||||
import org.springframework.boot.cli.compiler.RepositoryConfigurationFactory;
|
||||
import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration;
|
||||
import org.springframework.boot.cli.jar.PackagedSpringApplicationLauncher;
|
||||
import org.springframework.boot.loader.tools.JarWriter;
|
||||
import org.springframework.boot.loader.tools.Layout;
|
||||
import org.springframework.boot.loader.tools.Libraries;
|
||||
import org.springframework.boot.loader.tools.Library;
|
||||
import org.springframework.boot.loader.tools.LibraryCallback;
|
||||
@@ -65,51 +67,66 @@ import joptsimple.OptionSet;
|
||||
import joptsimple.OptionSpec;
|
||||
|
||||
/**
|
||||
* {@link Command} to create a self-contained executable jar file from a CLI application.
|
||||
* Abstract {@link Command} to create a self-contained executable archive file from a CLI
|
||||
* application.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Phillip Webb
|
||||
* @author Andrey Stolyarov
|
||||
*/
|
||||
public class JarCommand extends OptionParsingCommand {
|
||||
abstract class ArchiveCommand extends OptionParsingCommand {
|
||||
|
||||
public JarCommand() {
|
||||
super("jar",
|
||||
"Create a self-contained "
|
||||
+ "executable jar file from a Spring Groovy script",
|
||||
new JarOptionHandler());
|
||||
protected ArchiveCommand(String name, String description,
|
||||
OptionHandler optionHandler) {
|
||||
super(name, description, optionHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsageHelp() {
|
||||
return "[options] <jar-name> <files>";
|
||||
return "[options] <" + getName() + "-name> <files>";
|
||||
}
|
||||
|
||||
private static final class JarOptionHandler extends CompilerOptionHandler {
|
||||
/**
|
||||
* Abstract base {@link CompilerOptionHandler} for archive commands.
|
||||
*/
|
||||
protected abstract static class ArchiveOptionHandler extends CompilerOptionHandler {
|
||||
|
||||
private final String type;
|
||||
|
||||
private final Layout layout;
|
||||
|
||||
private OptionSpec<String> includeOption;
|
||||
|
||||
private OptionSpec<String> excludeOption;
|
||||
|
||||
public ArchiveOptionHandler(String type, Layout layout) {
|
||||
this.type = type;
|
||||
this.layout = layout;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doOptions() {
|
||||
this.includeOption = option("include",
|
||||
"Pattern applied to directories on the classpath to find files to include in the resulting jar")
|
||||
.withRequiredArg().withValuesSeparatedBy(",").defaultsTo("");
|
||||
"Pattern applied to directories on the classpath to find files to "
|
||||
+ "include in the resulting ").withRequiredArg()
|
||||
.withValuesSeparatedBy(",").defaultsTo("");
|
||||
this.excludeOption = option("exclude",
|
||||
"Pattern applied to directories on the classpath to find files to exclude from the resulting jar")
|
||||
.withRequiredArg().withValuesSeparatedBy(",").defaultsTo("");
|
||||
"Pattern applied to directories on the classpath to find files to "
|
||||
+ "exclude from the resulting " + this.type).withRequiredArg()
|
||||
.withValuesSeparatedBy(",").defaultsTo("");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ExitStatus run(OptionSet options) throws Exception {
|
||||
List<?> nonOptionArguments = new ArrayList<Object>(
|
||||
options.nonOptionArguments());
|
||||
Assert.isTrue(nonOptionArguments.size() >= 2,
|
||||
"The name of the resulting jar and at least one source file must be specified");
|
||||
Assert.isTrue(nonOptionArguments.size() >= 2, "The name of the resulting "
|
||||
+ this.type + " and at least one source file must be specified");
|
||||
|
||||
File output = new File((String) nonOptionArguments.remove(0));
|
||||
Assert.isTrue(output.getName().toLowerCase().endsWith(".jar"),
|
||||
"The output '" + output + "' is not a JAR file.");
|
||||
Assert.isTrue(output.getName().toLowerCase().endsWith("." + this.type),
|
||||
"The output '" + output + "' is not a " + this.type.toUpperCase()
|
||||
+ " file.");
|
||||
deleteIfExists(output);
|
||||
|
||||
GroovyCompiler compiler = createCompiler(options);
|
||||
@@ -196,7 +213,7 @@ public class JarCommand extends OptionParsingCommand {
|
||||
List<Library> libraries = new ArrayList<Library>();
|
||||
for (URL dependency : dependencies) {
|
||||
File file = new File(dependency.toURI());
|
||||
libraries.add(new Library(file, LibraryScope.COMPILE));
|
||||
libraries.add(new Library(file, getLibraryScope(file)));
|
||||
}
|
||||
return libraries;
|
||||
}
|
||||
@@ -220,7 +237,7 @@ public class JarCommand extends OptionParsingCommand {
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private void addCliClasses(JarWriter writer) throws IOException {
|
||||
protected void addCliClasses(JarWriter writer) throws IOException {
|
||||
addClass(writer, PackagedSpringApplicationLauncher.class);
|
||||
addClass(writer, SpringApplicationLauncher.class);
|
||||
Resource[] resources = new PathMatchingResourcePatternResolver()
|
||||
@@ -232,10 +249,19 @@ public class JarCommand extends OptionParsingCommand {
|
||||
}
|
||||
}
|
||||
|
||||
private void addClass(JarWriter writer, Class<?> sourceClass) throws IOException {
|
||||
String name = sourceClass.getName().replace(".", "/") + ".class";
|
||||
InputStream stream = sourceClass.getResourceAsStream("/" + name);
|
||||
writer.writeEntry(name, stream);
|
||||
protected final void addClass(JarWriter writer, Class<?> sourceClass)
|
||||
throws IOException {
|
||||
addClass(writer, sourceClass.getClassLoader(), sourceClass.getName());
|
||||
}
|
||||
|
||||
protected final void addClass(JarWriter writer, ClassLoader classLoader,
|
||||
String sourceClass) throws IOException {
|
||||
if (classLoader == null) {
|
||||
classLoader = Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
String name = sourceClass.replace(".", "/") + ".class";
|
||||
InputStream stream = classLoader.getResourceAsStream(name);
|
||||
writer.writeEntry(this.layout.getClassesLocation() + name, stream);
|
||||
}
|
||||
|
||||
private void addResource(JarWriter writer, Resource resource, String name)
|
||||
@@ -259,6 +285,8 @@ public class JarCommand extends OptionParsingCommand {
|
||||
return libraries;
|
||||
}
|
||||
|
||||
protected abstract LibraryScope getLibraryScope(File file);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.springframework.boot.cli.command.archive;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.springframework.boot.cli.command.Command;
|
||||
import org.springframework.boot.loader.tools.Layouts;
|
||||
import org.springframework.boot.loader.tools.LibraryScope;
|
||||
|
||||
/**
|
||||
* {@link Command} to create a self-contained executable jar file from a CLI application.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class JarCommand extends ArchiveCommand {
|
||||
|
||||
public JarCommand() {
|
||||
super("jar", "Create a self-contained executable jar "
|
||||
+ "file from a Spring Groovy script", new JarOptionHandler());
|
||||
}
|
||||
|
||||
private static final class JarOptionHandler extends ArchiveOptionHandler {
|
||||
|
||||
JarOptionHandler() {
|
||||
super("jar", new Layouts.Jar());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LibraryScope getLibraryScope(File file) {
|
||||
return LibraryScope.COMPILE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.cli.command.jar;
|
||||
package org.springframework.boot.cli.command.archive;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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.springframework.boot.cli.command.archive;
|
||||
|
||||
import java.io.File;
|
||||
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;
|
||||
|
||||
/**
|
||||
* {@link Command} to create a self-contained executable jar file from a CLI application.
|
||||
*
|
||||
* @author Andrey Stolyarov
|
||||
* @author Phillip Webb
|
||||
* @since 1.3.0
|
||||
*/
|
||||
public class WarCommand extends ArchiveCommand {
|
||||
|
||||
public WarCommand() {
|
||||
super("war", "Create a self-contained executable war "
|
||||
+ "file from a Spring Groovy script", new WarOptionHandler());
|
||||
}
|
||||
|
||||
private static final class WarOptionHandler extends ArchiveOptionHandler {
|
||||
|
||||
WarOptionHandler() {
|
||||
super("war", new Layouts.War());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LibraryScope getLibraryScope(File file) {
|
||||
String fileName = file.getName();
|
||||
if (fileName.contains("tomcat-embed")
|
||||
|| fileName.contains("spring-boot-starter-tomcat")) {
|
||||
return LibraryScope.PROVIDED;
|
||||
}
|
||||
return LibraryScope.COMPILE;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addCliClasses(JarWriter writer) throws IOException {
|
||||
addClass(writer, null, "org.springframework.boot."
|
||||
+ "cli.app.SpringApplicationWebApplicationInitializer");
|
||||
super.addCliClasses(writer);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user