1 | |
package org.apache.maven.tools.plugin.extractor.ant; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.HashMap; |
25 | |
import java.util.List; |
26 | |
import java.util.Map; |
27 | |
import java.util.Set; |
28 | |
|
29 | |
import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException; |
30 | |
import org.apache.maven.plugin.descriptor.MojoDescriptor; |
31 | |
import org.apache.maven.plugin.descriptor.Parameter; |
32 | |
import org.apache.maven.plugin.tools.model.PluginMetadataParseException; |
33 | |
import org.apache.maven.plugin.tools.model.PluginMetadataParser; |
34 | |
import org.apache.maven.project.MavenProject; |
35 | |
import org.apache.maven.project.path.PathTranslator; |
36 | |
import org.apache.maven.tools.plugin.PluginToolsRequest; |
37 | |
import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor; |
38 | |
import org.apache.maven.tools.plugin.extractor.ExtractionException; |
39 | |
import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor; |
40 | |
import org.codehaus.plexus.component.annotations.Component; |
41 | |
import org.codehaus.plexus.component.repository.ComponentRequirement; |
42 | |
import org.codehaus.plexus.util.StringUtils; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
@Component( role = MojoDescriptorExtractor.class, hint = "ant" ) |
50 | 1 | public class AntMojoDescriptorExtractor |
51 | |
extends AbstractScriptedMojoDescriptorExtractor |
52 | |
implements MojoDescriptorExtractor |
53 | |
{ |
54 | |
|
55 | |
private static final String METADATA_FILE_EXTENSION = ".mojos.xml"; |
56 | |
|
57 | |
|
58 | |
private static final String SCRIPT_FILE_EXTENSION = ".build.xml"; |
59 | |
|
60 | |
|
61 | |
protected List<MojoDescriptor> extractMojoDescriptorsFromMetadata( Map<String, Set<File>> metadataFilesKeyedByBasedir, |
62 | |
PluginToolsRequest request ) |
63 | |
throws ExtractionException, InvalidPluginDescriptorException |
64 | |
{ |
65 | 1 | List<MojoDescriptor> descriptors = new ArrayList<MojoDescriptor>(); |
66 | |
|
67 | 1 | PluginMetadataParser parser = new PluginMetadataParser(); |
68 | |
|
69 | 1 | for ( Map.Entry<String, Set<File>> entry : metadataFilesKeyedByBasedir.entrySet() ) |
70 | |
{ |
71 | 1 | String basedir = entry.getKey(); |
72 | 1 | Set<File> metadataFiles = entry.getValue(); |
73 | |
|
74 | 1 | for ( File metadataFile : metadataFiles ) |
75 | |
{ |
76 | 1 | String basename = metadataFile.getName(); |
77 | 1 | basename = basename.substring( 0, basename.length() - METADATA_FILE_EXTENSION.length() ); |
78 | |
|
79 | 1 | File scriptFile = new File( metadataFile.getParentFile(), basename + SCRIPT_FILE_EXTENSION ); |
80 | |
|
81 | 1 | if ( !scriptFile.exists() ) |
82 | |
{ |
83 | 0 | throw new InvalidPluginDescriptorException( |
84 | |
"Found orphaned plugin metadata file: " + metadataFile ); |
85 | |
} |
86 | |
|
87 | 1 | String relativePath = scriptFile.getPath().substring( basedir.length() ).replace( '\\', '/' ); |
88 | |
|
89 | 1 | if ( relativePath.startsWith( "/" ) ) |
90 | |
{ |
91 | 1 | relativePath = relativePath.substring( 1 ); |
92 | |
} |
93 | |
|
94 | |
try |
95 | |
{ |
96 | 1 | Set<MojoDescriptor> mojoDescriptors = parser.parseMojoDescriptors( metadataFile ); |
97 | |
|
98 | 1 | for ( MojoDescriptor descriptor : mojoDescriptors ) |
99 | |
{ |
100 | |
@SuppressWarnings( "unchecked" ) |
101 | 2 | Map<String, ?> paramMap = descriptor.getParameterMap(); |
102 | |
|
103 | 2 | if ( !paramMap.containsKey( "basedir" ) ) |
104 | |
{ |
105 | 2 | Parameter param = new Parameter(); |
106 | 2 | param.setName( "basedir" ); |
107 | 2 | param.setAlias( "ant.basedir" ); |
108 | 2 | param.setExpression( "${antBasedir}" ); |
109 | 2 | param.setDefaultValue( "${basedir}" ); |
110 | 2 | param.setType( "java.io.File" ); |
111 | 2 | param.setDescription( "The base directory from which to execute the Ant script." ); |
112 | 2 | param.setEditable( true ); |
113 | 2 | param.setRequired( true ); |
114 | |
|
115 | 2 | descriptor.addParameter( param ); |
116 | |
} |
117 | |
|
118 | 2 | if ( !paramMap.containsKey( "antMessageLevel" ) ) |
119 | |
{ |
120 | 2 | Parameter param = new Parameter(); |
121 | 2 | param.setName( "messageLevel" ); |
122 | 2 | param.setAlias( "ant.messageLevel" ); |
123 | 2 | param.setExpression( "${antMessageLevel}" ); |
124 | 2 | param.setDefaultValue( "info" ); |
125 | 2 | param.setType( "java.lang.String" ); |
126 | 2 | param.setDescription( "The message-level used to tune the verbosity of Ant logging." ); |
127 | 2 | param.setEditable( true ); |
128 | 2 | param.setRequired( false ); |
129 | |
|
130 | 2 | descriptor.addParameter( param ); |
131 | |
} |
132 | |
|
133 | 2 | if ( !paramMap.containsKey( "project" ) ) |
134 | |
{ |
135 | 1 | Parameter param = new Parameter(); |
136 | 1 | param.setName( "project" ); |
137 | 1 | param.setDefaultValue( "${project}" ); |
138 | 1 | param.setType( MavenProject.class.getName() ); |
139 | 1 | param.setDescription( "The current MavenProject instance, which contains classpath elements." ); |
140 | 1 | param.setEditable( false ); |
141 | 1 | param.setRequired( true ); |
142 | |
|
143 | 1 | descriptor.addParameter( param ); |
144 | |
} |
145 | |
|
146 | 2 | if ( !paramMap.containsKey( "session" ) ) |
147 | |
{ |
148 | 1 | Parameter param = new Parameter(); |
149 | 1 | param.setName( "session" ); |
150 | 1 | param.setDefaultValue( "${session}" ); |
151 | 1 | param.setType( "org.apache.maven.execution.MavenSession" ); |
152 | 1 | param.setDescription( "The current MavenSession instance, which is used for plugin-style expression resolution." ); |
153 | 1 | param.setEditable( false ); |
154 | 1 | param.setRequired( true ); |
155 | |
|
156 | 1 | descriptor.addParameter( param ); |
157 | |
} |
158 | |
|
159 | 2 | if ( !paramMap.containsKey( "mojoExecution" ) ) |
160 | |
{ |
161 | 1 | Parameter param = new Parameter(); |
162 | 1 | param.setName( "mojoExecution" ); |
163 | 1 | param.setDefaultValue( "${mojoExecution}" ); |
164 | 1 | param.setType( "org.apache.maven.plugin.MojoExecution" ); |
165 | 1 | param.setDescription( "The current Maven MojoExecution instance, which contains information about the mojo currently executing." ); |
166 | 1 | param.setEditable( false ); |
167 | 1 | param.setRequired( true ); |
168 | |
|
169 | 1 | descriptor.addParameter( param ); |
170 | |
} |
171 | |
|
172 | |
@SuppressWarnings( "unchecked" ) |
173 | 2 | List<ComponentRequirement> requirements = descriptor.getRequirements(); |
174 | 2 | Map<String, ComponentRequirement> reqMap = new HashMap<String, ComponentRequirement>(); |
175 | |
|
176 | 2 | if ( requirements != null ) |
177 | |
{ |
178 | 2 | for ( ComponentRequirement req : requirements ) |
179 | |
{ |
180 | 1 | reqMap.put( req.getRole(), req ); |
181 | |
} |
182 | |
} |
183 | |
|
184 | 2 | if ( !reqMap.containsKey( PathTranslator.class.getName() ) ) |
185 | |
{ |
186 | 1 | ComponentRequirement req = new ComponentRequirement(); |
187 | 1 | req.setRole( PathTranslator.class.getName() ); |
188 | |
|
189 | 1 | descriptor.addRequirement( req ); |
190 | |
} |
191 | |
|
192 | 2 | String implementation = relativePath; |
193 | |
|
194 | 2 | String dImpl = descriptor.getImplementation(); |
195 | 2 | if ( StringUtils.isNotEmpty( dImpl ) ) |
196 | |
{ |
197 | 2 | if ( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.equals( dImpl ) ) |
198 | |
{ |
199 | 1 | implementation = relativePath; |
200 | |
} |
201 | |
else |
202 | |
{ |
203 | 1 | implementation = |
204 | |
relativePath + dImpl.substring( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.length() ); |
205 | |
} |
206 | |
} |
207 | |
|
208 | 2 | descriptor.setImplementation( implementation ); |
209 | |
|
210 | 2 | descriptor.setLanguage( "ant-mojo" ); |
211 | 2 | descriptor.setComponentComposer( "map-oriented" ); |
212 | 2 | descriptor.setComponentConfigurator( "map-oriented" ); |
213 | |
|
214 | 2 | descriptor.setPluginDescriptor( request.getPluginDescriptor() ); |
215 | |
|
216 | 2 | descriptors.add( descriptor ); |
217 | 2 | } |
218 | |
} |
219 | 0 | catch ( PluginMetadataParseException e ) |
220 | |
{ |
221 | 0 | throw new ExtractionException( "Error extracting mojo descriptor from script: " + metadataFile, e ); |
222 | 1 | } |
223 | 1 | } |
224 | 1 | } |
225 | |
|
226 | 1 | return descriptors; |
227 | |
} |
228 | |
|
229 | |
|
230 | |
protected String getScriptFileExtension( PluginToolsRequest request ) |
231 | |
{ |
232 | 0 | return SCRIPT_FILE_EXTENSION; |
233 | |
} |
234 | |
|
235 | |
|
236 | |
protected String getMetadataFileExtension( PluginToolsRequest request ) |
237 | |
{ |
238 | 0 | return METADATA_FILE_EXTENSION; |
239 | |
} |
240 | |
} |