I have to admit I have never written a Maven plugin before and saw your question as an good learning exercise. I borrowed heavily from the Maven Guide to Developing Java Plugins and the source code from the maven-dependency-plugin.
I may be wrong about this but I do not think there is a direct mapping between artifact and repository that is stored anywhere by Maven.
I have been a Maven user for sometime and you often see Maven querying every remote repository to ascertain the location of a given artifact. Therefore, in my code below you will not get a single URL for an artifact you will get as many URLs as there are remote repositories. You could always extend this code to attempt to download the artifact and retaining the URLs where the download is successful.
I hope this helps.
package sample.plugin;
import java.util.List;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.artifact.InvalidDependencyVersionException;
/**
* Says "Hi" to the user.
* @goal sayhi
*/
public class GreetingMojo extends AbstractMojo {
/**
* @parameter expression="${localRepository}"
* @readonly
* @required
*/
protected ArtifactRepository local;
/**
* @parameter expression="${project.remoteArtifactRepositories}"
* @readonly
* @required
*/
protected List<ArtifactRepository> remoteRepos;
/**
* @component role="org.apache.maven.project.MavenProjectBuilder"
* @required
* @readonly
*/
protected MavenProjectBuilder mavenProjectBuilder;
/**
* @component
*/
protected ArtifactFactory factory;
/**
* @component
*/
protected MavenProject project;
public void execute() throws MojoExecutionException {
try {
resolveDependencies(project);
} catch (Exception ex) {
getLog().error(ex);
}
}
private void resolveDependencies(MavenProject theProject)
throws ArtifactResolutionException, ArtifactNotFoundException, InvalidDependencyVersionException, ProjectBuildingException {
Set<Artifact> artifacts = theProject.createArtifacts(this.factory, Artifact.SCOPE_TEST,
new ScopeArtifactFilter(Artifact.SCOPE_TEST));
for (Artifact a : artifacts) {
System.out.printf("%s : %s : %s\n", a.getGroupId(), a.getArtifactId(), a.getVersion());
for (ArtifactRepository r : remoteRepos) {
System.out.printf("%s/%s\n", r.getUrl(), r.pathOf(a));
}
System.out.println();
Artifact pomArtifact = this.factory.createArtifact(a.getGroupId(), a.getArtifactId(), a.getVersion(), "", "pom");
MavenProject pomProject = mavenProjectBuilder.buildFromRepository(pomArtifact, remoteRepos, local);
resolveDependencies(pomProject);
}
}
}