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 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 List<MojoDescriptor> descriptors = new ArrayList<MojoDescriptor>();
66
67 PluginMetadataParser parser = new PluginMetadataParser();
68
69 for ( Map.Entry<String, Set<File>> entry : metadataFilesKeyedByBasedir.entrySet() )
70 {
71 String basedir = entry.getKey();
72 Set<File> metadataFiles = entry.getValue();
73
74 for ( File metadataFile : metadataFiles )
75 {
76 String basename = metadataFile.getName();
77 basename = basename.substring( 0, basename.length() - METADATA_FILE_EXTENSION.length() );
78
79 File scriptFile = new File( metadataFile.getParentFile(), basename + SCRIPT_FILE_EXTENSION );
80
81 if ( !scriptFile.exists() )
82 {
83 throw new InvalidPluginDescriptorException(
84 "Found orphaned plugin metadata file: " + metadataFile );
85 }
86
87 String relativePath = scriptFile.getPath().substring( basedir.length() ).replace( '\\', '/' );
88
89 if ( relativePath.startsWith( "/" ) )
90 {
91 relativePath = relativePath.substring( 1 );
92 }
93
94 try
95 {
96 Set<MojoDescriptor> mojoDescriptors = parser.parseMojoDescriptors( metadataFile );
97
98 for ( MojoDescriptor descriptor : mojoDescriptors )
99 {
100 @SuppressWarnings( "unchecked" )
101 Map<String, ?> paramMap = descriptor.getParameterMap();
102
103 if ( !paramMap.containsKey( "basedir" ) )
104 {
105 Parameter param = new Parameter();
106 param.setName( "basedir" );
107 param.setAlias( "ant.basedir" );
108 param.setExpression( "${antBasedir}" );
109 param.setDefaultValue( "${basedir}" );
110 param.setType( "java.io.File" );
111 param.setDescription( "The base directory from which to execute the Ant script." );
112 param.setEditable( true );
113 param.setRequired( true );
114
115 descriptor.addParameter( param );
116 }
117
118 if ( !paramMap.containsKey( "antMessageLevel" ) )
119 {
120 Parameter param = new Parameter();
121 param.setName( "messageLevel" );
122 param.setAlias( "ant.messageLevel" );
123 param.setExpression( "${antMessageLevel}" );
124 param.setDefaultValue( "info" );
125 param.setType( "java.lang.String" );
126 param.setDescription( "The message-level used to tune the verbosity of Ant logging." );
127 param.setEditable( true );
128 param.setRequired( false );
129
130 descriptor.addParameter( param );
131 }
132
133 if ( !paramMap.containsKey( "project" ) )
134 {
135 Parameter param = new Parameter();
136 param.setName( "project" );
137 param.setDefaultValue( "${project}" );
138 param.setType( MavenProject.class.getName() );
139 param.setDescription( "The current MavenProject instance, which contains classpath elements." );
140 param.setEditable( false );
141 param.setRequired( true );
142
143 descriptor.addParameter( param );
144 }
145
146 if ( !paramMap.containsKey( "session" ) )
147 {
148 Parameter param = new Parameter();
149 param.setName( "session" );
150 param.setDefaultValue( "${session}" );
151 param.setType( "org.apache.maven.execution.MavenSession" );
152 param.setDescription( "The current MavenSession instance, which is used for plugin-style expression resolution." );
153 param.setEditable( false );
154 param.setRequired( true );
155
156 descriptor.addParameter( param );
157 }
158
159 if ( !paramMap.containsKey( "mojoExecution" ) )
160 {
161 Parameter param = new Parameter();
162 param.setName( "mojoExecution" );
163 param.setDefaultValue( "${mojoExecution}" );
164 param.setType( "org.apache.maven.plugin.MojoExecution" );
165 param.setDescription( "The current Maven MojoExecution instance, which contains information about the mojo currently executing." );
166 param.setEditable( false );
167 param.setRequired( true );
168
169 descriptor.addParameter( param );
170 }
171
172 @SuppressWarnings( "unchecked" )
173 List<ComponentRequirement> requirements = descriptor.getRequirements();
174 Map<String, ComponentRequirement> reqMap = new HashMap<String, ComponentRequirement>();
175
176 if ( requirements != null )
177 {
178 for ( ComponentRequirement req : requirements )
179 {
180 reqMap.put( req.getRole(), req );
181 }
182 }
183
184 if ( !reqMap.containsKey( PathTranslator.class.getName() ) )
185 {
186 ComponentRequirement req = new ComponentRequirement();
187 req.setRole( PathTranslator.class.getName() );
188
189 descriptor.addRequirement( req );
190 }
191
192 String implementation = relativePath;
193
194 String dImpl = descriptor.getImplementation();
195 if ( StringUtils.isNotEmpty( dImpl ) )
196 {
197 if ( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.equals( dImpl ) )
198 {
199 implementation = relativePath;
200 }
201 else
202 {
203 implementation =
204 relativePath + dImpl.substring( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.length() );
205 }
206 }
207
208 descriptor.setImplementation( implementation );
209
210 descriptor.setLanguage( "ant-mojo" );
211 descriptor.setComponentComposer( "map-oriented" );
212 descriptor.setComponentConfigurator( "map-oriented" );
213
214 descriptor.setPluginDescriptor( request.getPluginDescriptor() );
215
216 descriptors.add( descriptor );
217 }
218 }
219 catch ( PluginMetadataParseException e )
220 {
221 throw new ExtractionException( "Error extracting mojo descriptor from script: " + metadataFile, e );
222 }
223 }
224 }
225
226 return descriptors;
227 }
228
229
230 protected String getScriptFileExtension( PluginToolsRequest request )
231 {
232 return SCRIPT_FILE_EXTENSION;
233 }
234
235
236 protected String getMetadataFileExtension( PluginToolsRequest request )
237 {
238 return METADATA_FILE_EXTENSION;
239 }
240 }