org.sonatype.aether.artifact.Artifact Java Examples

The following examples show how to use org.sonatype.aether.artifact.Artifact. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PluginDiscovery.java    From presto with Apache License 2.0 6 votes vote down vote up
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()) {
        throw new RuntimeException("Unexpected services file in artifact directory: " + file);
    }

    return listClasses(file.toPath()).stream()
            .filter(name -> classInterfaces(name, classLoader).contains(Plugin.class.getName()))
            .collect(toImmutableSet());
}
 
Example #2
Source File: PluginManager.java    From presto with Apache License 2.0 6 votes vote down vote up
private PluginClassLoader buildClassLoaderFromPom(File pomFile)
        throws Exception
{
    List<Artifact> artifacts = resolver.resolvePom(pomFile);
    PluginClassLoader classLoader = createClassLoader(artifacts, pomFile.getPath());

    Artifact artifact = artifacts.get(0);
    Set<String> plugins = discoverPlugins(artifact, classLoader);
    if (!plugins.isEmpty()) {
        File root = new File(artifact.getFile().getParentFile().getCanonicalFile(), "plugin-discovery");
        writePluginServices(plugins, root);
        log.debug("    %s", root);
        classLoader = classLoader.withUrl(root.toURI().toURL());
    }

    return classLoader;
}
 
Example #3
Source File: DependencyContext.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private List<ArtifactResult> fetchArtifactWithDep(Dependency dep)
    throws DependencyResolutionException, ArtifactResolutionException {
  Artifact artifact = new DefaultArtifact(dep.getGroupArtifactVersion());

  DependencyFilter classpathFilter = DependencyFilterUtils
      .classpathFilter(JavaScopes.COMPILE);
  PatternExclusionsDependencyFilter exclusionFilter = new PatternExclusionsDependencyFilter(
      dep.getExclusions());

  CollectRequest collectRequest = new CollectRequest();
  collectRequest.setRoot(new org.sonatype.aether.graph.Dependency(artifact,
      JavaScopes.COMPILE));

  collectRequest.addRepository(mavenCentral);
  collectRequest.addRepository(mavenLocal);
  for (Repository repo : repositories) {
    RemoteRepository rr = new RemoteRepository(repo.getId(), "default", repo.getUrl());
    rr.setPolicy(repo.isSnapshot(), null);
    collectRequest.addRepository(rr);
  }

  DependencyRequest dependencyRequest = new DependencyRequest(collectRequest,
      DependencyFilterUtils.andFilter(exclusionFilter, classpathFilter));

  return system.resolveDependencies(session, dependencyRequest).getArtifactResults();
}
 
Example #4
Source File: PluginManager.java    From presto with Apache License 2.0 5 votes vote down vote up
private PluginClassLoader buildClassLoaderFromCoordinates(String coordinates)
        throws Exception
{
    Artifact rootArtifact = new DefaultArtifact(coordinates);
    List<Artifact> artifacts = resolver.resolveArtifacts(rootArtifact);
    return createClassLoader(artifacts, rootArtifact.toString());
}
 
Example #5
Source File: PluginManager.java    From presto with Apache License 2.0 5 votes vote down vote up
private PluginClassLoader 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);
}
 
Example #6
Source File: MavenJarUtil.java    From alchemy with Apache License 2.0 5 votes vote down vote up
public MavenLoaderInfo forGAV(String gav) {
    try {
        CollectRequest e = this.createCollectRequestForGAV(gav);
        List artifacts = this.collectDependenciesIntoArtifacts(e);
        LinkedList urls = Lists.newLinkedList();
        String[] gavs = gav.split(":");
        String jarName = gavs[1] + "-" + gavs[2];
        File targetFile = null;
        Iterator urlClassLoader = artifacts.iterator();

        while (urlClassLoader.hasNext()) {
            Artifact artifact = (Artifact)urlClassLoader.next();
            File file = artifact.getFile();
            urls.add(file.toURI().toURL());
            if (file.getName().contains(jarName)) {
                targetFile = file;
            }
        }
        URLClassLoader classLoader = AccessController.doPrivileged(new PrivilegedAction<URLClassLoader>() {
            @Override
            public URLClassLoader run() {
                return new URLClassLoader((URL[])urls.toArray(new URL[urls.size()]), SHARE_NOTHING);
            }
        });
        return new MavenLoaderInfo(classLoader, targetFile);
    } catch (Exception var11) {
        throw Throwables.propagate(var11);
    }
}
 
Example #7
Source File: MavenJarUtil.java    From alchemy with Apache License 2.0 5 votes vote down vote up
private List<Artifact> collectDependenciesIntoArtifacts(CollectRequest collectRequest)
    throws PlexusContainerException, ComponentLookupException, DependencyCollectionException,
    ArtifactResolutionException {
    RepositorySystem repositorySystem = this.newRepositorySystem();
    RepositorySystemSession session = this.newSession(repositorySystem);
    DependencyNode node = repositorySystem.collectDependencies(session, collectRequest).getRoot();
    repositorySystem.resolveDependencies(session, node, null);
    PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
    node.accept(nlg);
    return nlg.getArtifacts(false);
}
 
Example #8
Source File: DependencyResolver.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #9
Source File: DependencyResolver.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
/**
 * @param dependency
 * @param excludes list of pattern can either be of the form groupId:artifactId
 * @return
 * @throws Exception
 */
@Override
public List<ArtifactResult> getArtifactsWithDep(String dependency,
                                                Collection<String> excludes)
    throws RepositoryException {
  Artifact artifact = new DefaultArtifact(dependency);
  DependencyFilter classpathFilter = DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE);
  PatternExclusionsDependencyFilter exclusionFilter =
          new PatternExclusionsDependencyFilter(excludes);

  CollectRequest collectRequest = new CollectRequest();
  collectRequest.setRoot(new Dependency(artifact, JavaScopes.COMPILE));

  synchronized (repos) {
    for (RemoteRepository repo : repos) {
      collectRequest.addRepository(repo);
    }
  }
  DependencyRequest dependencyRequest = new DependencyRequest(collectRequest,
          DependencyFilterUtils.andFilter(exclusionFilter, classpathFilter));
  try {
    return system.resolveDependencies(session, dependencyRequest).getArtifactResults();
  } catch (NullPointerException | DependencyResolutionException ex) {
    throw new RepositoryException(
            String.format("Cannot fetch dependencies for %s", dependency), ex);
  }
}
 
Example #10
Source File: PluginManager.java    From presto with Apache License 2.0 4 votes vote down vote up
private static List<Artifact> sortedArtifacts(List<Artifact> artifacts)
{
    List<Artifact> list = new ArrayList<>(artifacts);
    Collections.sort(list, Ordering.natural().nullsLast().onResultOf(Artifact::getFile));
    return list;
}
 
Example #11
Source File: IDEWorkspaceReader1.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public File findArtifact(Artifact artifact) {
    return super.findArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(), artifact.getExtension(), artifact.getClassifier());
}
 
Example #12
Source File: IDEWorkspaceReader1.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public List<String> findVersions(Artifact artifact) {
    return super.findVersions(artifact.getGroupId(), artifact.getArtifactId());
}