Search code examples
javaapimavendependenciesaether

Fetching Maven artifacts programmatically


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.

Solution

  • 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()