1 package org.apache.maven.resolver.examples.maven;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.List;
23
24 import org.apache.maven.plugin.AbstractMojo;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugin.MojoFailureException;
27 import org.eclipse.aether.RepositorySystem;
28 import org.eclipse.aether.RepositorySystemSession;
29 import org.eclipse.aether.artifact.Artifact;
30 import org.eclipse.aether.artifact.DefaultArtifact;
31 import org.eclipse.aether.repository.RemoteRepository;
32 import org.eclipse.aether.resolution.ArtifactRequest;
33 import org.eclipse.aether.resolution.ArtifactResolutionException;
34 import org.eclipse.aether.resolution.ArtifactResult;
35
36
37
38
39
40
41 public class ResolveArtifactMojo
42 extends AbstractMojo
43 {
44
45
46
47
48
49
50 private RepositorySystem repoSystem;
51
52
53
54
55
56
57
58 private RepositorySystemSession repoSession;
59
60
61
62
63
64
65
66 private List<RemoteRepository> remoteRepos;
67
68
69
70
71
72
73 private String artifactCoords;
74
75 public void execute()
76 throws MojoExecutionException, MojoFailureException
77 {
78 Artifact artifact;
79 try
80 {
81 artifact = new DefaultArtifact( artifactCoords );
82 }
83 catch ( IllegalArgumentException e )
84 {
85 throw new MojoFailureException( e.getMessage(), e );
86 }
87
88 ArtifactRequest request = new ArtifactRequest();
89 request.setArtifact( artifact );
90 request.setRepositories( remoteRepos );
91
92 getLog().info( "Resolving artifact " + artifact + " from " + remoteRepos );
93
94 ArtifactResult result;
95 try
96 {
97 result = repoSystem.resolveArtifact( repoSession, request );
98 }
99 catch ( ArtifactResolutionException e )
100 {
101 throw new MojoExecutionException( e.getMessage(), e );
102 }
103
104 getLog().info( "Resolved artifact " + artifact + " to " + result.getArtifact().getFile() + " from "
105 + result.getRepository() );
106 }
107
108 }