protected final 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.getSchemeSpecificPart()); 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)); }
@Override protected List<Archive> getClassPathArchives() throws Exception { List<Archive> lib = new ArrayList<Archive>(); for (String path : this.paths) { for (Archive archive : getClassPathArchives(path)) { if (archive instanceof ExplodedArchive) { List<Archive> nested = new ArrayList<Archive>( archive.getNestedArchives(new ArchiveEntryFilter())); nested.add(0, archive); lib.addAll(nested); } else { lib.add(archive); } } } addNestedEntries(lib); return lib; }
@Test public void explodedWarHasOnlyWebInfClassesAndContentsOfWebInfLibOnClasspath() throws Exception { File warRoot = new File("target/exploded-war"); FileSystemUtils.deleteRecursively(warRoot); warRoot.mkdirs(); File webInfClasses = new File(warRoot, "WEB-INF/classes"); webInfClasses.mkdirs(); File webInfLib = new File(warRoot, "WEB-INF/lib"); webInfLib.mkdirs(); File webInfLibFoo = new File(webInfLib, "foo.jar"); new JarOutputStream(new FileOutputStream(webInfLibFoo)).close(); WarLauncher launcher = new WarLauncher(new ExplodedArchive(warRoot, true)); List<Archive> archives = launcher.getClassPathArchives(); assertThat(archives).hasSize(2); assertThat(getUrls(archives)).containsOnly(webInfClasses.toURI().toURL(), new URL("jar:" + webInfLibFoo.toURI().toURL() + "!/")); }
@Test public void explodedWarHasOnlyWebInfClassesAndContentsOfWebInfLibOnClasspath() throws Exception { File warRoot = new File("target/exploded-war"); FileSystemUtils.deleteRecursively(warRoot); warRoot.mkdirs(); File webInfClasses = new File(warRoot, "WEB-INF/classes"); webInfClasses.mkdirs(); File webInfLib = new File(warRoot, "WEB-INF/lib"); webInfLib.mkdirs(); File webInfLibFoo = new File(webInfLib, "foo.jar"); new JarOutputStream(new FileOutputStream(webInfLibFoo)).close(); WarLauncher launcher = new WarLauncher(new ExplodedArchive(warRoot, true)); List<Archive> archives = launcher.getClassPathArchives(); assertEquals(2, archives.size()); assertThat(getUrls(archives), hasItems(webInfClasses.toURI().toURL(), new URL("jar:" + webInfLibFoo.toURI().toURL() + "!/"))); }
@Override public List<Archive> getNestedArchives(EntryFilter ignored) throws IOException { try { List<Archive> archives = new ArrayList<>(mavenProject.getRuntimeClasspathElements().size()); for (String dep : mavenProject.getRuntimeClasspathElements()) { File file = new File(dep); archives.add(file.isDirectory() ? new ExplodedArchive(file) : new JarFileArchive(file)); } return archives; } catch (DependencyResolutionRequiredException e) { throw new IOException("Could not create boot archive", e); } }
private void addParentClassLoaderEntries(List<Archive> lib) throws IOException, URISyntaxException { ClassLoader parentClassLoader = getClass().getClassLoader(); List<Archive> urls = new ArrayList<Archive>(); for (URL url : getURLs(parentClassLoader)) { if (url.toString().endsWith(".jar") || url.toString().endsWith(".zip")) { urls.add(new JarFileArchive(new File(url.toURI()))); } else if (url.toString().endsWith("/*")) { String name = url.getFile(); File dir = new File(name.substring(0, name.length() - 1)); if (dir.exists()) { urls.add(new ExplodedArchive( new File(name.substring(0, name.length() - 1)), false)); } } else { String filename = URLDecoder.decode(url.getFile(), "UTF-8"); urls.add(new ExplodedArchive(new File(filename))); } } // The parent archive might have a "lib/" directory, meaning we are running from // an executable JAR. We add nested entries from there with low priority (i.e. at // end). addNestedArchivesFromParent(urls); for (Archive archive : urls) { // But only add them if they are not already included if (findArchive(lib, archive) < 0) { lib.add(archive); } } }
private Archive resolveAsArchive(Resource app) throws IOException { File moduleFile = app.getFile(); return moduleFile.isDirectory() ? new ExplodedArchive(moduleFile) : new JarFileArchive(moduleFile); }