1 package org.apache.maven.plugin.plugin;
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.filter.ArtifactFilter;
25 import org.apache.maven.artifact.resolver.filter.IncludesArtifactFilter;
26 import org.apache.maven.plugin.AbstractMojo;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
29 import org.apache.maven.plugin.descriptor.PluginDescriptor;
30 import org.apache.maven.plugins.annotations.Component;
31 import org.apache.maven.plugins.annotations.Parameter;
32 import org.apache.maven.project.MavenProject;
33 import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
34 import org.apache.maven.tools.plugin.PluginToolsRequest;
35 import org.apache.maven.tools.plugin.extractor.ExtractionException;
36 import org.apache.maven.tools.plugin.generator.Generator;
37 import org.apache.maven.tools.plugin.generator.GeneratorException;
38 import org.apache.maven.tools.plugin.generator.GeneratorUtils;
39 import org.apache.maven.tools.plugin.scanner.MojoScanner;
40 import org.codehaus.plexus.component.repository.ComponentDependency;
41 import org.codehaus.plexus.util.ReaderFactory;
42
43 import java.io.File;
44 import java.util.Arrays;
45 import java.util.LinkedHashSet;
46 import java.util.List;
47 import java.util.Set;
48
49
50
51
52
53
54
55 public abstract class AbstractGeneratorMojo
56 extends AbstractMojo
57 {
58
59
60
61 @Parameter( defaultValue = "${project}", readonly = true )
62 protected MavenProject project;
63
64
65
66
67 @Component
68 protected MojoScanner mojoScanner;
69
70
71
72
73
74
75 @Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}" )
76 protected String encoding;
77
78
79
80
81 @Parameter
82 protected String goalPrefix;
83
84
85
86
87
88
89
90
91 @Parameter( property = "maven.plugin.skipErrorNoDescriptorsFound", defaultValue = "false" )
92 protected boolean skipErrorNoDescriptorsFound;
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 @Parameter
118 protected Set<String> extractors;
119
120
121
122
123
124
125 @Parameter( defaultValue = "false", property = "maven.plugin.skip" )
126 protected boolean skip;
127
128
129
130
131
132
133 @Parameter( defaultValue = "${project.artifacts}", required = true, readonly = true )
134 protected Set<Artifact> dependencies;
135
136
137
138
139
140
141
142
143 @Parameter
144 private List<String> mojoDependencies;
145
146
147
148
149
150
151 @Parameter( defaultValue = "${project.remoteArtifactRepositories}", required = true, readonly = true )
152 protected List<ArtifactRepository> remoteRepos;
153
154
155
156
157
158
159 @Parameter( defaultValue = "${localRepository}", required = true, readonly = true )
160 protected ArtifactRepository local;
161
162
163
164
165
166
167 @Parameter
168 protected List<String> packagingTypes = Arrays.asList( "maven-plugin" );
169
170
171
172
173 protected abstract File getOutputDirectory();
174
175
176
177
178 protected abstract Generator createGenerator();
179
180
181
182
183 public void execute()
184 throws MojoExecutionException
185 {
186 if ( !packagingTypes.contains( project.getPackaging() ) )
187 {
188 getLog().warn( "Unsupported packaging type " + project.getPackaging() + ", execution skipped" );
189 return;
190 }
191 if ( skip )
192 {
193 getLog().warn( "Execution skipped" );
194 return;
195 }
196
197 if ( project.getArtifactId().toLowerCase().startsWith( "maven-" )
198 && project.getArtifactId().toLowerCase().endsWith( "-plugin" ) && !"org.apache.maven.plugins".equals(
199 project.getGroupId() ) )
200 {
201 getLog().error( "\n\nArtifact Ids of the format maven-___-plugin are reserved for \n"
202 + "plugins in the Group Id org.apache.maven.plugins\n"
203 + "Please change your artifactId to the format ___-maven-plugin\n"
204 + "In the future this error will break the build.\n\n" );
205 }
206
207 String defaultGoalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId( project.getArtifactId() );
208 if ( goalPrefix == null )
209 {
210 goalPrefix = defaultGoalPrefix;
211 }
212 else if ( !goalPrefix.equals( defaultGoalPrefix ) )
213 {
214 getLog().warn(
215 "\n\nGoal prefix is specified as: '" + goalPrefix + "'. " + "Maven currently expects it to be '"
216 + defaultGoalPrefix + "'.\n" );
217 }
218
219 mojoScanner.setActiveExtractors( extractors );
220
221
222 PluginDescriptor pluginDescriptor = new PluginDescriptor();
223
224 pluginDescriptor.setGroupId( project.getGroupId() );
225
226 pluginDescriptor.setArtifactId( project.getArtifactId() );
227
228 pluginDescriptor.setVersion( project.getVersion() );
229
230 pluginDescriptor.setGoalPrefix( goalPrefix );
231
232 pluginDescriptor.setName( project.getName() );
233
234 pluginDescriptor.setDescription( project.getDescription() );
235
236 if ( encoding == null || encoding.length() < 1 )
237 {
238 getLog().warn( "Using platform encoding (" + ReaderFactory.FILE_ENCODING
239 + " actually) to read mojo source files, i.e. build is platform dependent!" );
240 }
241 else
242 {
243 getLog().info( "Using '" + encoding + "' encoding to read mojo source files." );
244 }
245
246 Set<Artifact> requestDependencies;
247 if ( mojoDependencies == null )
248 {
249 requestDependencies = dependencies;
250 }
251 else if ( mojoDependencies == null )
252 {
253 requestDependencies = null;
254 }
255 else
256 {
257 requestDependencies = new LinkedHashSet<Artifact>();
258
259 ArtifactFilter filter = new IncludesArtifactFilter( mojoDependencies );
260
261 for ( Artifact artifact : dependencies )
262 {
263 if ( filter.include( artifact ) )
264 {
265 requestDependencies.add( artifact );
266 }
267 }
268 }
269
270
271 try
272 {
273 List<ComponentDependency> deps = GeneratorUtils.toComponentDependencies( project.getRuntimeDependencies() );
274 pluginDescriptor.setDependencies( deps );
275
276 PluginToolsRequest request = new DefaultPluginToolsRequest( project, pluginDescriptor );
277 request.setEncoding( encoding );
278 request.setSkipErrorNoDescriptorsFound( skipErrorNoDescriptorsFound );
279 request.setDependencies( requestDependencies );
280 request.setLocal( this.local );
281 request.setRemoteRepos( this.remoteRepos );
282
283 mojoScanner.populatePluginDescriptor( request );
284
285 getOutputDirectory().mkdirs();
286
287 createGenerator().execute( getOutputDirectory(), request );
288 }
289 catch ( GeneratorException e )
290 {
291 throw new MojoExecutionException( "Error writing plugin descriptor", e );
292 }
293 catch ( InvalidPluginDescriptorException e )
294 {
295 throw new MojoExecutionException( "Error extracting plugin descriptor: \'" + e.getLocalizedMessage() + "\'",
296 e );
297 }
298 catch ( ExtractionException e )
299 {
300 throw new MojoExecutionException( "Error extracting plugin descriptor: \'" + e.getLocalizedMessage() + "\'",
301 e );
302 }
303 catch ( LinkageError e )
304 {
305 throw new MojoExecutionException( "The API of the mojo scanner is not compatible with this plugin version."
306 + " Please check the plugin dependencies configured in the POM and ensure the versions match.", e );
307 }
308 }
309
310 }