private URLClassLoader createClassLoader(List<Artifact> artifacts, String name) throws IOException { log.debug("Classpath for %s:", name); List<URL> urls = new ArrayList<>(); for (Artifact artifact : sortedArtifacts(artifacts)) { if (artifact.getFile() == null) { throw new RuntimeException("Could not resolve artifact: " + artifact); } File file = artifact.getFile().getCanonicalFile(); log.debug(" %s", file); urls.add(file.toURI().toURL()); } return createClassLoader(urls); }
public static Set<String> discoverPlugins(Artifact artifact, ClassLoader classLoader) throws IOException { if (!artifact.getExtension().equals("presto-plugin")) { throw new RuntimeException("Unexpected extension for main artifact: " + artifact); } File file = artifact.getFile(); if (!file.getPath().endsWith("/target/classes")) { throw new RuntimeException("Unexpected file for main artifact: " + file); } if (!file.isDirectory()) { throw new RuntimeException("Main artifact file is not a directory: " + file); } if (new File(file, SERVICES_FILE).exists()) { return ImmutableSet.of(); } return listClasses(file.toPath()).stream() .filter(name -> classInterfaces(name, classLoader).contains(Plugin.class.getName())) .collect(toImmutableSet()); }
private URLClassLoader buildClassLoaderFromPom(File pomFile) throws Exception { List<Artifact> artifacts = resolver.resolvePom(pomFile); URLClassLoader classLoader = createClassLoader(artifacts, pomFile.getPath()); Artifact artifact = artifacts.get(0); Set<String> plugins = discoverPlugins(artifact, classLoader); if (!plugins.isEmpty()) { writePluginServices(plugins, artifact.getFile()); } return classLoader; }
private List<File> loadFromMvn(String artifact, Collection<String> excludes) throws RepositoryException { Collection<String> allExclusions = new LinkedList<>(); allExclusions.addAll(excludes); allExclusions.addAll(Arrays.asList(exclusions)); List<ArtifactResult> listOfArtifact; listOfArtifact = getArtifactsWithDep(artifact, allExclusions); Iterator<ArtifactResult> it = listOfArtifact.iterator(); while (it.hasNext()) { Artifact a = it.next().getArtifact(); String gav = a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion(); for (String exclude : allExclusions) { if (gav.startsWith(exclude)) { it.remove(); break; } } } List<File> files = new LinkedList<>(); for (ArtifactResult artifactResult : listOfArtifact) { files.add(artifactResult.getArtifact().getFile()); logger.debug("load {}", artifactResult.getArtifact().getFile().getAbsolutePath()); } return files; }
/** * fetch all artifacts * @return * @throws MalformedURLException * @throws ArtifactResolutionException * @throws DependencyResolutionException */ public List<File> fetch() throws MalformedURLException, DependencyResolutionException, ArtifactResolutionException { for (Dependency dep : dependencies) { if (!dep.isLocalFsArtifact()) { List<ArtifactResult> artifacts = fetchArtifactWithDep(dep); for (ArtifactResult artifact : artifacts) { if (dep.isDist()) { filesDist.add(artifact.getArtifact().getFile()); } files.add(artifact.getArtifact().getFile()); } } else { if (dep.isDist()) { filesDist.add(new File(dep.getGroupArtifactVersion())); } files.add(new File(dep.getGroupArtifactVersion())); } } return files; }
protected static String artifactResultToFilePath(ArtifactResult result) { if (result != null) { Artifact artifact = result.getArtifact(); if (artifact != null) { File file = artifact.getFile(); if (file != null) { return file.getAbsolutePath(); } } } return null; }
private void addDependencies(final ClassRealm childClassRealm, Set<org.sonatype.aether.artifact.Artifact> artifacts) throws MalformedURLException { for (org.sonatype.aether.artifact.Artifact a : artifacts) { childClassRealm.addURL(a.getFile().toURI().toURL()); } }
public List<URL> resolve(File rootPom, boolean offline) throws RepositoryException { List<ArtifactResult> results = resolveResult(rootPom, offline); List<URL> urls = new ArrayList<URL>(); for (ArtifactResult r : results) { try { urls.add(r.getArtifact().getFile().toURI().toURL()); } catch (MalformedURLException e) { // Should never happen throw new RuntimeException(e); } } return urls; }
@Override public void executeOn(Artifact artifact, File outputDirectory, Map<String,String> parameters) { File file = artifact.getFile(); if (outputDirectory != null) { io.copy(file, new File(outputDirectory, file.getName())); } else { io.copy(file, new File(outputDirectory, file.getName())); } } }
private File resolveFatLib(MavenProject project, final String xcodeConfiguration, final Artifact primaryArtifact) { try { return downloadManager.resolveSideArtifact(primaryArtifact, xcodeConfiguration + XCodeFatLibraryMojo.FAT_LIBRARY_CLASSIFIER_SUFFIX, TYPE_ARCHIVE).getFile(); } catch (SideArtifactNotFoundException ex) { LOGGER.info("There does not exist a fat library for the artifact " + primaryArtifact.getId()); return null; } }
@Override protected File resolveArtifact(Artifact a) throws ArtifactResolutionException { return rs.resolveArtifact(a).getArtifact().getFile(); } }
/** * Indicates whether the requested artifact was resolved. Note that the artifact might have been successfully * resolved despite {@link #getExceptions()} indicating transfer errors while trying to fetch the artifact from some * of the specified remote repositories. * * @return {@code true} if the artifact was resolved, {@code false} otherwise. * @see Artifact#getFile() */ public boolean isResolved() { return getArtifact() != null && getArtifact().getFile() != null; }
/** * Indicates whether the requested artifact was resolved. Note that the artifact might have been successfully * resolved despite {@link #getExceptions()} indicating transfer errors while trying to fetch the artifact from some * of the specified remote repositories. * * @return {@code true} if the artifact was resolved, {@code false} otherwise. * @see Artifact#getFile() */ public boolean isResolved() { return getArtifact() != null && getArtifact().getFile() != null; }
private URLClassLoader buildClassLoaderFromPom(File pomFile) throws Exception { List<Artifact> artifacts = resolver.resolvePom(pomFile); URLClassLoader classLoader = createClassLoader(artifacts, pomFile.getPath()); Artifact artifact = artifacts.get(0); Set<String> plugins = discoverPlugins(artifact, classLoader); if (!plugins.isEmpty()) { writePluginServices(plugins, artifact.getFile()); } return classLoader; }
private URLClassLoader buildClassLoaderFromPom(File pomFile) throws Exception { List<Artifact> artifacts = resolver.resolvePom(pomFile); URLClassLoader classLoader = createClassLoader(artifacts, pomFile.getPath()); Artifact artifact = artifacts.get(0); Set<String> plugins = discoverPlugins(artifact, classLoader); if (!plugins.isEmpty()) { writePluginServices(plugins, artifact.getFile()); } return classLoader; }
private URLClassLoader buildClassLoaderFromPom(File pomFile) throws Exception { List<Artifact> artifacts = resolver.resolvePom(pomFile); URLClassLoader classLoader = createClassLoader(artifacts, pomFile.getPath()); Artifact artifact = artifacts.get(0); Set<String> plugins = discoverPlugins(artifact, classLoader); if (!plugins.isEmpty()) { writePluginServices(plugins, artifact.getFile()); } return classLoader; }
@Override protected File resolveArtifact(Artifact a) throws ArtifactResolutionException { return getRepositoryService().resolveArtifact(a).getArtifact().getFile(); } };
@Test public void testResolveArtifacts() throws Exception { ArtifactResolver artifactResolver = new ArtifactResolver(USER_LOCAL_REPO, MAVEN_CENTRAL_URI); List<Artifact> artifacts = artifactResolver.resolveArtifacts(ImmutableList.of(new DefaultArtifact("org.apache.maven:maven-core:3.0.4"))); Assert.assertNotNull(artifacts, "artifacts is null"); for (Artifact artifact : artifacts) { Assert.assertNotNull(artifact.getFile(), "Artifact " + artifact + " is not resolved"); } }
public File resolveFile(Artifact root) throws ArtifactResolutionException { RepositorySystem repositorySystem = getRepositorySystem(); final MavenRepositorySystemSession session = createRepositorSystemSession(offline, repositorySystem); List<RemoteRepository> repos = getRemoteRepositories(); ArtifactRequest request = new ArtifactRequest(); request.setArtifact(root); request.setRepositories(repos); request.setRequestContext("runtime"); ArtifactResult result = repositorySystem.resolveArtifact(session, request); return result.getArtifact().getFile(); }
public File resolveFile(Artifact root) throws ArtifactResolutionException { RepositorySystem repositorySystem = getRepositorySystem(); final MavenRepositorySystemSession session = createRepositorSystemSession(offline, repositorySystem); List<RemoteRepository> repos = getRemoteRepositories(); ArtifactRequest request = new ArtifactRequest(); request.setArtifact(root); request.setRepositories(repos); request.setRequestContext("runtime"); ArtifactResult result = repositorySystem.resolveArtifact(session, request); return result.getArtifact().getFile(); }