Coverage Report - org.apache.maven.plugin.javadoc.JavadocJar
 
Classes in this File Line Coverage Branch Coverage Complexity
JavadocJar
61%
35/57
42%
12/28
9
 
 1  
 package org.apache.maven.plugin.javadoc;
 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.archiver.MavenArchiveConfiguration;
 23  
 import org.apache.maven.archiver.MavenArchiver;
 24  
 import org.apache.maven.artifact.DependencyResolutionRequiredException;
 25  
 import org.apache.maven.artifact.handler.ArtifactHandler;
 26  
 import org.apache.maven.model.Resource;
 27  
 import org.apache.maven.plugin.MojoExecutionException;
 28  
 import org.apache.maven.plugins.annotations.Component;
 29  
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 30  
 import org.apache.maven.plugins.annotations.Mojo;
 31  
 import org.apache.maven.plugins.annotations.Parameter;
 32  
 import org.apache.maven.plugins.annotations.ResolutionScope;
 33  
 import org.apache.maven.project.MavenProjectHelper;
 34  
 import org.apache.maven.reporting.MavenReportException;
 35  
 import org.codehaus.plexus.archiver.Archiver;
 36  
 import org.codehaus.plexus.archiver.ArchiverException;
 37  
 import org.codehaus.plexus.archiver.jar.JarArchiver;
 38  
 import org.codehaus.plexus.archiver.jar.ManifestException;
 39  
 
 40  
 import java.io.File;
 41  
 import java.io.IOException;
 42  
 import java.util.List;
 43  
 import java.util.Locale;
 44  
 
 45  
 /**
 46  
  * Bundles the Javadoc documentation for <code>main Java code</code> in an <b>NON aggregator</b> project into
 47  
  * a jar using the standard <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/javadoc/">Javadoc Tool</a>.
 48  
  *
 49  
  * @version $Id: JavadocJar.java 1385191 2012-09-15 22:04:49Z hboutemy $
 50  
  * @since 2.0
 51  
  */
 52  
 @Mojo( name = "jar", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true )
 53  2
 public class JavadocJar
 54  
     extends AbstractJavadocMojo
 55  
 {
 56  
     /**
 57  
      * Includes all generated Javadoc files
 58  
      */
 59  1
     private static final String[] DEFAULT_INCLUDES = new String[]{ "**/**" };
 60  
 
 61  
     /**
 62  
      * Excludes all processing files.
 63  
      *
 64  
      * @see AbstractJavadocMojo#DEBUG_JAVADOC_SCRIPT_NAME
 65  
      * @see AbstractJavadocMojo#OPTIONS_FILE_NAME
 66  
      * @see AbstractJavadocMojo#PACKAGES_FILE_NAME
 67  
      * @see AbstractJavadocMojo#ARGFILE_FILE_NAME
 68  
      * @see AbstractJavadocMojo#FILES_FILE_NAME
 69  
      */
 70  1
     private static final String[] DEFAULT_EXCLUDES =
 71  
         new String[]{ DEBUG_JAVADOC_SCRIPT_NAME, OPTIONS_FILE_NAME, PACKAGES_FILE_NAME, ARGFILE_FILE_NAME,
 72  
             FILES_FILE_NAME };
 73  
 
 74  
     // ----------------------------------------------------------------------
 75  
     // Mojo components
 76  
     // ----------------------------------------------------------------------
 77  
 
 78  
     /**
 79  
      * Used for attaching the artifact in the project.
 80  
      */
 81  
     @Component
 82  
     private MavenProjectHelper projectHelper;
 83  
 
 84  
     /**
 85  
      * The Jar archiver.
 86  
      *
 87  
      * @since 2.5
 88  
      */
 89  
     @Component( role = Archiver.class, hint = "jar" )
 90  
     private JarArchiver jarArchiver;
 91  
 
 92  
     // ----------------------------------------------------------------------
 93  
     // Mojo Parameters
 94  
     // ----------------------------------------------------------------------
 95  
 
 96  
     /**
 97  
      * Specifies the destination directory where javadoc saves the generated HTML files.
 98  
      * See <a href="http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/javadoc.html#d">d</a>.
 99  
      *
 100  
      * @deprecated
 101  
      */
 102  
     @Parameter( property = "destDir" )
 103  
     private File destDir;
 104  
 
 105  
     /**
 106  
      * Specifies the directory where the generated jar file will be put.
 107  
      */
 108  
     @Parameter( property = "project.build.directory" )
 109  
     private String jarOutputDirectory;
 110  
 
 111  
     /**
 112  
      * Specifies the filename that will be used for the generated jar file. Please note that <code>-javadoc</code>
 113  
      * or <code>-test-javadoc</code> will be appended to the file name.
 114  
      */
 115  
     @Parameter( property = "project.build.finalName" )
 116  
     private String finalName;
 117  
 
 118  
     /**
 119  
      * Specifies whether to attach the generated artifact to the project helper.
 120  
      * <br/>
 121  
      */
 122  
     @Parameter( property = "attach", defaultValue = "true" )
 123  
     private boolean attach;
 124  
 
 125  
     /**
 126  
      * The archive configuration to use.
 127  
      * See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven Archiver Reference</a>.
 128  
      *
 129  
      * @since 2.5
 130  
      */
 131  2
     @Parameter
 132  
     private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
 133  
 
 134  
     /**
 135  
      * Path to the default MANIFEST file to use. It will be used if
 136  
      * <code>useDefaultManifestFile</code> is set to <code>true</code>.
 137  
      *
 138  
      * @since 2.5
 139  
      */
 140  
     @Parameter( defaultValue = "${project.build.outputDirectory}/META-INF/MANIFEST.MF", required = true,
 141  
                 readonly = true )
 142  
     private File defaultManifestFile;
 143  
 
 144  
     /**
 145  
      * Set this to <code>true</code> to enable the use of the <code>defaultManifestFile</code>.
 146  
      * <br/>
 147  
      *
 148  
      * @since 2.5
 149  
      */
 150  
     @Parameter( defaultValue = "false" )
 151  
     private boolean useDefaultManifestFile;
 152  
 
 153  
     /** {@inheritDoc} */
 154  
     public void execute()
 155  
         throws MojoExecutionException
 156  
     {
 157  2
         if ( skip )
 158  
         {
 159  0
             getLog().info( "Skipping javadoc generation" );
 160  0
             return;
 161  
         }
 162  
 
 163  2
         File innerDestDir = this.destDir;
 164  2
         if ( innerDestDir == null )
 165  
         {
 166  1
             innerDestDir = new File( getOutputDirectory() );
 167  
         }
 168  
 
 169  2
         if ( !( "pom".equalsIgnoreCase( project.getPackaging() ) && isAggregator() ) )
 170  
         {
 171  2
             ArtifactHandler artifactHandler = project.getArtifact().getArtifactHandler();
 172  2
             if ( !"java".equals( artifactHandler.getLanguage() ) )
 173  
             {
 174  0
                 getLog().info( "Not executing Javadoc as the project is not a Java classpath-capable package" );
 175  0
                 return;
 176  
             }
 177  
         }
 178  
 
 179  
         try
 180  
         {
 181  2
             executeReport( Locale.getDefault() );
 182  
 
 183  2
             if ( innerDestDir.exists() )
 184  
             {
 185  1
                 File outputFile = generateArchive( innerDestDir, finalName + "-" + getClassifier() + ".jar" );
 186  
 
 187  1
                 if ( !attach )
 188  
                 {
 189  0
                     getLog().info( "NOT adding javadoc to attached artifacts list." );
 190  
                 }
 191  
                 else
 192  
                 {
 193  
                     // TODO: these introduced dependencies on the project are going to become problematic - can we export it
 194  
                     //  through metadata instead?
 195  1
                     projectHelper.attachArtifact( project, "javadoc", getClassifier(), outputFile );
 196  
                 }
 197  
             }
 198  
         }
 199  0
         catch ( ArchiverException e )
 200  
         {
 201  0
             failOnError( "ArchiverException: Error while creating archive", e );
 202  
         }
 203  0
         catch ( IOException e )
 204  
         {
 205  0
             failOnError( "IOException: Error while creating archive", e );
 206  
         }
 207  0
         catch ( MavenReportException e )
 208  
         {
 209  0
             failOnError( "MavenReportException: Error while creating archive", e );
 210  
         }
 211  0
         catch ( RuntimeException e )
 212  
         {
 213  0
             failOnError( "RuntimeException: Error while creating archive", e );
 214  2
         }
 215  2
     }
 216  
 
 217  
     // ----------------------------------------------------------------------
 218  
     // Protected methods
 219  
     // ----------------------------------------------------------------------
 220  
 
 221  
     /**
 222  
      * @return the wanted classifier, i.e. <code>javadoc</code> or <code>test-javadoc</code>
 223  
      */
 224  
     protected String getClassifier()
 225  
     {
 226  2
         return "javadoc";
 227  
     }
 228  
 
 229  
     // ----------------------------------------------------------------------
 230  
     // private methods
 231  
     // ----------------------------------------------------------------------
 232  
 
 233  
     /**
 234  
      * Method that creates the jar file
 235  
      *
 236  
      * @param javadocFiles the directory where the generated jar file will be put
 237  
      * @param jarFileName the filename of the generated jar file
 238  
      * @return a File object that contains the generated jar file
 239  
      * @throws ArchiverException if any
 240  
      * @throws IOException if any
 241  
      */
 242  
     private File generateArchive( File javadocFiles, String jarFileName )
 243  
         throws ArchiverException, IOException
 244  
     {
 245  1
         File javadocJar = new File( jarOutputDirectory, jarFileName );
 246  
 
 247  1
         if ( javadocJar.exists() )
 248  
         {
 249  1
             javadocJar.delete();
 250  
         }
 251  
 
 252  1
         MavenArchiver archiver = new MavenArchiver();
 253  1
         archiver.setArchiver( jarArchiver );
 254  1
         archiver.setOutputFile( javadocJar );
 255  
 
 256  1
         File contentDirectory = javadocFiles;
 257  1
         if ( !contentDirectory.exists() )
 258  
         {
 259  0
             getLog().warn( "JAR will be empty - no content was marked for inclusion!" );
 260  
         }
 261  
         else
 262  
         {
 263  1
             archiver.getArchiver().addDirectory( contentDirectory, DEFAULT_INCLUDES, DEFAULT_EXCLUDES );
 264  
         }
 265  
 
 266  1
         List<Resource> resources = project.getBuild().getResources();
 267  
 
 268  1
         for ( Resource r : resources )
 269  
         {
 270  0
             if ( r.getDirectory().endsWith( "maven-shared-archive-resources" ) )
 271  
             {
 272  0
                 archiver.getArchiver().addDirectory( new File( r.getDirectory() ) );
 273  
             }
 274  
         }
 275  
 
 276  1
         if ( useDefaultManifestFile && defaultManifestFile.exists() && archive.getManifestFile() == null )
 277  
         {
 278  0
             getLog().info( "Adding existing MANIFEST to archive. Found under: " + defaultManifestFile.getPath() );
 279  0
             archive.setManifestFile( defaultManifestFile );
 280  
         }
 281  
 
 282  
         try
 283  
         {
 284  
             // we don't want Maven stuff
 285  1
             archive.setAddMavenDescriptor( false );
 286  1
             archiver.createArchive( project, archive );
 287  
         }
 288  0
         catch ( ManifestException e )
 289  
         {
 290  0
             throw new ArchiverException( "ManifestException: " + e.getMessage(), e );
 291  
         }
 292  0
         catch ( DependencyResolutionRequiredException e )
 293  
         {
 294  0
             throw new ArchiverException( "DependencyResolutionRequiredException: " + e.getMessage(), e );
 295  1
         }
 296  
 
 297  1
         return javadocJar;
 298  
     }
 299  
 }