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.plugin.AbstractMojo;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
27 import org.apache.maven.plugin.descriptor.PluginDescriptor;
28 import org.apache.maven.project.MavenProject;
29 import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
30 import org.apache.maven.tools.plugin.PluginToolsRequest;
31 import org.apache.maven.tools.plugin.extractor.ExtractionException;
32 import org.apache.maven.tools.plugin.generator.Generator;
33 import org.apache.maven.tools.plugin.generator.GeneratorException;
34 import org.apache.maven.tools.plugin.generator.GeneratorUtils;
35 import org.apache.maven.tools.plugin.scanner.MojoScanner;
36 import org.codehaus.plexus.util.ReaderFactory;
37
38 import java.io.File;
39 import java.util.List;
40 import java.util.Set;
41
42
43
44
45
46
47
48
49 public abstract class AbstractGeneratorMojo
50 extends AbstractMojo
51 {
52
53
54
55
56
57
58
59 protected MavenProject project;
60
61
62
63
64
65
66
67 protected MojoScanner mojoScanner;
68
69
70
71
72
73
74
75 protected String encoding;
76
77
78
79
80
81
82 protected String goalPrefix;
83
84
85
86
87
88
89
90
91
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
118
119 protected Set<String> extractors;
120
121
122
123
124
125
126
127 protected boolean skip;
128
129
130
131
132
133
134
135
136
137 protected Set<Artifact> dependencies;
138
139
140
141
142
143
144
145
146
147 protected List<ArtifactRepository> remoteRepos;
148
149
150
151
152
153
154
155
156
157 protected ArtifactRepository local;
158
159
160
161
162 protected abstract File getOutputDirectory();
163
164
165
166
167 protected abstract Generator createGenerator();
168
169
170
171
172 public void execute()
173 throws MojoExecutionException
174 {
175 if ( !"maven-plugin".equals( project.getPackaging() ) )
176 {
177 return;
178 }
179 if ( skip )
180 {
181 getLog().warn( "Execution skipped" );
182 return;
183 }
184
185 if ( project.getArtifactId().toLowerCase().startsWith( "maven-" )
186 && project.getArtifactId().toLowerCase().endsWith( "-plugin" ) && !"org.apache.maven.plugins".equals(
187 project.getGroupId() ) )
188 {
189 getLog().error( "\n\nArtifact Ids of the format maven-___-plugin are reserved for \n"
190 + "plugins in the Group Id org.apache.maven.plugins\n"
191 + "Please change your artifactId to the format ___-maven-plugin\n"
192 + "In the future this error will break the build.\n\n" );
193 }
194
195 String defaultGoalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId( project.getArtifactId() );
196 if ( goalPrefix == null )
197 {
198 goalPrefix = defaultGoalPrefix;
199 }
200 else if ( !goalPrefix.equals( defaultGoalPrefix ) )
201 {
202 getLog().warn(
203 "\n\nGoal prefix is specified as: '" + goalPrefix + "'. " + "Maven currently expects it to be '"
204 + defaultGoalPrefix + "'.\n" );
205 }
206
207 mojoScanner.setActiveExtractors( extractors );
208
209
210 PluginDescriptor pluginDescriptor = new PluginDescriptor();
211
212 pluginDescriptor.setGroupId( project.getGroupId() );
213
214 pluginDescriptor.setArtifactId( project.getArtifactId() );
215
216 pluginDescriptor.setVersion( project.getVersion() );
217
218 pluginDescriptor.setGoalPrefix( goalPrefix );
219
220 pluginDescriptor.setName( project.getName() );
221
222 pluginDescriptor.setDescription( project.getDescription() );
223
224 if ( encoding == null || encoding.length() < 1 )
225 {
226 getLog().warn( "Using platform encoding (" + ReaderFactory.FILE_ENCODING
227 + " actually) to read mojo metadata, i.e. build is platform dependent!" );
228 }
229 else
230 {
231 getLog().info( "Using '" + encoding + "' encoding to read mojo metadata." );
232 }
233
234 try
235 {
236 pluginDescriptor.setDependencies( GeneratorUtils.toComponentDependencies( project.getRuntimeDependencies() ) );
237
238 PluginToolsRequest request = new DefaultPluginToolsRequest( project, pluginDescriptor );
239 request.setEncoding( encoding );
240 request.setSkipErrorNoDescriptorsFound( skipErrorNoDescriptorsFound );
241 request.setDependencies( dependencies );
242 request.setLocal( this.local );
243 request.setRemoteRepos( this.remoteRepos );
244
245 mojoScanner.populatePluginDescriptor( request );
246
247 getOutputDirectory().mkdirs();
248
249 createGenerator().execute( getOutputDirectory(), request );
250 }
251 catch ( GeneratorException e )
252 {
253 throw new MojoExecutionException( "Error writing plugin descriptor", e );
254 }
255 catch ( InvalidPluginDescriptorException e )
256 {
257 throw new MojoExecutionException( "Error extracting plugin descriptor: \'" + e.getLocalizedMessage() + "\'",
258 e );
259 }
260 catch ( ExtractionException e )
261 {
262 throw new MojoExecutionException( "Error extracting plugin descriptor: \'" + e.getLocalizedMessage() + "\'",
263 e );
264 }
265 catch ( LinkageError e )
266 {
267 throw new MojoExecutionException( "The API of the mojo scanner is not compatible with this plugin version."
268 + " Please check the plugin dependencies configured in the POM and ensure the versions match.",
269 e );
270 }
271 }
272
273 }