View Javadoc
1   package org.apache.maven.plugin.jar;
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 java.io.File;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugins.annotations.LifecyclePhase;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.Parameter;
27  import org.apache.maven.plugins.annotations.ResolutionScope;
28  
29  /**
30   * Build a JAR of the test classes for the current project.
31   *
32   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
33   * @version $Id: TestJarMojo.java 1636726 2014-11-04 20:11:52Z khmarbaise $
34   */
35  @Mojo( name = "test-jar", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true,
36         requiresDependencyResolution = ResolutionScope.TEST )
37  public class TestJarMojo
38      extends AbstractJarMojo
39  {
40  
41      /**
42       * Set this to <code>true</code> to bypass unit tests entirely.
43       * Its use is <b>NOT RECOMMENDED</b>, but quite convenient on occasion.
44       */
45      @Parameter( property = "maven.test.skip" )
46      private boolean skip;
47  
48      /**
49       * Directory containing the test classes and resource files that should be packaged into the JAR.
50       */
51      @Parameter( defaultValue = "${project.build.testOutputDirectory}", required = true )
52      private File testClassesDirectory;
53  
54      protected String getClassifier()
55      {
56          return "tests";
57      }
58  
59      /**
60       * @return type of the generated artifact
61       */
62      protected String getType()
63      {
64          return "test-jar";
65      }
66  
67      /**
68       * Return the test-classes directory, to serve as the root of the tests jar.
69       */
70      protected File getClassesDirectory()
71      {
72          return testClassesDirectory;
73      }
74  
75      public void execute()
76          throws MojoExecutionException
77      {
78          if ( skip )
79          {
80              getLog().info( "Skipping packaging of the test-jar" );
81          }
82          else
83          {
84              super.execute();
85          }
86      }
87  }