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