View Javadoc

1   package org.apache.maven.tools.plugin.extractor.ant;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  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          List descriptors = new ArrayList();
52  
53          PluginMetadataParser parser = new PluginMetadataParser();
54  
55          for ( Iterator mapIterator = metadataFilesKeyedByBasedir.entrySet().iterator(); mapIterator.hasNext(); )
56          {
57              Map.Entry entry = (Map.Entry) mapIterator.next();
58  
59              String basedir = (String) entry.getKey();
60              Set metadataFiles = (Set) entry.getValue();
61  
62              for ( Iterator it = metadataFiles.iterator(); it.hasNext(); )
63              {
64                  File metadataFile = (File) it.next();
65  
66                  String basename = metadataFile.getName();
67                  basename = basename.substring( 0, basename.length() - METADATA_FILE_EXTENSION.length() );
68  
69                  File scriptFile = new File( metadataFile.getParentFile(), basename + SCRIPT_FILE_EXTENSION );
70  
71                  if ( !scriptFile.exists() )
72                  {
73                      throw new InvalidPluginDescriptorException(
74                          "Found orphaned plugin metadata file: " + metadataFile );
75                  }
76  
77                  String relativePath = null;
78  
79                  if ( basedir.endsWith( "/" ) )
80                  {
81                      basedir = basedir.substring( 0, basedir.length() - 2 );
82                  }
83  
84                  relativePath = scriptFile.getPath().substring( basedir.length() );
85  
86                  relativePath = relativePath.replace( '\\', '/' );
87  
88                  try
89                  {
90                      Set mojoDescriptors = parser.parseMojoDescriptors( metadataFile );
91  
92                      for ( Iterator discoveredMojoIterator = mojoDescriptors.iterator(); discoveredMojoIterator
93                          .hasNext(); )
94                      {
95                          MojoDescriptor descriptor = (MojoDescriptor) discoveredMojoIterator.next();
96  
97                          Map paramMap = descriptor.getParameterMap();
98  
99                          if ( !paramMap.containsKey( "basedir" ) )
100                         {
101                             Parameter param = new Parameter();
102                             param.setName( "basedir" );
103                             param.setAlias( "ant.basedir" );
104                             param.setExpression( "${antBasedir}" );
105                             param.setDefaultValue( "${basedir}" );
106                             param.setType( "java.io.File" );
107                             param.setDescription( "The base directory from which to execute the Ant script." );
108                             param.setEditable( true );
109                             param.setRequired( true );
110 
111                             descriptor.addParameter( param );
112                         }
113 
114                         if ( !paramMap.containsKey( "antMessageLevel" ) )
115                         {
116                             Parameter param = new Parameter();
117                             param.setName( "messageLevel" );
118                             param.setAlias( "ant.messageLevel" );
119                             param.setExpression( "${antMessageLevel}" );
120                             param.setDefaultValue( "info" );
121                             param.setType( "java.lang.String" );
122                             param.setDescription( "The message-level used to tune the verbosity of Ant logging." );
123                             param.setEditable( true );
124                             param.setRequired( false );
125 
126                             descriptor.addParameter( param );
127                         }
128 
129                         String implementation = relativePath;
130 
131                         String dImpl = descriptor.getImplementation();
132                         if ( StringUtils.isNotEmpty( dImpl ) )
133                         {
134                             implementation =
135                                 relativePath + dImpl.substring( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.length() );
136                         }
137 
138                         descriptor.setImplementation( implementation );
139 
140                         descriptor.setLanguage( "ant-mojo" );
141                         descriptor.setComponentComposer( "map-oriented" );
142                         descriptor.setComponentConfigurator( "map-oriented" );
143 
144                         descriptor.setPluginDescriptor( pluginDescriptor );
145 
146                         descriptors.add( descriptor );
147                     }
148                 }
149                 catch ( PluginMetadataParseException e )
150                 {
151                     throw new ExtractionException( "Error extracting mojo descriptor from script: " + metadataFile, e );
152                 }
153             }
154         }
155 
156         return descriptors;
157     }
158 
159     protected String getScriptFileExtension()
160     {
161         return SCRIPT_FILE_EXTENSION;
162     }
163 
164     protected String getMetadataFileExtension()
165     {
166         return METADATA_FILE_EXTENSION;
167     }
168 }