View Javadoc
1   package org.codehaus.plexus.maven.plugin;
2   
3   /*
4    * Copyright (c) 2004-2005, Codehaus.org
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7    * associated documentation files (the "Software"), to deal in the Software without restriction,
8    * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9    * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10   * furnished to do so, subject to the following conditions:
11   *
12   * The above copyright notice and this permission notice shall be included in all copies or
13   * substantial portions of the Software.
14   *
15   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16   * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18   * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20   */
21  
22  import java.io.File;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugins.annotations.LifecyclePhase;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.plugins.annotations.ResolutionScope;
29  import org.codehaus.plexus.metadata.MetadataGenerationRequest;
30  
31  /**
32   * Generates a Plexus {@code components.xml} component descriptor file from source (javadoc) or
33   * class annotations and manually crafted descriptor files.
34   *
35   * @author Jason van Zyl
36   * @author Trygve Laugstøl
37   */
38  @Mojo(
39          name = "generate-metadata",
40          defaultPhase = LifecyclePhase.PROCESS_CLASSES,
41          requiresDependencyResolution = ResolutionScope.COMPILE)
42  public class PlexusDescriptorMojo extends AbstractDescriptorMojo {
43      /**
44       * The output location for the generated descriptor.
45       */
46      @Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/plexus/components.xml", required = true)
47      protected File generatedMetadata;
48  
49      /**
50       * The location of manually crafted component descriptors. The contents of the descriptor files in this directory is
51       * merged with the information extracted from the project's sources/classes.
52       */
53      @Parameter(defaultValue = "${basedir}/src/main/resources/META-INF/plexus", required = true)
54      protected File staticMetadataDirectory;
55  
56      /**
57       * The output location for the intermediary descriptor. This descriptors contains only the information extracted
58       * from the project's sources/classes.
59       */
60      @Parameter(defaultValue = "${project.build.directory}/components.xml", required = true)
61      protected File intermediaryMetadata;
62  
63      public void execute() throws MojoExecutionException {
64          MetadataGenerationRequest request = new MetadataGenerationRequest();
65  
66          try {
67              request.classpath = mavenProject.getCompileClasspathElements();
68              request.classesDirectory = new File(mavenProject.getBuild().getOutputDirectory());
69              request.sourceDirectories = mavenProject.getCompileSourceRoots();
70              request.sourceEncoding = sourceEncoding;
71              request.componentDescriptorDirectory = staticMetadataDirectory;
72              request.intermediaryFile = intermediaryMetadata;
73              request.outputFile = generatedMetadata;
74              request.extractors = extractors;
75  
76              metadataGenerator.generateDescriptor(request);
77          } catch (Exception e) {
78              throw new MojoExecutionException("Error generating metadata: ", e);
79          }
80      }
81  }