View Javadoc

1   package org.apache.maven.tools.plugin.scanner;
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.PluginDescriptor;
25  import org.apache.maven.project.MavenProject;
26  import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
27  import org.apache.maven.tools.plugin.PluginToolsRequest;
28  import org.apache.maven.tools.plugin.extractor.ExtractionException;
29  import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor;
30  import org.codehaus.plexus.logging.AbstractLogEnabled;
31  import org.codehaus.plexus.logging.Logger;
32  import org.codehaus.plexus.logging.console.ConsoleLogger;
33  import org.codehaus.plexus.util.StringUtils;
34  
35  import java.util.HashSet;
36  import java.util.List;
37  import java.util.Map;
38  import java.util.Set;
39  
40  /**
41   * @author jdcasey
42   */
43  public class DefaultMojoScanner
44      extends AbstractLogEnabled
45      implements MojoScanner
46  {
47  
48      private Map<String, MojoDescriptorExtractor> mojoDescriptorExtractors;
49  
50      /**
51       * The names of the active extractors
52       */
53      private Set<String> activeExtractors;
54  
55      /**
56       * Default constructor
57       *
58       * @param extractors not null
59       */
60      public DefaultMojoScanner( Map<String, MojoDescriptorExtractor> extractors )
61      {
62          this.mojoDescriptorExtractors = extractors;
63  
64          this.enableLogging( new ConsoleLogger( Logger.LEVEL_INFO, "standalone-scanner-logger" ) );
65      }
66  
67      /**
68       * Empty constructor
69       */
70      public DefaultMojoScanner()
71      {
72          // nop
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      public void populatePluginDescriptor( MavenProject project, PluginDescriptor pluginDescriptor )
79          throws ExtractionException, InvalidPluginDescriptorException
80      {
81          populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) );
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      public void populatePluginDescriptor( PluginToolsRequest request )
88          throws ExtractionException, InvalidPluginDescriptorException
89      {
90          Logger logger = getLogger();
91          Set<String> activeExtractorsInternal = getActiveExtractors();
92  
93          logger.debug( "Using " + activeExtractorsInternal.size() + " mojo extractors." );
94  
95          int numMojoDescriptors = 0;
96  
97          for ( String language : activeExtractorsInternal )
98          {
99              MojoDescriptorExtractor extractor = mojoDescriptorExtractors.get( language );
100 
101             if ( extractor == null )
102             {
103                 throw new ExtractionException( "No mojo extractor for language: " + language );
104             }
105 
106             logger.info( "Applying mojo extractor for language: " + language );
107 
108             List<MojoDescriptor> extractorDescriptors = extractor.execute( request );
109 
110             logger.info( "Mojo extractor for language: " + language + " found " + extractorDescriptors.size()
111                              + " mojo descriptors." );
112             numMojoDescriptors += extractorDescriptors.size();
113 
114             for ( MojoDescriptor descriptor : extractorDescriptors )
115             {
116                 logger.debug( "Adding mojo: " + descriptor + " to plugin descriptor." );
117 
118                 descriptor.setPluginDescriptor( request.getPluginDescriptor() );
119 
120                 request.getPluginDescriptor().addMojo( descriptor );
121             }
122         }
123 
124         if ( numMojoDescriptors == 0 && !request.isSkipErrorNoDescriptorsFound() )
125         {
126             throw new InvalidPluginDescriptorException(
127                 "No mojo definitions were found for plugin: " + request.getPluginDescriptor().getPluginLookupKey()
128                     + "." );
129         }
130     }
131 
132     /**
133      * Gets the name of the active extractors.
134      *
135      * @return A Set containing the names of the active extractors.
136      */
137     protected Set<String> getActiveExtractors()
138     {
139         Set<String> result = activeExtractors;
140 
141         if ( result == null )
142         {
143             result = new HashSet<String>( mojoDescriptorExtractors.keySet() );
144         }
145 
146         return result;
147     }
148 
149     public void setActiveExtractors( Set<String> extractors )
150     {
151         if ( extractors == null )
152         {
153             this.activeExtractors = null;
154         }
155         else
156         {
157             this.activeExtractors = new HashSet<String>();
158 
159             for ( String extractor : extractors )
160             {
161                 if ( StringUtils.isNotEmpty( extractor ) )
162                 {
163                     this.activeExtractors.add( extractor );
164                 }
165             }
166         }
167     }
168 
169 }