View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.jar;
20  
21  import java.io.File;
22  
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$
34   */
35  // CHECKSTYLE_OFF: LineLength
36  @Mojo(
37          name = "test-jar",
38          defaultPhase = LifecyclePhase.PACKAGE,
39          requiresProject = true,
40          threadSafe = true,
41          requiresDependencyResolution = ResolutionScope.TEST)
42  // CHECKSTYLE_ON: LineLength
43  public class TestJarMojo extends AbstractJarMojo {
44  
45      /**
46       * Set this to <code>true</code> to bypass test-jar generation. Its use is <b>NOT RECOMMENDED</b>, but quite
47       * convenient on occasion.
48       */
49      @Parameter(property = "maven.test.skip")
50      private boolean skip;
51  
52      /**
53       * Directory containing the test classes and resource files that should be packaged into the JAR.
54       */
55      @Parameter(defaultValue = "${project.build.testOutputDirectory}", required = true)
56      private File testClassesDirectory;
57  
58      /**
59       * Classifier to use for {@code test-jar}.
60       */
61      @Parameter(defaultValue = "tests")
62      private String classifier;
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      protected String getClassifier() {
69          return classifier;
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      protected String getType() {
77          return "test-jar";
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      protected File getClassesDirectory() {
85          return testClassesDirectory;
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public void execute() throws MojoExecutionException {
93          if (skip) {
94              getLog().info("Skipping packaging of the test-jar");
95          } else {
96              super.execute();
97          }
98      }
99  }