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 public void execute() throws MojoExecutionException, MojoFailureException {
123 if (skip) {
124 getLog().info("maven.site.skip = true: Skipping jar generation");
125 return;
126 }
127
128 super.execute();
129
130 try {
131 File outputFile =
132 createArchive(outputDirectory, finalName + "-" + getClassifier() + "." + getArtifactType());
133
134 if (attach) {
135 projectHelper.attachArtifact(project, getArtifactType(), getClassifier(), outputFile);
136 } else {
137 getLog().info("NOT adding site jar to the list of attached artifacts.");
138 }
139 } catch (ArchiverException | IOException | ManifestException | DependencyResolutionRequiredException e) {
140 throw new MojoExecutionException("Error while creating archive", e);
141 }
142 }
143
144 protected String getArtifactType() {
145 return "jar";
146 }
147
148 protected String getClassifier() {
149 return "site";
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163 private File createArchive(File siteDirectory, String jarFilename)
164 throws ArchiverException, IOException, ManifestException, DependencyResolutionRequiredException {
165 File siteJar = new File(jarOutputDirectory, jarFilename);
166
167 MavenArchiver archiver = new MavenArchiver();
168 archiver.setCreatedBy("Maven Site Plugin", "org.apache.maven.plugins", "maven-site-plugin");
169
170 archiver.setArchiver(this.jarArchiver);
171
172 archiver.setOutputFile(siteJar);
173
174
175 archiver.configureReproducibleBuild(outputTimestamp);
176
177 if (!siteDirectory.isDirectory()) {
178 getLog().warn("JAR will be empty - no content was marked for inclusion!");
179 } else {
180 archiver.getArchiver().addDirectory(siteDirectory, getArchiveIncludes(), getArchiveExcludes());
181 }
182
183 archiver.createArchive(getSession(), getProject(), archive);
184
185 return siteJar;
186 }
187
188 private String[] getArchiveIncludes() {
189 if (this.archiveIncludes != null && this.archiveIncludes.length > 0) {
190 return this.archiveIncludes;
191 }
192
193 return DEFAULT_ARCHIVE_INCLUDES;
194 }
195
196 private String[] getArchiveExcludes() {
197 if (this.archiveExcludes != null && this.archiveExcludes.length > 0) {
198 return this.archiveExcludes;
199 }
200 return DEFAULT_ARCHIVE_EXCLUDES;
201 }
202 }