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