1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
47
48
49
50
51
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
62
63 public static final String BUNDLE_OPTIONS_PATH = "META-INF/maven/javadoc-options.xml";
64
65
66
67
68 public static final String RESOURCES_DIR_PATH = "resources";
69
70
71
72
73
74
75 @Parameter(defaultValue = "${project.build.finalName}", readonly = true)
76 private String finalName;
77
78
79
80
81
82 private MavenProjectHelper projectHelper;
83
84
85
86
87 private ArchiverManager archiverManager;
88
89
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
112
113
114
115
116
117
118
119
120
121
122
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 }