View Javadoc

1   package org.apache.maven.plugin.plugin;
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.artifact.Artifact;
23  import org.apache.maven.artifact.repository.ArtifactRepository;
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
27  import org.apache.maven.plugin.descriptor.PluginDescriptor;
28  import org.apache.maven.plugins.annotations.Component;
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
32  import org.apache.maven.tools.plugin.PluginToolsRequest;
33  import org.apache.maven.tools.plugin.extractor.ExtractionException;
34  import org.apache.maven.tools.plugin.generator.Generator;
35  import org.apache.maven.tools.plugin.generator.GeneratorException;
36  import org.apache.maven.tools.plugin.generator.GeneratorUtils;
37  import org.apache.maven.tools.plugin.scanner.MojoScanner;
38  import org.codehaus.plexus.util.ReaderFactory;
39  
40  import java.io.File;
41  import java.util.List;
42  import java.util.Set;
43  
44  /**
45   * Abstract class for this Plugin.
46   *
47   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
48   * @version $Id: AbstractGeneratorMojo.java 1345787 2012-06-03 21:58:22Z hboutemy $
49   */
50  public abstract class AbstractGeneratorMojo
51      extends AbstractMojo
52  {
53      /**
54       * The project currently being built.
55       */
56      @Component
57      protected MavenProject project;
58  
59      /**
60       * The component used for scanning the source tree for mojos.
61       */
62      @Component
63      protected MojoScanner mojoScanner;
64  
65      /**
66       * The file encoding of the source files.
67       *
68       * @since 2.5
69       */
70      @Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}" )
71      protected String encoding;
72  
73      /**
74       * The goal prefix that will appear before the ":".
75       */
76      @Parameter
77      protected String goalPrefix;
78  
79      /**
80       * By default an exception is throw if no mojo descriptor is found. As the maven-plugin is defined in core, the
81       * descriptor generator mojo is bound to generate-resources phase.
82       * But for annotations, the compiled classes are needed, so skip error
83       *
84       * @since 3.0
85       */
86      @Parameter( property = "maven.plugin.skipErrorNoDescriptorsFound", defaultValue = "false" )
87      protected boolean skipErrorNoDescriptorsFound;
88  
89      /**
90       * The role names of mojo extractors to use.
91       * <p/>
92       * If not set, all mojo extractors will be used. If set to an empty extractor name, no mojo extractors
93       * will be used.
94       * <p/>
95       * Example:
96       * <p/>
97       * <pre>
98       *  &lt;!-- Use all mojo extractors --&gt;
99       *  &lt;extractors/&gt;
100      *
101      *  &lt;!-- Use no mojo extractors --&gt;
102      *  &lt;extractors&gt;
103      *      &lt;extractor/&gt;
104      *  &lt;/extractors&gt;
105      *
106      *  &lt;!-- Use only bsh mojo extractor --&gt;
107      *  &lt;extractors&gt;
108      *      &lt;extractor&gt;bsh&lt;/extractor&gt;
109      *  &lt;/extractors&gt;
110      * </pre>
111      */
112     @Parameter
113     protected Set<String> extractors;
114 
115     /**
116      * Set this to "true" to skip invoking any goals or reports of the plugin.
117      *
118      * @since 2.8
119      */
120     @Parameter( defaultValue = "false", property = "maven.plugin.skip" )
121     protected boolean skip;
122 
123     /**
124      * The set of dependencies for the current project
125      *
126      * @since 3.0
127      */
128     @Parameter( defaultValue = "${project.artifacts}", required = true, readonly = true )
129     protected Set<Artifact> dependencies;
130 
131     /**
132      * List of Remote Repositories used by the resolver
133      *
134      * @since 3.0
135      */
136     @Parameter( defaultValue = "${project.remoteArtifactRepositories}", required = true, readonly = true )
137     protected List<ArtifactRepository> remoteRepos;
138 
139     /**
140      * Location of the local repository.
141      *
142      * @since 3.0
143      */
144     @Parameter( defaultValue = "${localRepository}", required = true, readonly = true )
145     protected ArtifactRepository local;
146 
147     /**
148      * @return the output directory where files will be generated.
149      */
150     protected abstract File getOutputDirectory();
151 
152     /**
153      * @return the wanted <code>Generator</code> implementation.
154      */
155     protected abstract Generator createGenerator();
156 
157     /**
158      * {@inheritDoc}
159      */
160     public void execute()
161         throws MojoExecutionException
162     {
163         if ( !"maven-plugin".equals( project.getPackaging() ) )
164         {
165             return;
166         }
167         if ( skip )
168         {
169             getLog().warn( "Execution skipped" );
170             return;
171         }
172 
173         if ( project.getArtifactId().toLowerCase().startsWith( "maven-" )
174             && project.getArtifactId().toLowerCase().endsWith( "-plugin" ) && !"org.apache.maven.plugins".equals(
175             project.getGroupId() ) )
176         {
177             getLog().error( "\n\nArtifact Ids of the format maven-___-plugin are reserved for \n"
178                                 + "plugins in the Group Id org.apache.maven.plugins\n"
179                                 + "Please change your artifactId to the format ___-maven-plugin\n"
180                                 + "In the future this error will break the build.\n\n" );
181         }
182 
183         String defaultGoalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId( project.getArtifactId() );
184         if ( goalPrefix == null )
185         {
186             goalPrefix = defaultGoalPrefix;
187         }
188         else if ( !goalPrefix.equals( defaultGoalPrefix ) )
189         {
190             getLog().warn(
191                 "\n\nGoal prefix is specified as: '" + goalPrefix + "'. " + "Maven currently expects it to be '"
192                     + defaultGoalPrefix + "'.\n" );
193         }
194 
195         mojoScanner.setActiveExtractors( extractors );
196 
197         // TODO: could use this more, eg in the writing of the plugin descriptor!
198         PluginDescriptor pluginDescriptor = new PluginDescriptor();
199 
200         pluginDescriptor.setGroupId( project.getGroupId() );
201 
202         pluginDescriptor.setArtifactId( project.getArtifactId() );
203 
204         pluginDescriptor.setVersion( project.getVersion() );
205 
206         pluginDescriptor.setGoalPrefix( goalPrefix );
207 
208         pluginDescriptor.setName( project.getName() );
209 
210         pluginDescriptor.setDescription( project.getDescription() );
211 
212         if ( encoding == null || encoding.length() < 1 )
213         {
214             getLog().warn( "Using platform encoding (" + ReaderFactory.FILE_ENCODING
215                                + " actually) to read mojo metadata, i.e. build is platform dependent!" );
216         }
217         else
218         {
219             getLog().info( "Using '" + encoding + "' encoding to read mojo metadata." );
220         }
221 
222         try
223         {
224             pluginDescriptor.setDependencies( GeneratorUtils.toComponentDependencies( project.getRuntimeDependencies() ) );
225 
226             PluginToolsRequest request = new DefaultPluginToolsRequest( project, pluginDescriptor );
227             request.setEncoding( encoding );
228             request.setSkipErrorNoDescriptorsFound( skipErrorNoDescriptorsFound );
229             request.setDependencies( dependencies );
230             request.setLocal( this.local );
231             request.setRemoteRepos( this.remoteRepos );
232 
233             mojoScanner.populatePluginDescriptor( request );
234 
235             getOutputDirectory().mkdirs();
236 
237             createGenerator().execute( getOutputDirectory(), request );
238         }
239         catch ( GeneratorException e )
240         {
241             throw new MojoExecutionException( "Error writing plugin descriptor", e );
242         }
243         catch ( InvalidPluginDescriptorException e )
244         {
245             throw new MojoExecutionException( "Error extracting plugin descriptor: \'" + e.getLocalizedMessage() + "\'",
246                                               e );
247         }
248         catch ( ExtractionException e )
249         {
250             throw new MojoExecutionException( "Error extracting plugin descriptor: \'" + e.getLocalizedMessage() + "\'",
251                                               e );
252         }
253         catch ( LinkageError e )
254         {
255             throw new MojoExecutionException( "The API of the mojo scanner is not compatible with this plugin version."
256                                                   + " Please check the plugin dependencies configured in the POM and ensure the versions match.",
257                                               e );
258         }
259     }
260 
261 }