Add a command to produce a self-contained executable JAR for a CLI app
A new command, jar, has been added to the CLI. The command can be used to create a self-contained executable JAR file from a CLI app. Basic usage is: spring jar <jar-name> <source-files> For example: spring jar my-app.jar *.groovy The resulting jar will contain the classes generated by compiling the source files, all of the application's dependencies, and entries on the application's classpath. By default a CLI application has the current working directory on its classpath. This can be overridden using the --classpath option. Any file that is referenced directly by the classpath is always included in the jar. Any file that is found a result of being contained within a directory that is on the classpath is subject to filtering to determine whether or not it should be included. The default includes are public/**, static/**, resources/**, META-INF/**, *. The default excludes are .*, repository/**, build/**, target/**. To be included in the jar, a file must match one of the includes and none of the excludes. The filters can be overridden using the --include and --exclude options. Closes #241
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright 2012-2014 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.loader;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.JarURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.security.CodeSource;
|
||||
import java.security.ProtectionDomain;
|
||||
|
||||
import org.springframework.boot.loader.archive.Archive;
|
||||
import org.springframework.boot.loader.archive.ExplodedArchive;
|
||||
import org.springframework.boot.loader.archive.JarFileArchive;
|
||||
|
||||
/**
|
||||
* Resolves the {@link Archive} from which a {@link Class} was loaded.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class ArchiveResolver {
|
||||
|
||||
/**
|
||||
* Resolves the {@link Archive} that contains the given {@code clazz}.
|
||||
* @param clazz The class whose containing archive is to be resolved
|
||||
*
|
||||
* @return The class's containing archive
|
||||
* @throws IOException if an error occurs when resolving the containing archive
|
||||
*/
|
||||
public Archive resolveArchive(Class<?> clazz) throws IOException {
|
||||
File root = resolveArchiveLocation(clazz);
|
||||
return (root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root));
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the location of the archive that contains the given {@code clazz}.
|
||||
* @param clazz The class for which the location of the containing archive is to be
|
||||
* resolved
|
||||
*
|
||||
* @return The location of the class's containing archive
|
||||
* @throws IOException if an error occurs when resolving the containing archive's
|
||||
* location
|
||||
*/
|
||||
public File resolveArchiveLocation(Class<?> clazz) throws IOException {
|
||||
ProtectionDomain protectionDomain = getClass().getProtectionDomain();
|
||||
CodeSource codeSource = protectionDomain.getCodeSource();
|
||||
|
||||
if (codeSource != null) {
|
||||
File root;
|
||||
URL location = codeSource.getLocation();
|
||||
URLConnection connection = location.openConnection();
|
||||
if (connection instanceof JarURLConnection) {
|
||||
root = new File(((JarURLConnection) connection).getJarFile().getName());
|
||||
}
|
||||
else {
|
||||
root = new File(location.getPath());
|
||||
}
|
||||
|
||||
if (!root.exists()) {
|
||||
throw new IllegalStateException(
|
||||
"Unable to determine code source archive from " + root);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
throw new IllegalStateException("Unable to determine code source archive");
|
||||
}
|
||||
}
|
||||
@@ -16,10 +16,6 @@
|
||||
|
||||
package org.springframework.boot.loader;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
import java.security.CodeSource;
|
||||
import java.security.ProtectionDomain;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarEntry;
|
||||
@@ -27,8 +23,6 @@ import java.util.jar.JarEntry;
|
||||
import org.springframework.boot.loader.archive.Archive;
|
||||
import org.springframework.boot.loader.archive.Archive.Entry;
|
||||
import org.springframework.boot.loader.archive.Archive.EntryFilter;
|
||||
import org.springframework.boot.loader.archive.ExplodedArchive;
|
||||
import org.springframework.boot.loader.archive.JarFileArchive;
|
||||
|
||||
/**
|
||||
* Base class for executable archive {@link Launcher}s.
|
||||
@@ -49,19 +43,7 @@ public abstract class ExecutableArchiveLauncher extends Launcher {
|
||||
}
|
||||
|
||||
private Archive createArchive() throws Exception {
|
||||
ProtectionDomain protectionDomain = getClass().getProtectionDomain();
|
||||
CodeSource codeSource = protectionDomain.getCodeSource();
|
||||
URI location = (codeSource == null ? null : codeSource.getLocation().toURI());
|
||||
String path = (location == null ? null : location.getPath());
|
||||
if (path == null) {
|
||||
throw new IllegalStateException("Unable to determine code source archive");
|
||||
}
|
||||
File root = new File(path);
|
||||
if (!root.exists()) {
|
||||
throw new IllegalStateException(
|
||||
"Unable to determine code source archive from " + root);
|
||||
}
|
||||
return (root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root));
|
||||
return new ArchiveResolver().resolveArchive(getClass());
|
||||
}
|
||||
|
||||
protected final Archive getArchive() {
|
||||
|
||||
@@ -21,13 +21,10 @@ import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.net.URLConnection;
|
||||
import java.security.CodeSource;
|
||||
import java.security.ProtectionDomain;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
@@ -497,19 +494,7 @@ public class PropertiesLauncher extends Launcher {
|
||||
}
|
||||
|
||||
private Archive createArchive() throws Exception {
|
||||
ProtectionDomain protectionDomain = getClass().getProtectionDomain();
|
||||
CodeSource codeSource = protectionDomain.getCodeSource();
|
||||
URI location = (codeSource == null ? null : codeSource.getLocation().toURI());
|
||||
String path = (location == null ? null : location.getPath());
|
||||
if (path == null) {
|
||||
throw new IllegalStateException("Unable to determine code source archive");
|
||||
}
|
||||
File root = new File(path);
|
||||
if (!root.exists()) {
|
||||
throw new IllegalStateException(
|
||||
"Unable to determine code source archive from " + root);
|
||||
}
|
||||
return (root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root));
|
||||
return new ArchiveResolver().resolveArchive(getClass());
|
||||
}
|
||||
|
||||
private void addParentClassLoaderEntries(List<Archive> lib) throws IOException,
|
||||
|
||||
Reference in New Issue
Block a user