@Test public void testFileResources() throws Exception { String someResource = "/org/node/resource.txt"; File file = new File("files/node-1.0-SNAPSHOT.jar"); URL url1 = file.getAbsoluteFile().toURI().toURL(); URL url2 = new File("files/invoke-1.0-SNAPSHOT.jar").getAbsoluteFile().toURI().toURL(); URLClassLoader loader = new URLClassLoader(new URL[] {url1, url2}); final List<String> resourcePaths = Classpaths.listFromClassLoader(loader, someResource); int fileCount = 0; int dirCount = 0; for (String path : resourcePaths) { if (!Files.isDirectory(IO.path(path))) { fileCount++; } else { dirCount++; } } boolean ok = true; ok |= dirCount == 0 || die(); // ok |= fileCount == 2 || die(); }
@Test public void testResourcesFromPath() throws Exception { final List<String> paths = Classpaths.resources(this.getClass(), "/org/node/file1.txt"); String path = paths.get(0); boolean ok = true; ok |= Str.in("abc", IO.read(path)) || die(); }
@Test public void test2NoRoot() throws Exception { final List<URL> urls = Classpaths.classpathResources(this.getClass(), "org/node/file1.txt"); URL url = urls.get(0); boolean ok = true; ok |= Str.in("abc", IO.read(url.openStream())) || die(); }
public static String readFromClasspath(Class<?> clazz, String location) { List<Path> resources = Classpaths.resources(clazz, location); if (len(resources) > 0) { try { return read(Files.newBufferedReader(resources.get(0), DEFAULT_CHARSET)); } catch (IOException e) { return Exceptions.handle(String.class, "unable to read classpath resource " + location, e); } } else { return null; } }
public static Path path(String location) { if (!location.startsWith(CLASSPATH_SCHEMA + ":")) { return Paths.get(location); } else { String path = StringScanner.split(location, ':')[1]; final List<Path> resources = Classpaths.resources(IO.class, path); Path result = Lists.idx(resources, 0); if (result == null) { return path(path); } return result; } }
private static List<String> listFromDefaultClassLoader(String s) { List<String> result = new ArrayList<>(); String newPath = s; final List<Path> resources = Classpaths.resources(IO.class, newPath); for (Path resourcePath : resources) { if (Files.isDirectory(resourcePath)) { result.addAll(IO.list(resourcePath)); } else { result.add(resourcePath.toString()); } } // for ( int index = 0; index < result.size(); index++ ) { // result.set( index, "classpath:" + result.get( index ) ); // } return result; }
@Test public void testFileResources2() throws Exception { String someResource = "/org/node/resource.txt"; File file = new File("files/node-1.0-SNAPSHOT.jar"); URL url1 = file.getAbsoluteFile().toURI().toURL(); URL url2 = new File("files/invoke-1.0-SNAPSHOT.jar").getAbsoluteFile().toURI().toURL(); URLClassLoader loader = new URLClassLoader(new URL[] {url1, url2}); final List<String> resourcePaths = Classpaths.listFromClassLoader(loader, someResource); List<Path> list = IO.paths("classpath://org/node/"); // List<Path> list = Classpaths.pathsFromClassLoader(loader, someResource); puts(multiply('-', 10), "Path "); for (Path path : list) { puts(path, path.getFileSystem(), path.getClass().getName()); if (path.toString().endsWith(".txt")) { puts(IO.readPath(path)); } } puts(multiply('-', 10), "String Path "); List<String> slist = IO.list("classpath://org/node/"); for (String spath : slist) { puts(spath); if (spath.toString().endsWith(".txt")) { puts(IO.readResource(spath)); } } }
@Override public ImmutableSetMultimap<JavaLibraryRule, String> getTransitiveClasspathEntries() { return Classpaths.getClasspathEntries(getDeps()); }
@Override public <A extends Args> BuildRule createBuildRule( TargetGraph targetGraph, BuildRuleParams params, BuildRuleResolver resolver, A args) { SourcePathResolver pathResolver = new SourcePathResolver(resolver); ImmutableMap<String, SourcePath> nativeLibraries = JavaLibraryRules.getNativeLibraries(targetGraph, params.getDeps(), cxxPlatform); BuildRuleParams binaryParams = params; // If we're packaging native libraries, we'll build the binary JAR in a separate rule and // package it into the final fat JAR, so adjust it's params to use a flavored target. if (!nativeLibraries.isEmpty()) { binaryParams = params.copyWithChanges( BuildTarget.builder(params.getBuildTarget()) .addFlavors(FAT_JAR_INNER_JAR_FLAVOR) .build(), params.getDeclaredDeps(), params.getExtraDeps()); } // Construct the build rule to build the binary JAR. ImmutableSetMultimap<JavaLibrary, Path> transitiveClasspathEntries = Classpaths.getClasspathEntries(binaryParams.getDeps()); BuildRule rule = new JavaBinary( binaryParams.appendExtraDeps(transitiveClasspathEntries.keys()), pathResolver, args.mainClass.orNull(), args.manifestFile.orNull(), args.mergeManifests.or(true), args.metaInfDirectory.orNull(), args.blacklist.or(ImmutableSortedSet.<String>of()), new DefaultDirectoryTraverser(), transitiveClasspathEntries, javaBinOverride); // If we're packaging native libraries, construct the rule to build the fat JAR, which packages // up the original binary JAR and any required native libraries. if (!nativeLibraries.isEmpty()) { BuildRule innerJarRule = rule; resolver.addToIndex(innerJarRule); SourcePath innerJar = new BuildTargetSourcePath(innerJarRule.getBuildTarget()); rule = new JarFattener( params.appendExtraDeps( Suppliers.<Iterable<BuildRule>>ofInstance( pathResolver.filterBuildRuleInputs( ImmutableList.<SourcePath>builder() .add(innerJar) .addAll(nativeLibraries.values()) .build()))), pathResolver, javacOptions, innerJar, nativeLibraries, javaBinOverride); } return rule; }