private void writeJar(File file, Class<?>[] compiledClasses, List<MatchedResource> classpathEntries, List<URL> dependencies) throws FileNotFoundException, IOException, URISyntaxException { final List<Library> libraries; JarWriter writer = new JarWriter(file); try { addManifest(writer, compiledClasses); addCliClasses(writer); for (Class<?> compiledClass : compiledClasses) { addClass(writer, compiledClass); } libraries = addClasspathEntries(writer, classpathEntries); } finally { writer.close(); } libraries.addAll(createLibraries(dependencies)); Repackager repackager = new Repackager(file); repackager.setMainClass(PackagedSpringApplicationLauncher.class.getName()); repackager.repackage(new Libraries() { @Override public void doWithLibraries(LibraryCallback callback) throws IOException { for (Library library : libraries) { callback.library(library); } } }); }
private void addManifest(JarWriter writer, Class<?>[] compiledClasses) throws IOException { Manifest manifest = new Manifest(); manifest.getMainAttributes().putValue("Manifest-Version", "1.0"); manifest.getMainAttributes().putValue( PackagedSpringApplicationLauncher.SOURCE_ENTRY, commaDelimitedClassNames(compiledClasses)); writer.writeManifest(manifest); }
protected void addCliClasses(JarWriter writer) throws IOException { addClass(writer, PackagedSpringApplicationLauncher.class); addClass(writer, SpringApplicationLauncher.class); Resource[] resources = new PathMatchingResourcePatternResolver() .getResources("org/springframework/boot/groovy/**"); for (Resource resource : resources) { String url = resource.getURL().toString(); addResource(writer, resource, url.substring(url.indexOf("org/springframework/boot/groovy/"))); } }