Question

How do I check from within a Mojo if an artifact already exists in the local repository?

I'm installing large binaries into the local Maven repository and I need to know if they already exist before attempting to download them.

Was it helpful?

Solution

Solved with the help of http://docs.codehaus.org/display/MAVENUSER/Mojo+Developer+Cookbook

/**
 * The local maven repository.
 *
 * @parameter expression="${localRepository}"
 * @required
 * @readonly
 */
@SuppressWarnings("UWF_UNWRITTEN_FIELD")
private ArtifactRepository localRepository;
/**
 * @parameter default-value="${project.remoteArtifactRepositories}"
 * @required
 * @readonly
 */
private List<?> remoteRepositories;
/**
 * Resolves Artifacts in the local repository.
 * 
 * @component
 */
private ArtifactResolver artifactResolver;
/**
 * @component
 */
private ArtifactFactory artifactFactory;
[...]
Artifact artifact = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, packagingType, classifier);
boolean artifactExists;
try
{
  // Downloads the remote artifact, if necessary
  artifactResolver.resolve(artifact, remoteRepositories, localRepository);
  artifactExists = true;
}
catch (ArtifactResolutionException e)
{
  throw new MojoExecutionException("", e);
}
catch (ArtifactNotFoundException e)
{
  artifactExists = false;
}
if (artifactExists)
  System.out.println("Artifact found at: " + artifact.getFile());

If you want to check if a remote artifact exists without downloading it, you can use the Aether library to do the following (based on http://dev.eclipse.org/mhonarc/lists/aether-users/msg00127.html):

MavenDefaultLayout defaultLayout = new MavenDefaultLayout();
RemoteRepository centralRepository = new RemoteRepository.Builder("central", "default", "http://repo1.maven.org/maven2/").build();
URI centralUri = URI.create(centralRepository.getUrl());
URI artifactUri = centralUri.resolve(defaultLayout.getPath(artifact));
HttpURLConnection connection = (HttpURLConnection) artifactUri.toURL().openConnection();
connection.setRequestMethod("HEAD");
connection.connect();
boolean artifactExists = connection.getResponseCode() != 404;

OTHER TIPS

Since the answer which is accepted as correct, is no longer pointing to valid URL-s and because I know a better way, I am posting a new answer.

There's the wagon-maven-plugin, which has an exist goal. The documentation is a bit inaccurate, but you can use that.

Code-wise, you can have a look at the DefaultWagonDownload class' exists method:

/**
 * 
 * @param wagon - a Wagon instance
 * @param resource - Remote resource to check
 * @throws WagonException
 */
public boolean exists( Wagon wagon, String resource )
    throws WagonException
{
    return wagon.resourceExists( resource );
}

If you expect your artifacts being present in a remote maven repository, I'd suggest you simply use the copy mojo of the maven-dependency-plugin.

It will use normal maven resolution mechanism for retrieving artifacts so will not download something that is already in the local repository.

In a plugin, when using maven 2 (not sure about maven3) you can use the mojo executor to call a mojo from within you code easily.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top