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 org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException; |
23 |
|
import org.apache.maven.plugin.descriptor.MojoDescriptor; |
24 |
|
import org.apache.maven.plugin.descriptor.Parameter; |
25 |
|
import org.apache.maven.plugin.descriptor.PluginDescriptor; |
26 |
|
import org.apache.maven.plugin.tools.model.PluginMetadataParseException; |
27 |
|
import org.apache.maven.plugin.tools.model.PluginMetadataParser; |
28 |
|
import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor; |
29 |
|
import org.apache.maven.tools.plugin.extractor.ExtractionException; |
30 |
|
import org.codehaus.plexus.util.StringUtils; |
31 |
|
|
32 |
|
import java.io.File; |
33 |
|
import java.util.ArrayList; |
34 |
|
import java.util.Iterator; |
35 |
|
import java.util.List; |
36 |
|
import java.util.Map; |
37 |
|
import java.util.Set; |
38 |
|
|
39 |
0 |
public class AntMojoDescriptorExtractor |
40 |
|
extends AbstractScriptedMojoDescriptorExtractor |
41 |
|
{ |
42 |
|
|
43 |
|
private static final String METADATA_FILE_EXTENSION = ".mojos.xml"; |
44 |
|
|
45 |
|
private static final String SCRIPT_FILE_EXTENSION = ".build.xml"; |
46 |
|
|
47 |
|
protected List extractMojoDescriptorsFromMetadata( Map metadataFilesKeyedByBasedir, |
48 |
|
PluginDescriptor pluginDescriptor ) |
49 |
|
throws ExtractionException, InvalidPluginDescriptorException |
50 |
|
{ |
51 |
0 |
List descriptors = new ArrayList(); |
52 |
|
|
53 |
0 |
PluginMetadataParser parser = new PluginMetadataParser(); |
54 |
|
|
55 |
0 |
for ( Iterator mapIterator = metadataFilesKeyedByBasedir.entrySet().iterator(); mapIterator.hasNext(); ) |
56 |
|
{ |
57 |
0 |
Map.Entry entry = (Map.Entry) mapIterator.next(); |
58 |
|
|
59 |
0 |
String basedir = (String) entry.getKey(); |
60 |
0 |
Set metadataFiles = (Set) entry.getValue(); |
61 |
|
|
62 |
0 |
for ( Iterator it = metadataFiles.iterator(); it.hasNext(); ) |
63 |
|
{ |
64 |
0 |
File metadataFile = (File) it.next(); |
65 |
|
|
66 |
0 |
String basename = metadataFile.getName(); |
67 |
0 |
basename = basename.substring( 0, basename.length() - METADATA_FILE_EXTENSION.length() ); |
68 |
|
|
69 |
0 |
File scriptFile = new File( metadataFile.getParentFile(), basename + SCRIPT_FILE_EXTENSION ); |
70 |
|
|
71 |
0 |
if ( !scriptFile.exists() ) |
72 |
|
{ |
73 |
0 |
throw new InvalidPluginDescriptorException( |
74 |
|
"Found orphaned plugin metadata file: " + metadataFile ); |
75 |
|
} |
76 |
|
|
77 |
0 |
String relativePath = null; |
78 |
|
|
79 |
0 |
if ( basedir.endsWith( "/" ) ) |
80 |
|
{ |
81 |
0 |
basedir = basedir.substring( 0, basedir.length() - 2 ); |
82 |
|
} |
83 |
|
|
84 |
0 |
relativePath = scriptFile.getPath().substring( basedir.length() ); |
85 |
|
|
86 |
0 |
relativePath = relativePath.replace( '\\', '/' ); |
87 |
|
|
88 |
|
try |
89 |
|
{ |
90 |
0 |
Set mojoDescriptors = parser.parseMojoDescriptors( metadataFile ); |
91 |
|
|
92 |
0 |
for ( Iterator discoveredMojoIterator = mojoDescriptors.iterator(); discoveredMojoIterator |
93 |
0 |
.hasNext(); ) |
94 |
|
{ |
95 |
0 |
MojoDescriptor descriptor = (MojoDescriptor) discoveredMojoIterator.next(); |
96 |
|
|
97 |
0 |
Map paramMap = descriptor.getParameterMap(); |
98 |
|
|
99 |
0 |
if ( !paramMap.containsKey( "basedir" ) ) |
100 |
|
{ |
101 |
0 |
Parameter param = new Parameter(); |
102 |
0 |
param.setName( "basedir" ); |
103 |
0 |
param.setAlias( "ant.basedir" ); |
104 |
0 |
param.setExpression( "${antBasedir}" ); |
105 |
0 |
param.setDefaultValue( "${basedir}" ); |
106 |
0 |
param.setType( "java.io.File" ); |
107 |
0 |
param.setDescription( "The base directory from which to execute the Ant script." ); |
108 |
0 |
param.setEditable( true ); |
109 |
0 |
param.setRequired( true ); |
110 |
|
|
111 |
0 |
descriptor.addParameter( param ); |
112 |
|
} |
113 |
|
|
114 |
0 |
if ( !paramMap.containsKey( "antMessageLevel" ) ) |
115 |
|
{ |
116 |
0 |
Parameter param = new Parameter(); |
117 |
0 |
param.setName( "messageLevel" ); |
118 |
0 |
param.setAlias( "ant.messageLevel" ); |
119 |
0 |
param.setExpression( "${antMessageLevel}" ); |
120 |
0 |
param.setDefaultValue( "info" ); |
121 |
0 |
param.setType( "java.lang.String" ); |
122 |
0 |
param.setDescription( "The message-level used to tune the verbosity of Ant logging." ); |
123 |
0 |
param.setEditable( true ); |
124 |
0 |
param.setRequired( false ); |
125 |
|
|
126 |
0 |
descriptor.addParameter( param ); |
127 |
|
} |
128 |
|
|
129 |
0 |
String implementation = relativePath; |
130 |
|
|
131 |
0 |
String dImpl = descriptor.getImplementation(); |
132 |
0 |
if ( StringUtils.isNotEmpty( dImpl ) ) |
133 |
|
{ |
134 |
0 |
implementation = |
135 |
|
relativePath + dImpl.substring( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.length() ); |
136 |
|
} |
137 |
|
|
138 |
0 |
descriptor.setImplementation( implementation ); |
139 |
|
|
140 |
0 |
descriptor.setLanguage( "ant-mojo" ); |
141 |
0 |
descriptor.setComponentComposer( "map-oriented" ); |
142 |
0 |
descriptor.setComponentConfigurator( "map-oriented" ); |
143 |
|
|
144 |
0 |
descriptor.setPluginDescriptor( pluginDescriptor ); |
145 |
|
|
146 |
0 |
descriptors.add( descriptor ); |
147 |
0 |
} |
148 |
|
} |
149 |
0 |
catch ( PluginMetadataParseException e ) |
150 |
|
{ |
151 |
0 |
throw new ExtractionException( "Error extracting mojo descriptor from script: " + metadataFile, e ); |
152 |
0 |
} |
153 |
0 |
} |
154 |
0 |
} |
155 |
|
|
156 |
0 |
return descriptors; |
157 |
|
} |
158 |
|
|
159 |
|
protected String getScriptFileExtension() |
160 |
|
{ |
161 |
0 |
return SCRIPT_FILE_EXTENSION; |
162 |
|
} |
163 |
|
|
164 |
|
protected String getMetadataFileExtension() |
165 |
|
{ |
166 |
0 |
return METADATA_FILE_EXTENSION; |
167 |
|
} |
168 |
|
} |