1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.site.render;
20
21 import java.io.File;
22 import java.io.IOException;
23
24 import org.apache.maven.archiver.MavenArchiveConfiguration;
25 import org.apache.maven.archiver.MavenArchiver;
26 import org.apache.maven.artifact.DependencyResolutionRequiredException;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.MojoFailureException;
29 import org.apache.maven.plugins.annotations.Component;
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.project.MavenProjectHelper;
35 import org.codehaus.plexus.archiver.Archiver;
36 import org.codehaus.plexus.archiver.ArchiverException;
37 import org.codehaus.plexus.archiver.jar.JarArchiver;
38 import org.codehaus.plexus.archiver.jar.ManifestException;
39
40
41
42
43
44
45
46
47
48 @Mojo(
49 name = "jar",
50 defaultPhase = LifecyclePhase.PACKAGE,
51 requiresDependencyResolution = ResolutionScope.TEST,
52 requiresReports = true,
53 threadSafe = true)
54 public class SiteJarMojo extends SiteMojo {
55 private static final String[] DEFAULT_ARCHIVE_EXCLUDES = new String[] {};
56
57 private static final String[] DEFAULT_ARCHIVE_INCLUDES = new String[] {"**/**"};
58
59
60
61
62 @Parameter(property = "project.build.directory", required = true)
63 private String jarOutputDirectory;
64
65
66
67
68
69 @Parameter(property = "project.build.finalName", required = true)
70 private String finalName;
71
72
73
74
75 @Component
76 private MavenProjectHelper projectHelper;
77
78
79
80
81 @Parameter(property = "site.attach", defaultValue = "true")
82 private boolean attach;
83
84
85
86
87
88
89 @Component(role = Archiver.class, hint = "jar")
90 private JarArchiver jarArchiver;
91
92
93
94
95
96
97
98 @Parameter
99 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
100
101
102
103
104
105
106
107 @Parameter
108 private String[] archiveIncludes;
109
110
111
112
113
114
115
116 @Parameter
117 private String[] archiveExcludes;
118
119
120
121
122
123
124
125
126 @Parameter(defaultValue = "${project.build.outputTimestamp}")
127 private String outputTimestamp;
128
129
130
131
132 public void execute() throws MojoExecutionException, MojoFailureException {
133 if (skip) {
134 getLog().info("maven.site.skip = true: Skipping jar generation");
135 return;
136 }
137
138 super.execute();
139
140 try {
141 File outputFile =
142 createArchive(outputDirectory, finalName + "-" + getClassifier() + "." + getArtifactType());
143
144 if (attach) {
145 projectHelper.attachArtifact(project, getArtifactType(), getClassifier(), outputFile);
146 } else {
147 getLog().info("NOT adding site jar to the list of attached artifacts.");
148 }
149 } catch (ArchiverException | IOException | ManifestException | DependencyResolutionRequiredException e) {
150 throw new MojoExecutionException("Error while creating archive.", e);
151 }
152 }
153
154 protected String getArtifactType() {
155 return "jar";
156 }
157
158 protected String getClassifier() {
159 return "site";
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173 private File createArchive(File siteDirectory, String jarFilename)
174 throws ArchiverException, IOException, ManifestException, DependencyResolutionRequiredException {
175 File siteJar = new File(jarOutputDirectory, jarFilename);
176
177 MavenArchiver archiver = new MavenArchiver();
178 archiver.setCreatedBy("Maven Site Plugin", "org.apache.maven.plugins", "maven-site-plugin");
179
180 archiver.setArchiver(this.jarArchiver);
181
182 archiver.setOutputFile(siteJar);
183
184
185 archiver.configureReproducibleBuild(outputTimestamp);
186
187 if (!siteDirectory.isDirectory()) {
188 getLog().warn("JAR will be empty - no content was marked for inclusion!");
189 } else {
190 archiver.getArchiver().addDirectory(siteDirectory, getArchiveIncludes(), getArchiveExcludes());
191 }
192
193 archiver.createArchive(getSession(), getProject(), archive);
194
195 return siteJar;
196 }
197
198 private String[] getArchiveIncludes() {
199 if (this.archiveIncludes != null && this.archiveIncludes.length > 0) {
200 return this.archiveIncludes;
201 }
202
203 return DEFAULT_ARCHIVE_INCLUDES;
204 }
205
206 private String[] getArchiveExcludes() {
207 if (this.archiveExcludes != null && this.archiveExcludes.length > 0) {
208 return this.archiveExcludes;
209 }
210 return DEFAULT_ARCHIVE_EXCLUDES;
211 }
212 }