Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching Maven artifacts programmatically [closed]

I'm looking for a Java API which can be used to retreive Maven artifacts from a remote repository. I've found Eclipse Ather so far but it looks over complicated for my needs so i'm seeking for something more simple.

What i need is:

  • I have to specify the location of the remote Maven repository
  • I like to fetch an artifact based on it's groupId + artifactId + version
  • The API have to provide the current remote version of the artifact (think about SNAPSHOT artifacts which are regularly built so they have a generated part in their versions)
  • Return the location of the artifact, a HTTP URL is preferred (i'll fetch it on my own with eg. Apache HTTP Client)
  • Optionally retreive the artifact's which are the dependants of the requested one.
like image 908
NagyI Avatar asked May 10 '12 14:05

NagyI


2 Answers

jcabi-aether may help you (I'm a developer). It's a simple wrapper around Aether, that lets you find all transitive dependencies of a Maven artifact:

File repo = this.session.getLocalRepository().getBasedir();
Collection<Artifact> deps = new Aether(this.getProject(), repo).resolve(
  new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
  JavaScopes.RUNTIME
);

Thus, all you need to provide as an input is:

  • Local repo location, as a directory name
  • List of repote repositories (MavenProject#getRemoteRepositories())
  • Maven coordinates of the artifact
  • Maven scope to look for

Absolute paths of every dependency found can be obtained as Artifact#getPath()

like image 140
yegor256 Avatar answered Sep 24 '22 08:09

yegor256


    public List<Artifact> findDependencies(Artifact artifact) throws DependencyCollectionException {

    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot( new Dependency(artifact, "" ) );
    collectRequest.addRepository(repository);

    final MavenServiceLocator locator = new MavenServiceLocator();
    locator.addService( RepositoryConnectorFactory.class, FileRepositoryConnectorFactory.class );
    locator.addService( RepositoryConnectorFactory.class, WagonRepositoryConnectorFactory.class );
    locator.setServices( WagonProvider.class, new WagonProvider() {
        public Wagon lookup(String roleHint) throws Exception {
            if (Arrays.asList("http", "https").contains(roleHint)) {
                return new LightweightHttpWagon();
            }
            return null;
        }

        public void release(Wagon wagon) {

        }
    });

    final RepositorySystem system = locator.getService(RepositorySystem.class);
    MavenRepositorySystemSession session = new MavenRepositorySystemSession();

    session.setLocalRepositoryManager( system.newLocalRepositoryManager(localRepository) );
    session.setTransferListener( new LoggingTransferListener() );
    session.setRepositoryListener( new LoggingRepositoryListener() );

    final List<Artifact> artifacts = new ArrayList<Artifact>();

    system.collectDependencies(session, collectRequest).getRoot().accept( new DependencyVisitor() {
        public boolean visitEnter(DependencyNode dependencyNode) {
            artifacts.add(dependencyNode.getDependency().getArtifact());
            return true;
        }

        public boolean visitLeave(DependencyNode dependencyNode) {
            return true;
        }
    });
    return artifacts;
}
like image 28
alex.collins Avatar answered Sep 22 '22 08:09

alex.collins