@Test public void jarFileAlwaysMatches() throws Exception { ResourceMatcher resourceMatcher = new ResourceMatcher(Arrays.asList("*"), Arrays.asList("**/*.jar")); List<MatchedResource> found = resourceMatcher .find(Arrays.asList(new File("src/test/resources/templates"), new File("src/test/resources/foo.jar"))); assertThat(found).areAtLeastOne(new Condition<MatchedResource>() { @Override public boolean matches(MatchedResource value) { return value.getFile().getName().equals("foo.jar") && value.isRoot(); } }); }
@Test public void resourceMatching() throws IOException { ResourceMatcher resourceMatcher = new ResourceMatcher( Arrays.asList("alpha/**", "bravo/*", "*"), Arrays.asList(".*", "alpha/**/excluded")); List<MatchedResource> matchedResources = resourceMatcher .find(Arrays.asList(new File("src/test/resources/resource-matcher/one"), new File("src/test/resources/resource-matcher/two"), new File("src/test/resources/resource-matcher/three"))); System.out.println(matchedResources); List<String> paths = new ArrayList<String>(); for (MatchedResource resource : matchedResources) { paths.add(resource.getName()); } assertThat(paths).containsOnly("alpha/nested/fileA", "bravo/fileC", "fileD", "bravo/fileE", "fileF", "three"); }
@Test public void resourceMatching() throws IOException { ResourceMatcher resourceMatcher = new ResourceMatcher( Arrays.asList("alpha/**", "bravo/*", "*"), Arrays.asList(".*", "alpha/**/excluded")); List<MatchedResource> matchedResources = resourceMatcher .find(Arrays.asList(new File("src/test/resources/resource-matcher/one"), new File("src/test/resources/resource-matcher/two"), new File("src/test/resources/resource-matcher/three"))); System.out.println(matchedResources); List<String> paths = new ArrayList<String>(); for (MatchedResource resource : matchedResources) { paths.add(resource.getName()); } assertEquals(6, paths.size()); assertTrue(paths.containsAll(Arrays.asList("alpha/nested/fileA", "bravo/fileC", "fileD", "bravo/fileE", "fileF", "three"))); }
@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 " + 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("." + this.type), "The output '" + output + "' is not a " + this.type.toUpperCase() + " file."); deleteIfExists(output); GroovyCompiler compiler = createCompiler(options); List<URL> classpath = getClassPathUrls(compiler); List<MatchedResource> classpathEntries = findMatchingClasspathEntries( classpath, options); String[] sources = new SourceOptions(nonOptionArguments).getSourcesArray(); Class<?>[] compiledClasses = compiler.compile(sources); List<URL> dependencies = getClassPathUrls(compiler); dependencies.removeAll(classpath); writeJar(output, compiledClasses, classpathEntries, dependencies); return ExitStatus.OK; }
private List<MatchedResource> findMatchingClasspathEntries(List<URL> classpath, OptionSet options) throws IOException { ResourceMatcher matcher = new ResourceMatcher( options.valuesOf(this.includeOption), options.valuesOf(this.excludeOption)); List<File> roots = new ArrayList<File>(); for (URL classpathEntry : classpath) { roots.add(new File(URI.create(classpathEntry.toString()))); } return matcher.find(roots); }
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 List<Library> addClasspathEntries(JarWriter writer, List<MatchedResource> entries) throws IOException { List<Library> libraries = new ArrayList<Library>(); for (MatchedResource entry : entries) { if (entry.isRoot()) { libraries.add(new Library(entry.getFile(), LibraryScope.COMPILE)); } else { writer.writeEntry(entry.getName(), new FileInputStream(entry.getFile())); } } return libraries; }
@Test public void nonExistentRoot() throws IOException { ResourceMatcher resourceMatcher = new ResourceMatcher( Arrays.asList("alpha/**", "bravo/*", "*"), Arrays.asList(".*", "alpha/**/excluded")); List<MatchedResource> matchedResources = resourceMatcher .find(Arrays.asList(new File("does-not-exist"))); assertThat(matchedResources).isEmpty(); }
@Test public void excludedWins() throws Exception { ResourceMatcher resourceMatcher = new ResourceMatcher(Arrays.asList("*"), Arrays.asList("**/*.jar")); List<MatchedResource> found = resourceMatcher .find(Arrays.asList(new File("src/test/resources"))); assertThat(found).areNot(new Condition<MatchedResource>() { @Override public boolean matches(MatchedResource value) { return value.getFile().getName().equals("foo.jar"); } }); }
@Test public void nonExistentRoot() throws IOException { ResourceMatcher resourceMatcher = new ResourceMatcher( Arrays.asList("alpha/**", "bravo/*", "*"), Arrays.asList(".*", "alpha/**/excluded")); List<MatchedResource> matchedResources = resourceMatcher .find(Arrays.asList(new File("does-not-exist"))); assertEquals(0, matchedResources.size()); }
@Test public void excludedWins() throws Exception { ResourceMatcher resourceMatcher = new ResourceMatcher(Arrays.asList("*"), Arrays.asList("**/*.jar")); List<MatchedResource> found = resourceMatcher .find(Arrays.asList(new File("src/test/resources"))); assertThat(found, not(hasItem(new FooJarMatcher(MatchedResource.class)))); }
@Test public void jarFileAlwaysMatches() throws Exception { ResourceMatcher resourceMatcher = new ResourceMatcher(Arrays.asList("*"), Arrays.asList("**/*.jar")); List<MatchedResource> found = resourceMatcher .find(Arrays.asList(new File("src/test/resources/templates"), new File("src/test/resources/foo.jar"))); FooJarMatcher matcher = new FooJarMatcher(MatchedResource.class); assertThat(found, hasItem(matcher)); // A jar file is always treated as a dependency (stick it in /lib) assertTrue(matcher.getMatched().isRoot()); }
@Override protected boolean matchesSafely(MatchedResource item) { boolean matches = item.getFile().getName().equals("foo.jar"); if (matches) { this.matched = item; } return matches; }
public MatchedResource getMatched() { return this.matched; }