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