9

If I have a Maven Artifact information (GroupId, ArtifactId, Version) how can I programmatically (using Java) retrieve that Artifact from my local repository?

Specifically, I need to be able to connect to the Maven Repository and create/retrieve a org.apache.maven.artifact.Artifact so I can retrieve the file associated with the Artifact.

I have looked into m2e source code, but the MavenImpl.java (which provides Artifact resolution) is way more complex than what I need and it is difficult to understand how the connection to the repository works.

yegor256
  • 102,010
  • 123
  • 446
  • 597
davdic
  • 249
  • 3
  • 10

3 Answers3

9

You'll probably want to look at Aether. See the Wiki for examples.

mkobit
  • 43,979
  • 12
  • 156
  • 150
Philippe Marschall
  • 4,452
  • 1
  • 34
  • 52
  • 4
    Note for anyone looking for this: Aether has been adopted by the apache foundation and renamed to maven-resolver: https://mvnrepository.com/artifact/org.apache.maven.resolver – oowekyala Apr 08 '20 at 18:30
0

You can construct a URL from the given information and download the file (note, replace the '.' in the <groupId> with '/'):

<repositoryUrl>/<groupId>/<artifactId>/<version>/<artifactId>-<version>.<type>
jtahlborn
  • 52,909
  • 5
  • 76
  • 118
0

This is how we do it in jcabi-aether:

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

Give it a list of remote repositories, a location of a local repo, and Maven coordinates of the artifact. As the name shows, the library uses Apache Aether from Sonatype.

yegor256
  • 102,010
  • 123
  • 446
  • 597
  • 2
    Hi! I try to use jcabi-aether outside of maven project as visible here: http://aether.jcabi.com/index.html, but I always getting a compile exception: Error:(22, 25) java: cannot access org.apache.maven.project.MavenProject class file for org.apache.maven.project.MavenProject not found. If I include maven-core, I get other exceptions. Why? Is this project still maintained? – Gergely Fehérvári Apr 07 '16 at 14:19