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      /**
53       * Bundle options path.
54       */
55      public static final String BUNDLE_OPTIONS_PATH = "META-INF/maven/javadoc-options.xml";
56  
57      /**
58       * Resources directory path.
59       */
60      public static final String RESOURCES_DIR_PATH = "resources";
61  
62      /**
63       * Base name of artifacts produced by this project. This will be combined with
64       * {@link ResourcesBundleMojo#getAttachmentClassifier()} to produce the name for this bundle
65       * jar.
66       */
67      @Parameter( defaultValue = "${project.build.finalName}", readonly = true )
68      private String finalName;
69  
70      /**
71       * Helper component to provide an easy mechanism for attaching an artifact to the project for
72       * installation/deployment.
73       */
74      @Component
75      private MavenProjectHelper projectHelper;
76  
77      /**
78       * Archiver manager, used to manage jar builder.
79       */
80      @Component
81      private ArchiverManager archiverManager;
82  
83      /**
84       * Assemble a new {@link org.apache.maven.plugin.javadoc.options.JavadocOptions JavadocOptions} instance that
85       * contains the configuration options in this
86       * mojo, which are a subset of those provided in derivatives of the {@link AbstractJavadocMojo}
87       * class (most of the javadoc mojos, in other words). Then, bundle the contents of the 
88       * <code>javadocDirectory</code> along with the assembled JavadocOptions instance (serialized to
89       * META-INF/maven/javadoc-options.xml) into a project attachment for installation/deployment.
90       * 
91       * {@inheritDoc}
92       * @see org.apache.maven.plugin.Mojo#execute()
93       */
94      public void execute()
95          throws MojoExecutionException, MojoFailureException
96      {
97          try
98          {
99              buildJavadocOptions();
100         }
101         catch ( IOException e )
102         {
103             throw new MojoExecutionException( "Failed to generate javadoc-options file: " + e.getMessage(), e );
104         }
105         
106         Archiver archiver;
107         try
108         {
109             archiver = archiverManager.getArchiver( "jar" );
110         }
111         catch ( NoSuchArchiverException e )
112         {
113             throw new MojoExecutionException( "Failed to retrieve jar archiver component from manager.", e );
114         }
115         
116         File optionsFile = getJavadocOptionsFile();
117         File bundleFile =
118             new File( getProject().getBuild().getDirectory(), finalName + "-" + getAttachmentClassifier() + ".jar" );
119         try
120         {
121             archiver.addFile( optionsFile, BUNDLE_OPTIONS_PATH );
122             
123             File javadocDir = getJavadocDirectory();
124             if ( javadocDir.exists() && javadocDir.isDirectory() )
125             {
126                 archiver.addDirectory( javadocDir, RESOURCES_DIR_PATH );
127             }
128             
129             archiver.setDestFile( bundleFile );
130             archiver.createArchive();
131         }
132         catch ( ArchiverException e )
133         {
134             throw new MojoExecutionException( "Failed to assemble javadoc-resources bundle archive. Reason: "
135                 + e.getMessage(), e );
136         }
137         catch ( IOException e )
138         {
139             throw new MojoExecutionException( "Failed to assemble javadoc-resources bundle archive. Reason: "
140                 + e.getMessage(), e );
141         }
142         
143         projectHelper.attachArtifact( getProject(), bundleFile, getAttachmentClassifier() );
144     }
145 }