1 package org.apache.maven.plugin.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.FileWriter;
23 import java.io.IOException;
24 import java.io.Writer;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
32 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugin.dependency.AbstractResolveMojo;
35 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
36 import org.apache.maven.plugins.annotations.LifecyclePhase;
37 import org.apache.maven.plugins.annotations.Mojo;
38 import org.apache.maven.plugins.annotations.Parameter;
39 import org.apache.maven.project.ProjectBuildingException;
40 import org.apache.maven.project.artifact.InvalidDependencyVersionException;
41 import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
42 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
43 import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
44 import org.codehaus.plexus.util.IOUtil;
45
46
47
48
49
50
51
52
53 @Mojo( name = "resolve-plugins", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true )
54 public class ResolvePluginsMojo
55 extends AbstractResolveMojo
56 {
57
58
59
60
61 @Parameter( defaultValue = "${project.pluginArtifactRepositories}", readonly = true, required = true )
62 private List<ArtifactRepository> remotePluginRepositories;
63
64
65
66
67 @Parameter( property = "excludeTransitive", defaultValue = "false" )
68 private boolean excludeTransitive;
69
70
71
72
73
74
75
76 protected void doExecute()
77 throws MojoExecutionException
78 {
79 Writer outputWriter = null;
80
81 try
82 {
83 final Set<Artifact> plugins = resolvePluginArtifacts();
84
85 if ( this.outputFile != null )
86 {
87 outputFile.getParentFile()
88 .mkdirs();
89
90 outputWriter = new FileWriter( outputFile );
91 }
92
93 for ( final Artifact plugin : plugins )
94 {
95 String logStr = "Plugin Resolved: " + DependencyUtil.getFormattedFileName( plugin, false );
96 if ( !silent )
97 {
98 this.getLog()
99 .info( logStr );
100 }
101
102 if ( outputWriter != null )
103 {
104 outputWriter.write( logStr );
105 outputWriter.write( "\n" );
106 }
107
108 if ( !excludeTransitive )
109 {
110 for ( final Artifact artifact : resolveArtifactDependencies( plugin ) )
111 {
112 logStr =
113 " Plugin Dependency Resolved: " + DependencyUtil.getFormattedFileName( artifact, false );
114
115 if ( !silent )
116 {
117 this.getLog()
118 .info( logStr );
119 }
120
121 if ( outputWriter != null )
122 {
123 outputWriter.write( logStr );
124 outputWriter.write( "\n" );
125 }
126 }
127 }
128 }
129 }
130 catch ( final IOException e )
131 {
132 throw new MojoExecutionException( "Nested:", e );
133 }
134 catch ( final ArtifactResolutionException e )
135 {
136 throw new MojoExecutionException( "Nested:", e );
137 }
138 catch ( final ArtifactNotFoundException e )
139 {
140 throw new MojoExecutionException( "Nested:", e );
141 }
142 catch ( final ProjectBuildingException e )
143 {
144 throw new MojoExecutionException( "Nested:", e );
145 }
146 catch ( final InvalidDependencyVersionException e )
147 {
148 throw new MojoExecutionException( "Nested:", e );
149 }
150 catch ( final ArtifactFilterException e )
151 {
152 throw new MojoExecutionException( "Nested:", e );
153 }
154 finally
155 {
156 IOUtil.close( outputWriter );
157 }
158
159 }
160
161
162
163
164
165
166
167
168
169 @SuppressWarnings( "unchecked" )
170 protected Set<Artifact> resolvePluginArtifacts()
171 throws ArtifactResolutionException, ArtifactNotFoundException, ArtifactFilterException
172 {
173 final Set<Artifact> plugins = project.getPluginArtifacts();
174 final Set<Artifact> reports = project.getReportArtifacts();
175
176 Set<Artifact> artifacts = new HashSet<Artifact>();
177 artifacts.addAll( reports );
178 artifacts.addAll( plugins );
179
180 final FilterArtifacts filter = getPluginArtifactsFilter();
181 artifacts = filter.filter( artifacts );
182
183
184 for ( final Artifact artifact : new HashSet<Artifact>( artifacts ) )
185 {
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 this.resolver.resolve( artifact, this.remotePluginRepositories, this.getLocal() );
203 }
204 return artifacts;
205 }
206
207 @Override
208 protected ArtifactsFilter getMarkedArtifactFilter()
209 {
210 return null;
211 }
212 }