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