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.plugins.annotations.Component;
25  import org.apache.maven.plugins.annotations.LifecyclePhase;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.plugins.annotations.ResolutionScope;
29  import org.apache.maven.project.MavenProjectHelper;
30  import org.codehaus.plexus.archiver.Archiver;
31  import org.codehaus.plexus.archiver.ArchiverException;
32  import org.codehaus.plexus.archiver.manager.ArchiverManager;
33  import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
34  
35  import java.io.File;
36  import java.io.IOException;
37  
38  /**
39   * Bundle {@link AbstractJavadocMojo#javadocDirectory}, along with javadoc configuration options such
40   * as taglet, doclet, and link information into a deployable artifact. This artifact can then be consumed
41   * by the javadoc plugin mojos when used by the <code>includeDependencySources</code> option, to generate
42   * javadocs that are somewhat consistent with those generated in the original project itself.
43   *  
44   * @since 2.7
45   */
46  @Mojo( name = "resource-bundle", defaultPhase = LifecyclePhase.PACKAGE,
47         requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true )
48  public class ResourcesBundleMojo
49  extends AbstractJavadocMojo
50  {
51  
52      public static final String BUNDLE_OPTIONS_PATH = "META-INF/maven/javadoc-options.xml";
53  
54      public static final String RESOURCES_DIR_PATH = "resources";
55  
56      /**
57       * Base name of artifacts produced by this project. This will be combined with
58       * {@link ResourcesBundleMojo#getAttachmentClassifier()} to produce the name for this bundle
59       * jar.
60       */
61      @Parameter( defaultValue = "${project.build.finalName}", readonly = true )
62      private String finalName;
63  
64      /**
65       * Helper component to provide an easy mechanism for attaching an artifact to the project for
66       * installation/deployment.
67       */
68      @Component
69      private MavenProjectHelper projectHelper;
70  
71      /**
72       * Archiver manager, used to manage jar builder.
73       */
74      @Component
75      private ArchiverManager archiverManager;
76  
77      /**
78       * Assemble a new {@link org.apache.maven.plugin.javadoc.options.JavadocOptions JavadocOptions} instance that
79       * 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 }