Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have to admit I have <em>never</em> written a Maven plugin before and saw your question as an good learning exercise. I borrowed heavily from the <a href="http://maven.apache.org/guides/plugin/guide-java-plugin-development.html" rel="noreferrer">Maven Guide to Developing Java Plugins</a> and the source code from the <a href="http://maven.apache.org/plugins/maven-dependency-plugin/source-repository.html" rel="noreferrer"><strong>maven-dependency-plugin</strong></a>.</p> <p>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.</p> <p>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.</p> <p>I hope this helps.</p> <pre><code>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&lt;ArtifactRepository&gt; 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&lt;Artifact&gt; 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); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload