View Javadoc
1   package org.apache.maven.plugins.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.plugins.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      @Override
95      public void doExecute()
96          throws MojoExecutionException, MojoFailureException
97      {
98          try
99          {
100             buildJavadocOptions();
101         }
102         catch ( IOException e )
103         {
104             throw new MojoExecutionException( "Failed to generate javadoc-options file: " + e.getMessage(), e );
105         }
106 
107         Archiver archiver;
108         try
109         {
110             archiver = archiverManager.getArchiver( "jar" );
111         }
112         catch ( NoSuchArchiverException e )
113         {
114             throw new MojoExecutionException( "Failed to retrieve jar archiver component from manager.", e );
115         }
116 
117         File optionsFile = getJavadocOptionsFile();
118         File bundleFile =
119             new File( getProject().getBuild().getDirectory(), finalName + "-" + getAttachmentClassifier() + ".jar" );
120         try
121         {
122             archiver.addFile( optionsFile, BUNDLE_OPTIONS_PATH );
123 
124             File javadocDir = getJavadocDirectory();
125             if ( javadocDir.exists() && javadocDir.isDirectory() )
126             {
127                 archiver.addDirectory( javadocDir, RESOURCES_DIR_PATH + "/" );
128             }
129 
130             archiver.setDestFile( bundleFile );
131             archiver.createArchive();
132         }
133         catch ( ArchiverException | IOException e )
134         {
135             throw new MojoExecutionException( "Failed to assemble javadoc-resources bundle archive. Reason: "
136                 + e.getMessage(), e );
137         }
138 
139         projectHelper.attachArtifact( getProject(), bundleFile, getAttachmentClassifier() );
140     }
141 }