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