001 package org.apache.maven.tools.plugin.scanner; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException; 023 import org.apache.maven.plugin.descriptor.MojoDescriptor; 024 import org.apache.maven.plugin.descriptor.PluginDescriptor; 025 import org.apache.maven.project.MavenProject; 026 import org.apache.maven.tools.plugin.DefaultPluginToolsRequest; 027 import org.apache.maven.tools.plugin.PluginToolsRequest; 028 import org.apache.maven.tools.plugin.extractor.ExtractionException; 029 import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor; 030 import org.codehaus.plexus.logging.AbstractLogEnabled; 031 import org.codehaus.plexus.logging.Logger; 032 import org.codehaus.plexus.logging.console.ConsoleLogger; 033 import org.codehaus.plexus.util.StringUtils; 034 035 import java.util.HashSet; 036 import java.util.List; 037 import java.util.Map; 038 import java.util.Set; 039 040 /** 041 * @author jdcasey 042 */ 043 public class DefaultMojoScanner 044 extends AbstractLogEnabled 045 implements MojoScanner 046 { 047 048 private Map<String, MojoDescriptorExtractor> mojoDescriptorExtractors; 049 050 /** 051 * The names of the active extractors 052 */ 053 private Set<String> activeExtractors; 054 055 /** 056 * Default constructor 057 * 058 * @param extractors not null 059 */ 060 public DefaultMojoScanner( Map<String, MojoDescriptorExtractor> extractors ) 061 { 062 this.mojoDescriptorExtractors = extractors; 063 064 this.enableLogging( new ConsoleLogger( Logger.LEVEL_INFO, "standalone-scanner-logger" ) ); 065 } 066 067 /** 068 * Empty constructor 069 */ 070 public DefaultMojoScanner() 071 { 072 // nop 073 } 074 075 /** 076 * {@inheritDoc} 077 */ 078 public void populatePluginDescriptor( MavenProject project, PluginDescriptor pluginDescriptor ) 079 throws ExtractionException, InvalidPluginDescriptorException 080 { 081 populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) ); 082 } 083 084 /** 085 * {@inheritDoc} 086 */ 087 public void populatePluginDescriptor( PluginToolsRequest request ) 088 throws ExtractionException, InvalidPluginDescriptorException 089 { 090 Logger logger = getLogger(); 091 Set<String> activeExtractorsInternal = getActiveExtractors(); 092 093 logger.debug( "Using " + activeExtractorsInternal.size() + " mojo extractors." ); 094 095 int numMojoDescriptors = 0; 096 097 for ( String language : activeExtractorsInternal ) 098 { 099 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 }