1 package dst.ass2.aop.util;
3 import static org.apache.commons.io.FileUtils.openOutputStream;
4 import static org.apache.commons.io.IOUtils.copy;
5 import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
6 import static org.apache.commons.lang3.StringUtils.join;
7 import static org.springframework.util.ClassUtils.CLASS_FILE_SUFFIX;
8 import static org.springframework.util.ClassUtils.convertClassNameToResourcePath;
11 import java.io.IOException;
12 import java.util.jar.JarEntry;
13 import java.util.jar.JarOutputStream;
14 import java.util.zip.ZipOutputStream;
16 import org.apache.commons.io.input.AutoCloseInputStream;
17 import org.springframework.core.io.ClassPathResource;
20 * Builds plugin JARs on demand.
22 * This class is for internal purposes only.
23 * Note that the {@link #main(String...)} method can be adjusted to create other plugins.
25 public final class JarUtils {
29 public static void main(String... args) throws IOException {
30 String path = join(args, " ");
31 File dir = new File(defaultIfBlank(path, "ass2-aop/src/test/resources"));
33 createJar(new File(dir, "simple.zip"),
34 "dst.ass2.aop.sample.SimplePluginExecutable"
37 createJar(new File(dir, "all.zip"),
38 "dst.ass2.aop.sample.SimplePluginExecutable",
39 "dst.ass2.aop.sample.IgnoredPluginExecutable"
44 * Creates a new JAR file containing the given classes.
46 * @param jarFile the destination JAR file
47 * @param classes the classes to add
48 * @throws IOException if an I/O error has occurred
50 public static void createJar(File jarFile, String... classes) throws IOException {
51 try (JarOutputStream stream = new JarOutputStream(openOutputStream(jarFile))) {
52 stream.setLevel(ZipOutputStream.STORED);
53 for (String clazz : classes) {
54 String path = convertClassNameToResourcePath(clazz) + CLASS_FILE_SUFFIX;
55 stream.putNextEntry(new JarEntry(path));
56 copy(new AutoCloseInputStream(new ClassPathResource(path).getInputStream()), stream);