View Javadoc

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