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.plugin.javadoc.options.JavadocOptions;
25 import org.apache.maven.project.MavenProjectHelper;
26 import org.codehaus.plexus.archiver.Archiver;
27 import org.codehaus.plexus.archiver.ArchiverException;
28 import org.codehaus.plexus.archiver.manager.ArchiverManager;
29 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
30
31 import java.io.File;
32 import java.io.IOException;
33
34 /**
35 * Bundle {@link AbstractJavadocMojo#javadocDirectory}, along with javadoc configuration options such
36 * as taglet, doclet, and link information into a deployable artifact. This artifact can then be consumed
37 * by the javadoc plugin mojos when used by the <code>includeDependencySources</code> option, to generate
38 * javadocs that are somewhat consistent with those generated in the original project itself.
39 *
40 * @goal resource-bundle
41 * @phase package
42 * @since 2.7
43 */
44 public class ResourcesBundleMojo
45 extends AbstractJavadocMojo
46 {
47
48 public static final String BUNDLE_OPTIONS_PATH = "META-INF/maven/javadoc-options.xml";
49
50 public static final String RESOURCES_DIR_PATH = "resources";
51
52 /**
53 * Base name of artifacts produced by this project. This will be combined with
54 * {@link ResourcesBundleMojo#getAttachmentClassifier()} to produce the name for this bundle
55 * jar.
56 *
57 * @parameter default-value="${project.build.finalName}"
58 * @readonly
59 */
60 private String finalName;
61
62 /**
63 * Helper component to provide an easy mechanism for attaching an artifact to the project for
64 * installation/deployment.
65 *
66 * @component
67 */
68 private MavenProjectHelper projectHelper;
69
70 /**
71 * Archiver manager, used to manage jar builder.
72 *
73 * @component
74 */
75 private ArchiverManager archiverManager;
76
77 /**
78 * Assemble a new {@link JavadocOptions} instance that contains the configuration options in this
79 * mojo, which are a subset of those provided in derivatives of the {@link AbstractJavadocMojo}
80 * class (most of the javadoc mojos, in other words). Then, bundle the contents of the
81 * <code>javadocDirectory</code> along with the assembled JavadocOptions instance (serialized to
82 * META-INF/maven/javadoc-options.xml) into a project attachment for installation/deployment.
83 *
84 * {@inheritDoc}
85 * @see org.apache.maven.plugin.Mojo#execute()
86 */
87 public void execute()
88 throws MojoExecutionException, MojoFailureException
89 {
90 try
91 {
92 buildJavadocOptions();
93 }
94 catch ( IOException e )
95 {
96 throw new MojoExecutionException( "Failed to generate javadoc-options file: " + e.getMessage(), e );
97 }
98
99 Archiver archiver;
100 try
101 {
102 archiver = archiverManager.getArchiver( "jar" );
103 }
104 catch ( NoSuchArchiverException e )
105 {
106 throw new MojoExecutionException( "Failed to retrieve jar archiver component from manager.", e );
107 }
108
109 File optionsFile = getJavadocOptionsFile();
110 File bundleFile =
111 new File( getProject().getBuild().getDirectory(), finalName + "-" + getAttachmentClassifier() + ".jar" );
112 try
113 {
114 archiver.addFile( optionsFile, BUNDLE_OPTIONS_PATH );
115
116 File javadocDir = getJavadocDirectory();
117 if ( javadocDir.exists() && javadocDir.isDirectory() )
118 {
119 archiver.addDirectory( javadocDir, RESOURCES_DIR_PATH );
120 }
121
122 archiver.setDestFile( bundleFile );
123 archiver.createArchive();
124 }
125 catch ( ArchiverException e )
126 {
127 throw new MojoExecutionException( "Failed to assemble javadoc-resources bundle archive. Reason: "
128 + e.getMessage(), e );
129 }
130 catch ( IOException e )
131 {
132 throw new MojoExecutionException( "Failed to assemble javadoc-resources bundle archive. Reason: "
133 + e.getMessage(), e );
134 }
135
136 projectHelper.attachArtifact( getProject(), bundleFile, getAttachmentClassifier() );
137 }
138 }