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