2
votes

Short: Given (groupId, artifactId, version, repository URL) can I programmatically have Maven resolve the artifact URL?

Long: Given (groupId, artifactId, version, repository URL), one can download a Maven artifact. Usually the URL to the artifact looks like:

scheme://{repository}/{groupId}/{artifactId}/{version}/{artifactId}-{version}.jar

For (org.apache.maven, maven-core, 3.0.0, http://repo.maven.org/maven2), the artifact URL resolves to:

http://repo.maven.org/maven2/org/apache/maven/maven-core/3.0.0/maven-core-3.0.0.jar

Using the inferred pattern above, I can use string concatenations to produce the artifact URL for any given (groupId, artifactId, version, repository URL). However, I do not want to rely on this inferred pattern since it is subject to change in future versions of Maven.

Can I programmatically have Maven resolve the artifact URL for me given (groupId, artifactId, version, repository URL)?

3

3 Answers

2
votes

Many Maven repositories have an API that you can use to search for artifacts. For example, the central Maven repository has one, for which the documentation is here.

For your example, the API of the central repository serves the following URL for linking directly to a search:

http://search.maven.org/#search|ga|1|g:"org.apache.maven" AND a:"maven-core" AND v:"3.0"

And the following URL for returning XML data (or JSON, by changing the last parameter):

http://search.maven.org/solrsearch/select?q=g:"org.apache.maven" AND a:"maven-core" AND v:"3.0"&wt=xml

In this case, you still have to create a URL for the JAR manually (or scrape the search results for it), but other repositories (like Nexus) provide this in the API results directly.

1
votes

Something like ivy would be more easily embedded in the manner you are suggesting. Ivy can use the same layout as maven, or a custom layout.

0
votes

It would be nice to have a uri scheme for maven. A maven uri would simply be mvn:group:artifact[:packaging]:version . Alternatively, you could graft it onto the urn scheme.