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  import org.codehaus.plexus.archiver.util.DefaultFileSet;
35  
36  import java.io.File;
37  import java.io.IOException;
38  
39  /**
40   * Bundle {@link AbstractJavadocMojo#javadocDirectory}, along with javadoc configuration options such
41   * as taglet, doclet, and link information into a deployable artifact. This artifact can then be consumed
42   * by the javadoc plugin mojos when used by the <code>includeDependencySources</code> option, to generate
43   * javadocs that are somewhat consistent with those generated in the original project itself.
44   *
45   * @since 2.7
46   */
47  @Mojo( name = "resource-bundle", defaultPhase = LifecyclePhase.PACKAGE,
48         requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true )
49  public class ResourcesBundleMojo
50  extends AbstractJavadocMojo
51  {
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 ResourcesBundleMojo#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()
97          throws MojoExecutionException, MojoFailureException
98      {
99          try
100         {
101             buildJavadocOptions();
102         }
103         catch ( IOException e )
104         {
105             throw new MojoExecutionException( "Failed to generate javadoc-options file: " + e.getMessage(), e );
106         }
107 
108         Archiver archiver;
109         try
110         {
111             archiver = archiverManager.getArchiver( "jar" );
112         }
113         catch ( NoSuchArchiverException e )
114         {
115             throw new MojoExecutionException( "Failed to retrieve jar archiver component from manager.", e );
116         }
117 
118         File optionsFile = getJavadocOptionsFile();
119         File bundleFile =
120             new File( getProject().getBuild().getDirectory(), finalName + "-" + getAttachmentClassifier() + ".jar" );
121         try
122         {
123             archiver.addFile( optionsFile, BUNDLE_OPTIONS_PATH );
124 
125             File javadocDir = getJavadocDirectory();
126             if ( javadocDir.isDirectory() )
127             {
128                 DefaultFileSet fileSet = DefaultFileSet.fileSet( javadocDir ).prefixed( RESOURCES_DIR_PATH + "/" );
129                 archiver.addFileSet( fileSet );
130             }
131 
132             archiver.setDestFile( bundleFile );
133             archiver.createArchive();
134         }
135         catch ( ArchiverException | IOException e )
136         {
137             throw new MojoExecutionException( "Failed to assemble javadoc-resources bundle archive. Reason: "
138                 + e.getMessage(), e );
139         }
140 
141         projectHelper.attachArtifact( getProject(), bundleFile, getAttachmentClassifier() );
142     }
143 }