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  
20  package org.apache.maven.plugin.javadoc;
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.MojoFailureException;
24  import org.apache.maven.plugin.javadoc.options.JavadocOptions;
25  import org.apache.maven.project.MavenProjectHelper;
26  import org.codehaus.plexus.archiver.Archiver;
27  import org.codehaus.plexus.archiver.ArchiverException;
28  import org.codehaus.plexus.archiver.manager.ArchiverManager;
29  import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
30  
31  import java.io.File;
32  import java.io.IOException;
33  
34  /**
35   * Bundle {@link AbstractJavadocMojo#javadocDirectory}, along with javadoc configuration options such
36   * as taglet, doclet, and link information into a deployable artifact. This artifact can then be consumed
37   * by the javadoc plugin mojos when used by the <code>includeDependencySources</code> option, to generate
38   * javadocs that are somewhat consistent with those generated in the original project itself.
39   *  
40   * @goal resource-bundle
41   * @phase package
42   * @since 2.7
43   */
44  public class ResourcesBundleMojo
45      extends AbstractJavadocMojo
46  {
47      
48      public static final String BUNDLE_OPTIONS_PATH = "META-INF/maven/javadoc-options.xml";
49  
50      public static final String RESOURCES_DIR_PATH = "resources";
51  
52      /**
53       * Base name of artifacts produced by this project. This will be combined with 
54       * {@link ResourcesBundleMojo#getAttachmentClassifier()} to produce the name for this bundle 
55       * jar.
56       * 
57       * @parameter default-value="${project.build.finalName}"
58       * @readonly
59       */
60      private String finalName;
61      
62      /**
63       * Helper component to provide an easy mechanism for attaching an artifact to the project for 
64       * installation/deployment.
65       * 
66       * @component
67       */
68      private MavenProjectHelper projectHelper;
69      
70      /**
71       * Archiver manager, used to manage jar builder.
72       *
73       * @component
74       */
75      private ArchiverManager archiverManager;
76  
77      /**
78       * Assemble a new {@link JavadocOptions} instance that contains the configuration options in this
79       * mojo, which are a subset of those provided in derivatives of the {@link AbstractJavadocMojo}
80       * class (most of the javadoc mojos, in other words). Then, bundle the contents of the 
81       * <code>javadocDirectory</code> along with the assembled JavadocOptions instance (serialized to
82       * META-INF/maven/javadoc-options.xml) into a project attachment for installation/deployment.
83       * 
84       * {@inheritDoc}
85       * @see org.apache.maven.plugin.Mojo#execute()
86       */
87      public void execute()
88          throws MojoExecutionException, MojoFailureException
89      {
90          try
91          {
92              buildJavadocOptions();
93          }
94          catch ( IOException e )
95          {
96              throw new MojoExecutionException( "Failed to generate javadoc-options file: " + e.getMessage(), e );
97          }
98          
99          Archiver archiver;
100         try
101         {
102             archiver = archiverManager.getArchiver( "jar" );
103         }
104         catch ( NoSuchArchiverException e )
105         {
106             throw new MojoExecutionException( "Failed to retrieve jar archiver component from manager.", e );
107         }
108         
109         File optionsFile = getJavadocOptionsFile();
110         File bundleFile = new File( getProject().getBuild().getDirectory(), finalName + "-" + getAttachmentClassifier() + ".jar" );
111         try
112         {
113             archiver.addFile( optionsFile, BUNDLE_OPTIONS_PATH );
114             
115             File javadocDir = getJavadocDirectory();
116             if ( javadocDir.exists() && javadocDir.isDirectory() )
117             {
118                 archiver.addDirectory( javadocDir, RESOURCES_DIR_PATH );
119             }
120             
121             archiver.setDestFile( bundleFile );
122             archiver.createArchive();
123         }
124         catch ( ArchiverException e )
125         {
126             throw new MojoExecutionException( "Failed to assemble javadoc-resources bundle archive. Reason: " + e.getMessage(), e );
127         }
128         catch ( IOException e )
129         {
130             throw new MojoExecutionException( "Failed to assemble javadoc-resources bundle archive. Reason: " + e.getMessage(), e );
131         }
132         
133         projectHelper.attachArtifact( getProject(), bundleFile, getAttachmentClassifier() );
134     }
135 }