1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.release.plugin.mojos;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.nio.file.Files;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.zip.ZipEntry;
27 import java.util.zip.ZipOutputStream;
28
29 import org.apache.commons.io.IOUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.maven.plugin.AbstractMojo;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.MojoFailureException;
34 import org.apache.maven.plugins.annotations.LifecyclePhase;
35 import org.apache.maven.plugins.annotations.Mojo;
36 import org.apache.maven.plugins.annotations.Parameter;
37
38
39
40
41
42
43
44
45
46 @Deprecated
47 @Mojo(name = "compress-site",
48 defaultPhase = LifecyclePhase.POST_SITE,
49 threadSafe = true,
50 aggregator = true)
51 public class CommonsSiteCompressionMojo extends AbstractMojo {
52
53
54
55
56
57 @Parameter(defaultValue = "${project.build.directory}/commons-release-plugin",
58 property = "commons.outputDirectory")
59 private File workingDirectory;
60
61
62
63 @Parameter(defaultValue = "${project.build.directory}/site", property = "commons.siteOutputDirectory")
64 private File siteDirectory;
65
66
67
68
69
70
71
72 @Parameter(defaultValue = "", property = "commons.distSvnStagingUrl")
73 private String distSvnStagingUrl;
74
75
76
77
78 @Parameter(defaultValue = "false", property = "commons.release.isDistModule")
79 private Boolean isDistModule;
80
81
82
83
84 private List<File> filesToCompress;
85
86 @Override
87 public void execute() throws MojoExecutionException, MojoFailureException {
88 if (!isDistModule) {
89 getLog().info("This module is marked as a non distribution "
90 + "or assembly module, and the plugin will not run.");
91 return;
92 }
93 if (StringUtils.isEmpty(distSvnStagingUrl)) {
94 getLog().warn("commons.distSvnStagingUrl is not set, the commons-release-plugin will not run.");
95 return;
96 }
97 if (!siteDirectory.exists()) {
98 getLog().error("\"mvn site\" was not run before this goal, or a siteDirectory did not exist.");
99 throw new MojoFailureException(
100 "\"mvn site\" was not run before this goal, or a siteDirectory did not exist."
101 );
102 }
103 if (!workingDirectory.exists()) {
104 getLog().info("Current project contains no distributions. Not executing.");
105 return;
106 }
107 try {
108 filesToCompress = new ArrayList<>();
109 getAllSiteFiles(siteDirectory, filesToCompress);
110 writeZipFile(workingDirectory, siteDirectory, filesToCompress);
111 } catch (final IOException e) {
112 getLog().error("Failed to create ./target/commons-release-plugin/site.zip: " + e.getMessage(), e);
113 throw new MojoExecutionException(
114 "Failed to create ./target/commons-release-plugin/site.zip: " + e.getMessage(),
115 e
116 );
117 }
118 }
119
120
121
122
123
124
125
126
127 private void getAllSiteFiles(final File siteDirectory, final List<File> filesToCompress) {
128 final File[] files = siteDirectory.listFiles();
129 for (final File file : files) {
130 filesToCompress.add(file);
131 if (file.isDirectory()) {
132 getAllSiteFiles(file, filesToCompress);
133 }
134 }
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148 private void writeZipFile(final File outputDirectory, final File directoryToZip, final List<File> fileList)
149 throws IOException {
150 try (OutputStream fos = Files.newOutputStream(new File(outputDirectory.getAbsolutePath() + "/site.zip")
151 .toPath());
152 ZipOutputStream zos = new ZipOutputStream(fos)) {
153 for (final File file : fileList) {
154 if (!file.isDirectory()) {
155 addToZip(directoryToZip, file, zos);
156 }
157 }
158 }
159 }
160
161
162
163
164
165
166
167
168
169
170
171 private void addToZip(final File directoryToZip, final File file, final ZipOutputStream zos) throws IOException {
172 try (InputStream fis = Files.newInputStream(file.toPath())) {
173
174
175 final String zipFilePath = file.getCanonicalPath().substring(
176 directoryToZip.getCanonicalPath().length() + 1,
177 file.getCanonicalPath().length());
178 final ZipEntry zipEntry = new ZipEntry(zipFilePath);
179 zos.putNextEntry(zipEntry);
180 IOUtils.copy(fis, zos);
181 }
182 }
183 }