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