1 package org.apache.maven.plugins.dependency.resolvers;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.util.LinkedHashSet;
24 import java.util.List;
25 import java.util.Set;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.repository.ArtifactRepository;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugins.annotations.LifecyclePhase;
31 import org.apache.maven.plugins.annotations.Mojo;
32 import org.apache.maven.plugins.annotations.Parameter;
33 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
34 import org.apache.maven.project.DefaultProjectBuildingRequest;
35 import org.apache.maven.project.ProjectBuildingRequest;
36 import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
37 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
38 import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
39 import org.apache.maven.shared.artifact.resolve.ArtifactResolverException;
40 import org.apache.maven.shared.dependencies.DefaultDependableCoordinate;
41 import org.apache.maven.shared.dependencies.resolve.DependencyResolverException;
42
43
44
45
46
47
48
49 @Mojo( name = "resolve-plugins", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true )
50 public class ResolvePluginsMojo
51 extends AbstractResolveMojo
52 {
53
54
55
56
57 @Parameter( defaultValue = "${project.pluginArtifactRepositories}", readonly = true, required = true )
58 private List<ArtifactRepository> remotePluginRepositories;
59
60
61
62
63
64
65 @Override
66 protected void doExecute()
67 throws MojoExecutionException
68 {
69 try
70 {
71
72 final Set<Artifact> plugins = resolvePluginArtifacts();
73
74 StringBuilder sb = new StringBuilder();
75 sb.append( "\n" );
76 sb.append( "The following plugins have been resolved:\n" );
77 if ( plugins == null || plugins.isEmpty() )
78 {
79 sb.append( " none\n" );
80 }
81 else
82 {
83 for ( Artifact plugin : plugins )
84 {
85 String artifactFilename = null;
86 if ( outputAbsoluteArtifactFilename )
87 {
88 try
89 {
90
91 artifactFilename = plugin.getFile().getAbsoluteFile().getPath();
92 }
93 catch ( NullPointerException e )
94 {
95
96 artifactFilename = null;
97 }
98 }
99
100 String id = plugin.toString();
101 sb.append( " " + id + ( outputAbsoluteArtifactFilename ? ":" + artifactFilename : "" ) + "\n" );
102
103 if ( !excludeTransitive )
104 {
105 DefaultDependableCoordinate pluginCoordinate = new DefaultDependableCoordinate();
106 pluginCoordinate.setGroupId( plugin.getGroupId() );
107 pluginCoordinate.setArtifactId( plugin.getArtifactId() );
108 pluginCoordinate.setVersion( plugin.getVersion() );
109
110 for ( final Artifact artifact : resolveArtifactDependencies( pluginCoordinate ) )
111 {
112 artifactFilename = null;
113 if ( outputAbsoluteArtifactFilename )
114 {
115 try
116 {
117
118 artifactFilename = artifact.getFile().getAbsoluteFile().getPath();
119 }
120 catch ( NullPointerException e )
121 {
122
123 artifactFilename = null;
124 }
125 }
126
127 id = artifact.toString();
128 sb.append( " " + id + ( outputAbsoluteArtifactFilename ? ":" + artifactFilename : "" )
129 + "\n" );
130 }
131 }
132 }
133 sb.append( "\n" );
134
135 String output = sb.toString();
136 if ( outputFile == null )
137 {
138 DependencyUtil.log( output, getLog() );
139 }
140 else
141 {
142 DependencyUtil.write( output, outputFile, appendOutput, getLog() );
143 }
144 }
145 }
146 catch ( final IOException e )
147 {
148 throw new MojoExecutionException( e.getMessage(), e );
149 }
150 catch ( final ArtifactFilterException e )
151 {
152 throw new MojoExecutionException( e.getMessage(), e );
153 }
154 catch ( ArtifactResolverException e )
155 {
156 throw new MojoExecutionException( e.getMessage(), e );
157 }
158 catch ( DependencyResolverException e )
159 {
160 throw new MojoExecutionException( e.getMessage(), e );
161 }
162 }
163
164
165
166
167
168
169
170
171 protected Set<Artifact> resolvePluginArtifacts()
172 throws ArtifactFilterException, ArtifactResolverException
173 {
174 final Set<Artifact> plugins = getProject().getPluginArtifacts();
175 final Set<Artifact> reports = getProject().getReportArtifacts();
176
177 Set<Artifact> artifacts = new LinkedHashSet<Artifact>();
178 artifacts.addAll( reports );
179 artifacts.addAll( plugins );
180
181 final FilterArtifacts filter = getPluginArtifactsFilter();
182 artifacts = filter.filter( artifacts );
183
184 Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>( artifacts.size() );
185
186 for ( final Artifact artifact : new LinkedHashSet<Artifact>( artifacts ) )
187 {
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 ProjectBuildingRequest buildingRequest =
203 new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
204
205 buildingRequest.setRemoteRepositories( this.remotePluginRepositories );
206
207
208 resolvedArtifacts.add( getArtifactResolver().resolveArtifact( buildingRequest, artifact ).getArtifact() );
209 }
210 return artifacts;
211 }
212
213 @Override
214 protected ArtifactsFilter getMarkedArtifactFilter()
215 {
216 return null;
217 }
218 }