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 <tt>components.xml</tt> component descriptor file from test source (javadoc)
33   * or test class annotations.
34   * 
35   * @author Jason van Zyl
36   * @author Trygve Laugst&oslash;l
37   * @version $Id$
38   */
39  @Mojo( name = "generate-test-metadata", defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES, requiresDependencyResolution = ResolutionScope.TEST )
40  public class PlexusTestDescriptorMojo
41      extends AbstractDescriptorMojo
42  {
43      /**
44       * The output location for the generated descriptor.
45       */
46      @Parameter( defaultValue = "${project.build.testOutputDirectory}/META-INF/plexus/components.xml", required = true )
47      protected File testGeneratedMetadata;
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/test/resources/META-INF/plexus", required = true )
54      protected File testStaticMetadataDirectory;
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}/test-components.xml", required = true )
61      protected File testIntermediaryMetadata;
62  
63      public void execute()
64          throws MojoExecutionException
65      {
66          MetadataGenerationRequest request = new MetadataGenerationRequest();
67  
68          try
69          {
70              request.classpath = mavenProject.getTestClasspathElements();
71              request.classesDirectory = new File( mavenProject.getBuild().getTestOutputDirectory() );
72              request.sourceDirectories = mavenProject.getTestCompileSourceRoots();
73              request.sourceEncoding = sourceEncoding;
74              request.componentDescriptorDirectory = testStaticMetadataDirectory;
75              request.intermediaryFile = testIntermediaryMetadata;
76              request.outputFile = testGeneratedMetadata;
77              request.extractors = extractors;
78              
79              metadataGenerator.generateDescriptor( request );
80          }
81          catch ( Exception e )
82          {
83              throw new MojoExecutionException( "Error generating test metadata: ", e );
84          }
85      }
86  }