1 package org.apache.maven.plugin.dependency.resolvers;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
5 * agreements. See the NOTICE file distributed with this work for additional information regarding
6 * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance with the License. You may obtain a
8 * copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
15 * the License.
16 */
17
18 import java.io.FileWriter;
19 import java.io.IOException;
20 import java.io.Writer;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
27 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.dependency.AbstractResolveMojo;
30 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
31 import org.apache.maven.project.ProjectBuildingException;
32 import org.apache.maven.project.artifact.InvalidDependencyVersionException;
33 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
34 import org.codehaus.plexus.util.IOUtil;
35
36 /**
37 * Goal that resolves all project plugins and reports and their dependencies.
38 *
39 * @goal resolve-plugins
40 * @phase generate-sources
41 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
42 * @version $Id: ResolvePluginsMojo.java 1085777 2011-03-26 18:13:19Z hboutemy $
43 * @since 2.0
44 */
45 public class ResolvePluginsMojo
46 extends AbstractResolveMojo
47 {
48
49 /**
50 * Remote repositories which will be searched for plugins.
51 *
52 * @parameter expression="${project.pluginArtifactRepositories}"
53 * @required
54 * @readonly
55 */
56 private List remotePluginRepositories;
57
58 /**
59 * If we should exclude transitive dependencies
60 *
61 * @parameter expression="${excludeTransitive}" default-value="false"
62 */
63 private boolean excludeTransitive;
64
65 /**
66 * Main entry into mojo. Gets the list of dependencies and iterates through
67 * displaying the resolved version.
68 *
69 * @throws MojoExecutionException
70 * with a message if an error occurs.
71 *
72 */
73 public void execute()
74 throws MojoExecutionException
75 {
76 Writer outputWriter = null;
77
78 try
79 {
80 Set<Artifact> plugins = resolvePluginArtifacts();
81
82 if ( this.outputFile != null )
83 {
84 outputFile.getParentFile().mkdirs();
85
86 outputWriter = new FileWriter( outputFile );
87 }
88
89 for ( Artifact plugin : plugins )
90 {
91 String logStr = "Plugin Resolved: " + DependencyUtil.getFormattedFileName( plugin, false );
92 if ( !silent )
93 {
94 this.getLog().info( logStr );
95 }
96
97 if ( outputWriter != null )
98 {
99 outputWriter.write( logStr );
100 outputWriter.write( "\n" );
101 }
102
103 if ( !excludeTransitive )
104 {
105 for ( Artifact artifact : resolveArtifactDependencies( plugin ) )
106 {
107 logStr =
108 " Plugin Dependency Resolved: " + DependencyUtil.getFormattedFileName( artifact, false );
109
110 if ( !silent )
111 {
112 this.getLog().info( logStr );
113 }
114
115 if ( outputWriter != null )
116 {
117 outputWriter.write( logStr );
118 outputWriter.write( "\n" );
119 }
120 }
121 }
122 }
123 }
124 catch ( IOException e )
125 {
126 throw new MojoExecutionException( "Nested:", e );
127 }
128 catch ( ArtifactResolutionException e )
129 {
130 throw new MojoExecutionException( "Nested:", e );
131 }
132 catch ( ArtifactNotFoundException e )
133 {
134 throw new MojoExecutionException( "Nested:", e );
135 }
136 catch ( ProjectBuildingException e )
137 {
138 throw new MojoExecutionException( "Nested:", e );
139 }
140 catch ( InvalidDependencyVersionException e )
141 {
142 throw new MojoExecutionException( "Nested:", e );
143 }
144 finally
145 {
146 IOUtil.close( outputWriter );
147 }
148
149 }
150
151 /**
152 * This method resolves the plugin artifacts from the project.
153 *
154 * @param project
155 * The POM.
156 * @param artifactFactory
157 * component to build artifact objects.
158 * @param localRepository
159 * where to resolve artifacts.
160 * @param remotePluginRepositories
161 * list of remote repositories used to resolve plugins.
162 * @param artifactResolver
163 * component used to resolve artifacts.
164 *
165 * @return set of resolved plugin artifacts.
166 *
167 * @throws ArtifactResolutionException
168 * @throws ArtifactNotFoundException
169 */
170 protected Set<Artifact> resolvePluginArtifacts()
171 throws ArtifactResolutionException, ArtifactNotFoundException
172 {
173 Set<Artifact> plugins = project.getPluginArtifacts();
174 Set<Artifact> reports = project.getReportArtifacts();
175
176 Set<Artifact> artifacts = new HashSet<Artifact>();
177 artifacts.addAll( reports );
178 artifacts.addAll( plugins );
179
180 for ( Artifact artifact : artifacts )
181 {
182 // resolve the new artifact
183 this.resolver.resolve( artifact, this.remotePluginRepositories, this.getLocal() );
184 }
185 return artifacts;
186 }
187
188 protected ArtifactsFilter getMarkedArtifactFilter()
189 {
190 return null;
191 }
192 }