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