View Javadoc

1   package org.apache.maven.plugin.source;
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.plugin.MojoExecutionException;
23  import org.codehaus.plexus.archiver.ArchiverException;
24  import org.codehaus.plexus.archiver.jar.JarArchiver;
25  
26  import java.io.File;
27  import java.io.IOException;
28  
29  /**
30   * This plugin bundles all the test sources into a jar archive.
31   *
32   * @goal test-jar
33   * @phase package
34   * @execute phase="generate-sources"
35   */
36  public class JarTestSourceMojo
37      extends AbstractJarSourceMojo
38  {
39      /**
40       * @see org.apache.maven.plugin.AbstractMojo#execute()
41       */
42      public void execute()
43          throws MojoExecutionException
44      {
45          if ( "pom".equals( packaging ) )
46          {
47              getLog().info( "NOT adding test sources to attached artifacts for packaging: \'" + packaging + "\'." );
48          }
49          else
50          {
51              File outputFile = new File( outputDirectory, finalName + "-test-sources.jar" );
52              File[] testSourceDirectories = getTestSources();
53  
54              try
55              {
56                  createJar( outputFile, testSourceDirectories, createArchiver() );
57              }
58              catch ( IOException e )
59              {
60                  throw new MojoExecutionException( "Error creating source archive: " + e.getMessage(), e );
61              }
62              catch ( ArchiverException e )
63              {
64                  throw new MojoExecutionException( "Error creating source archive: " + e.getMessage(), e );
65              }
66  
67              attachArtifact( outputFile, "test-sources" );
68          }
69      }
70  
71  }