|
13 | 13 | */
|
14 | 14 | package org.skife.waffles;
|
15 | 15 |
|
| 16 | +import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream; |
| 17 | +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; |
| 18 | +import org.apache.commons.compress.archivers.zip.ZipArchiveEntryPredicate; |
| 19 | +import org.apache.commons.compress.archivers.zip.ZipFile; |
16 | 20 | import org.apache.maven.artifact.Artifact;
|
17 | 21 | import org.apache.maven.plugin.AbstractMojo;
|
18 | 22 | import org.apache.maven.plugin.MojoExecutionException;
|
|
25 | 29 | import org.codehaus.plexus.util.FileUtils;
|
26 | 30 | import org.codehaus.plexus.util.IOUtil;
|
27 | 31 |
|
| 32 | +import java.io.ByteArrayOutputStream; |
28 | 33 | import java.io.File;
|
29 |
| -import java.io.FileOutputStream; |
30 | 34 | import java.io.IOException;
|
31 | 35 | import java.io.InputStream;
|
| 36 | +import java.net.URI; |
32 | 37 | import java.net.URL;
|
33 | 38 | import java.net.URLClassLoader;
|
34 | 39 | import java.nio.file.Files;
|
|
37 | 42 | import java.util.ArrayList;
|
38 | 43 | import java.util.List;
|
39 | 44 |
|
| 45 | +import static java.lang.String.format; |
| 46 | +import static java.nio.charset.StandardCharsets.US_ASCII; |
| 47 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 48 | +import static java.nio.file.Files.readAllBytes; |
| 49 | +import static org.apache.commons.compress.archivers.zip.Zip64Mode.AlwaysWithCompatibility; |
| 50 | + |
40 | 51 | /**
|
41 | 52 | * Make an artifact generated by the build really executable. The resulting artifact
|
42 | 53 | * can be run directly from the command line (Java must be installed and in the
|
@@ -162,39 +173,52 @@ private void makeExecutable(File file)
|
162 | 173 | {
|
163 | 174 | getLog().debug("Making " + file.getAbsolutePath() + " executable");
|
164 | 175 |
|
165 |
| - Path original = Paths.get(file.getAbsolutePath() + ".rx-orig"); |
| 176 | + Path original = Paths.get(file.getAbsolutePath() + ".original"); |
166 | 177 | Files.move(file.toPath(), original);
|
167 |
| - try (final FileOutputStream out = new FileOutputStream(file); |
168 |
| - final InputStream in = Files.newInputStream(original)) { |
| 178 | + try (JarArchiveOutputStream jar = new JarArchiveOutputStream(Files.newOutputStream(file.toPath()))) { |
| 179 | + jar.writePreamble(getPreamble(original.toUri())); |
| 180 | + jar.setUseZip64(AlwaysWithCompatibility); |
| 181 | + jar.setEncoding(UTF_8.name()); |
| 182 | + |
| 183 | + try (ZipFile zip = new ZipFile(original.toFile())) { |
| 184 | + zip.copyRawEntries(jar, zipArchiveEntry -> true); |
| 185 | + } |
| 186 | + jar.flush(); |
| 187 | + jar.finish(); |
| 188 | + } |
| 189 | + finally { |
| 190 | + Files.deleteIfExists(original); |
| 191 | + } |
| 192 | + |
| 193 | + if (!file.setExecutable(true, false)) { |
| 194 | + throw new MojoExecutionException(format("Could not make JAR [%s] executable", file.getAbsolutePath())); |
| 195 | + } |
| 196 | + getLog().info(format("Successfully made JAR [%s] executable", file.getAbsolutePath())); |
| 197 | + } |
169 | 198 |
|
| 199 | + private byte[] getPreamble(URI uri) throws MojoExecutionException |
| 200 | + { |
| 201 | + try |
| 202 | + { |
170 | 203 | if (scriptFile == null) {
|
171 |
| - out.write(("#!/bin/sh\n\nexec java " + flags + " -jar \"$0\" \"$@\"\n\n").getBytes("ASCII")); |
| 204 | + return ("#!/bin/sh\n\nexec java " + flags + " -jar \"$0\" \"$@\"\n\n").getBytes(UTF_8); |
172 | 205 | }
|
173 | 206 | else if (Files.exists(Paths.get(scriptFile))) {
|
174 |
| - getLog().debug(String.format("Loading file[%s] from filesystem", scriptFile)); |
175 |
| - |
176 |
| - byte[] script = Files.readAllBytes(Paths.get(scriptFile)); |
177 |
| - out.write(script); |
178 |
| - out.write(new byte[]{'\n', '\n'}); |
| 207 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 208 | + outputStream.write(readAllBytes(Paths.get(scriptFile))); |
| 209 | + outputStream.write("\n\n".getBytes(US_ASCII)); |
| 210 | + return outputStream.toByteArray(); |
179 | 211 | }
|
180 |
| - else { |
181 |
| - getLog().debug(String.format("Loading file[%s] from jar[%s]", scriptFile, original)); |
182 |
| - |
183 |
| - try (final URLClassLoader loader = new URLClassLoader(new URL[]{original.toUri().toURL()}, null); |
184 |
| - final InputStream scriptIn = loader.getResourceAsStream(scriptFile)) { |
185 | 212 |
|
186 |
| - out.write(IOUtil.toString(scriptIn).getBytes("ASCII")); |
187 |
| - out.write("\n\n".getBytes("ASCII")); |
188 |
| - } |
| 213 | + try (final URLClassLoader loader = new URLClassLoader(new URL[]{uri.toURL()}, null); final InputStream scriptIn = loader.getResourceAsStream(scriptFile)) { |
| 214 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 215 | + outputStream.write(IOUtil.toString(scriptIn).getBytes(UTF_8)); |
| 216 | + outputStream.write("\n\n".getBytes(UTF_8)); |
| 217 | + return outputStream.toByteArray(); |
189 | 218 | }
|
190 |
| - IOUtil.copy(in, out); |
191 | 219 | }
|
192 |
| - finally { |
193 |
| - Files.deleteIfExists(original); |
| 220 | + catch (IOException e) { |
| 221 | + throw new MojoExecutionException(e.getMessage()); |
194 | 222 | }
|
195 |
| - |
196 |
| - file.setExecutable(true, false); |
197 |
| - |
198 |
| - getLog().info(String.format("Successfully made JAR [%s] executable", file.getAbsolutePath())); |
199 | 223 | }
|
200 | 224 | }
|
0 commit comments