Coverage Report - org.apache.maven.plugin.source.AbstractJarSourceMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractJarSourceMojo
93% 
88% 
1.571
 
 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.model.Resource;
 23  
 import org.apache.maven.plugin.AbstractMojo;
 24  
 import org.apache.maven.plugin.MojoExecutionException;
 25  
 import org.apache.maven.project.MavenProject;
 26  
 import org.apache.maven.project.MavenProjectHelper;
 27  
 import org.codehaus.plexus.archiver.Archiver;
 28  
 import org.codehaus.plexus.archiver.ArchiverException;
 29  
 import org.codehaus.plexus.archiver.jar.JarArchiver;
 30  
 import org.codehaus.plexus.util.FileUtils;
 31  
 
 32  
 import java.io.File;
 33  
 import java.io.IOException;
 34  
 import java.util.Iterator;
 35  
 import java.util.List;
 36  
 
 37  7
 public abstract class AbstractJarSourceMojo
 38  
     extends AbstractMojo
 39  
 {
 40  1
     private static final String[] DEFAULT_INCLUDES = new String[]{"**/*",};
 41  
 
 42  
     /**
 43  
      * @parameter expression="${project}"
 44  
      * @readonly
 45  
      * @required
 46  
      */
 47  
     private MavenProject project;
 48  
 
 49  
     /**
 50  
      * @parameter expression="${project.packaging}"
 51  
      * @readonly
 52  
      * @required
 53  
      */
 54  
     protected String packaging;
 55  
 
 56  
     /**
 57  
      * The project where the plugin is currently being executed.
 58  
      * The default value is populated from maven.
 59  
      *
 60  
      * @parameter expression="${executedProject}"
 61  
      * @required
 62  
      */
 63  
     private MavenProject executedProject;
 64  
 
 65  
     /**
 66  
      * Specifies whether or not to attach the artifact to the project
 67  
      *
 68  
      * @parameter expression="${attach}" default-value="true"
 69  
      */
 70  7
     private boolean attach = true;
 71  
 
 72  
     /** @component */
 73  
     private MavenProjectHelper projectHelper;
 74  
 
 75  
     /**
 76  
      * The directory where the generated archive file will be put.
 77  
      * Defaults to ${project.build.directory} specified in the pom or
 78  
      * inherited from the super pom.
 79  
      *
 80  
      * @parameter expression="${project.build.directory}"
 81  
      * @required
 82  
      */
 83  
     protected File outputDirectory;
 84  
 
 85  
     /**
 86  
      * The filename to be used for the generated archive file.
 87  
      * For the source:jar goal, "-sources" is appended to this filename.
 88  
      * For the source:test-jar goal, "-test-sources" is appended.
 89  
      * Defaults to ${project.build.finalName} specified in the pom
 90  
      * or inherited from the super pom.
 91  
      *
 92  
      * @parameter expression="${project.build.finalName}"
 93  
      * @required
 94  
      */
 95  
     protected String finalName;
 96  
 
 97  
     /** @see org.apache.maven.plugin.AbstractMojo#execute() */
 98  
     public abstract void execute()
 99  
         throws MojoExecutionException;
 100  
 
 101  
     public MavenProject getProject()
 102  
     {
 103  4
         return project;
 104  
     }
 105  
 
 106  
     public void setProject( MavenProject project )
 107  
     {
 108  7
         this.project = project;
 109  7
     }
 110  
 
 111  
     public String getPackaging()
 112  
     {
 113  1
         return packaging;
 114  
     }
 115  
 
 116  
     public void setPackaging( String packaging )
 117  
     {
 118  5
         this.packaging = packaging;
 119  5
     }
 120  
 
 121  
     public MavenProject getExecutedProject()
 122  
     {
 123  1
         return executedProject;
 124  
     }
 125  
 
 126  
     public void setExecutedProject( MavenProject executedProject )
 127  
     {
 128  7
         this.executedProject = executedProject;
 129  7
     }
 130  
 
 131  
     /**
 132  
      * Add the compile source directories and resource directories that will be included in the jar file
 133  
      *
 134  
      * @param compileSourceRoots
 135  
      * @param resources
 136  
      * @param sourceDirectories
 137  
      * @return an array of File objects that contains the directories that will be included in the jar file
 138  
      */
 139  
     protected File[] addDirectories( List compileSourceRoots,
 140  
                                      List resources,
 141  
                                      File[] sourceDirectories )
 142  
     {
 143  5
         int count = 0;
 144  5
         for ( Iterator i = compileSourceRoots.iterator(); i.hasNext(); )
 145  
         {
 146  5
             sourceDirectories[count] = new File( (String) i.next() );
 147  5
             count++;
 148  5
         }
 149  
 
 150  5
         for ( Iterator i = resources.iterator(); i.hasNext(); )
 151  
         {
 152  5
             Resource resource = (Resource) i.next();
 153  5
             sourceDirectories[count] = new File( resource.getDirectory() );
 154  5
             count++;
 155  5
         }
 156  
 
 157  5
         return sourceDirectories;
 158  
     }
 159  
 
 160  
     /**
 161  
      * Get the test sources that will be included in the test sources jar file
 162  
      *
 163  
      * @return an array of File objects that contains the test source directories
 164  
      */
 165  
     protected File[] getTestSources()
 166  
     {
 167  2
         List testCompileSourceRoots = executedProject.getTestCompileSourceRoots();
 168  2
         List testResources = executedProject.getTestResources();
 169  
 
 170  2
         File[] testSourceDirectories = new File[testCompileSourceRoots.size() + testResources.size()];
 171  2
         testSourceDirectories = addDirectories( testCompileSourceRoots, testResources, testSourceDirectories );
 172  
 
 173  2
         return testSourceDirectories;
 174  
     }
 175  
 
 176  
     /**
 177  
      * Get the main sources that will be included in the jar file
 178  
      *
 179  
      * @return an array of File objects that contains the source directories
 180  
      */
 181  
     protected File[] getDefaultSources()
 182  
     {
 183  3
         List compileSourceRoots = executedProject.getCompileSourceRoots();
 184  3
         List resources = executedProject.getResources();
 185  
 
 186  3
         File[] sourceDirectories = new File[compileSourceRoots.size() + resources.size()];
 187  3
         sourceDirectories = addDirectories( compileSourceRoots, resources, sourceDirectories );
 188  
 
 189  3
         return sourceDirectories;
 190  
     }
 191  
 
 192  
     /**
 193  
      * Create jar file that contains the specified source directories
 194  
      *
 195  
      * @param outputFile        the file name of the jar
 196  
      * @param sourceDirectories the source directories that will be included in the jar file
 197  
      */
 198  
     protected void createJar( File outputFile,
 199  
                               File[] sourceDirectories,
 200  
                               Archiver archiver )
 201  
         throws IOException, ArchiverException
 202  
     {
 203  5
         makeSourceBundle( outputFile, sourceDirectories, archiver );
 204  5
     }
 205  
 
 206  
     /**
 207  
      * Method to attach generated artifact to artifact list
 208  
      *
 209  
      * @param outputFile the artifact file to be attached
 210  
      * @param classifier
 211  
      */
 212  
     protected void attachArtifact( File outputFile,
 213  
                                    String classifier )
 214  
     {
 215  5
         if ( !attach )
 216  
         {
 217  3
             getLog().info( "NOT adding java-sources to attached artifacts list." );
 218  3
         }
 219  
         else
 220  
         {
 221  
             // TODO: these introduced dependencies on the project are going to become problematic - can we export it
 222  
             //  through metadata instead?
 223  2
             projectHelper.attachArtifact( project, "java-source", classifier, outputFile );
 224  
         }
 225  5
     }
 226  
 
 227  
     /**
 228  
      * Method to create an archive of the specified files
 229  
      *
 230  
      * @param outputFile        the destination file of the generated archive
 231  
      * @param sourceDirectories the directory where the files to be archived are located
 232  
      * @param archiver          the archiver object that will create the archive
 233  
      * @throws ArchiverException
 234  
      * @throws IOException
 235  
      */
 236  
     protected void makeSourceBundle( File outputFile,
 237  
                                      File[] sourceDirectories,
 238  
                                      Archiver archiver )
 239  
         throws ArchiverException, IOException
 240  
     {
 241  5
         String[] includes = DEFAULT_INCLUDES;
 242  
 
 243  15
         for ( int i = 0; i < sourceDirectories.length; i++ )
 244  
         {
 245  10
             if ( sourceDirectories[i].exists() )
 246  
             {
 247  10
                 archiver.addDirectory( sourceDirectories[i], includes, FileUtils.getDefaultExcludes() );
 248  
             }
 249  
         }
 250  
 
 251  5
         archiver.setDestFile( outputFile );
 252  
 
 253  5
         archiver.createArchive();
 254  5
     }
 255  
 
 256  
     protected Archiver createArchiver()
 257  
         throws ArchiverException
 258  
     {
 259  5
         Archiver archiver = new JarArchiver();
 260  
 
 261  5
         if ( project.getBuild() != null )
 262  
         {
 263  2
             List resources = project.getBuild().getResources();
 264  
 
 265  2
             for ( Iterator i = resources.iterator(); i.hasNext(); )
 266  
             {
 267  0
                 Resource r = (Resource) i.next();
 268  
 
 269  0
                 if ( r.getDirectory().endsWith( "maven-shared-archive-resources" ) )
 270  
                 {
 271  0
                     archiver.addDirectory( new File( r.getDirectory() ) );
 272  
                 }
 273  0
             }
 274  
 
 275  
             //archiver.setDotFileDirectory( new File( project.getBuild().getDirectory() ) );
 276  
         }
 277  
 
 278  5
         return archiver;
 279  
     }
 280  
 }