7

I am writing an application that requires me to download a jar given the mavens groupid/artifactid/version.

I currently start with

public Model pomToModel(String pomUrl) throws Exception {
URL url = new URL(pomUrl);
    InputStream stream = url.openStream();
    try {
        Model model = new MavenXpp3Reader().read(stream);
        return model;
    } finally {
        stream.close();
    }
}

So given the url of the POM, I now have the maven Model object representing it. This seems to work well.

What I would like to do know is two things:

  1. find the url of the jar artifact that the model represents in the maven repository that the maven plugin would use
  2. given the model, find the file representing the jar in the local repository

I already have poor quality solutions: I go to mvnrepository.com manually coding the url, and I use ~/.m2/repository as the prefix for the file. I can obviously improve this solution, but I will end up poorly duplicating the code that is well tested and well used in the maven code base.

I'd really like to know how to do this using the maven classes.

I have done a fair bit of google searching, but the amusing thing about searching for anything about maven and jars (even on stackoverflow) is that I find lots of solutions about how to use maven, rather than how to code using the maven classes.

Thanks for the help

yegor256
  • 102,010
  • 123
  • 446
  • 597
Phil Rice
  • 151
  • 1
  • 6

2 Answers2

8

Option 1

Aether as recommended by khmarbaise.

It's the native library that ships with Maven 3.0

Option 2

If you want a simple solution for downloading Maven modules from the command line or within a shell script, Apache ivy has a standalone jar.

Example:

using IVY dependencies manager programmatically

Option 3

If you're just looking for a Maven jar and don't care about dependency management it's even simpler.

Here's how to retrieve the log4j jar direct from Maven Central:

curl -O http://search.maven.org/remotecontent?filepath=log4j/log4j/1.2.17/log4j-1.2.17.jar

If you're using a Nexus repository manager it has a useful REST API

Using the Nexus rest API to get latest artifact version for given groupid/artifactId

Community
  • 1
  • 1
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • I have to say that the ivy approach worked really well. Compiled and run. http://developers-blog.org/blog/default/2010/11/08/Embed-Ivy-How-to-use-Ivy-with-Java was excellent. Thanks I will go with this – Phil Rice Jun 05 '12 at 20:33
  • Don't forget to mark this as the answer if this was your solution – Noremac Apr 18 '13 at 14:55
  • @PhilRice if this answer worked for you, you should click the check mark button to accept it. – Cardinal System Oct 31 '22 at 19:15
  • In my case, I am going with option 3. How do we build the query? Is it just `?filepath=groupId/artifactId/artifact-version.jar`? I tried this with gson and had no luck. – Cardinal System Oct 31 '22 at 19:24
2

To download artifacts from a Maven repository it is intended to use Aether lib.

See also http://www.sonatype.com/people/category/aether/

Furthermore there are examples how to do things and the complete source code of Aether lib. Longer time a ago i had played a little bit around with Aether.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Well I have spent an unhappy few hours playing with Aether. Is there a tutorial or a blog anywhere? The demos don't compile, the classes they name don't seem to exist. I'll keep on for a few more hours before smacking my head on the desk :) – Phil Rice Jun 05 '12 at 20:09
  • Yes take a look inside: https://github.com/sonatype/sonatype-aether/tree/master/aether-demo – khmarbaise Jun 06 '12 at 12:07
  • Here's a some explanation of how to use the code, with a link to an example plugin that does this: http://www.sonatype.com/people/2011/01/how-to-use-aether-in-maven-plugins/ – Noremac Apr 18 '13 at 14:57
  • https://maven.apache.org/resolver/maven-resolver-api/ – phreed May 13 '22 at 23:07