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