|
1 | 1 | package io.github.fvarrui.javapackager.gradle;
|
2 | 2 |
|
3 |
| -import java.io.File; |
4 |
| -import java.util.UUID; |
| 3 | +import java.io.*; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.nio.file.Path; |
| 6 | +import java.util.stream.Stream; |
5 | 7 |
|
6 |
| -import org.gradle.api.tasks.bundling.Compression; |
7 |
| -import org.gradle.api.tasks.bundling.Tar; |
| 8 | +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
| 9 | +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; |
| 10 | +import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; |
8 | 11 |
|
9 | 12 | import io.github.fvarrui.javapackager.model.Platform;
|
10 | 13 | import io.github.fvarrui.javapackager.packagers.ArtifactGenerator;
|
11 |
| -import io.github.fvarrui.javapackager.packagers.Context; |
12 | 14 | import io.github.fvarrui.javapackager.packagers.MacPackager;
|
13 | 15 | import io.github.fvarrui.javapackager.packagers.Packager;
|
14 | 16 |
|
@@ -42,69 +44,97 @@ protected File doApply(Packager packager) throws Exception {
|
42 | 44 | String format = ".tar.gz";
|
43 | 45 | File tarFile = new File(outputDirectory, finalName + format);
|
44 | 46 |
|
45 |
| - Tar tarTask = createTarTask(); |
46 |
| - tarTask.setProperty("archiveFileName", tarFile.getName()); |
47 |
| - tarTask.setProperty("destinationDirectory", outputDirectory); |
48 |
| - tarTask.setCompression(Compression.GZIP); |
49 |
| - |
50 |
| - // if zipball is for windows platform |
51 |
| - if (Platform.windows.equals(platform)) { |
52 |
| - |
53 |
| - tarTask.from(appFolder.getParentFile(), copySpec -> { |
54 |
| - copySpec.include(appFolder.getName() + "/**"); |
55 |
| - }); |
56 |
| - |
57 |
| - } |
58 |
| - |
59 |
| - // if zipball is for linux platform |
60 |
| - else if (Platform.linux.equals(platform)) { |
61 |
| - |
62 |
| - tarTask.from(appFolder.getParentFile(), copySpec -> { |
63 |
| - copySpec.include(appFolder.getName() + "/**"); |
64 |
| - copySpec.exclude(appFolder.getName() + "/" + executable.getName()); |
65 |
| - copySpec.exclude(appFolder.getName() + "/" + jreDirectoryName + "/bin/*"); |
66 |
| - copySpec.exclude(appFolder.getName() + "/scripts/*"); |
67 |
| - }); |
68 |
| - tarTask.from(appFolder.getParentFile(), copySpec -> { |
69 |
| - copySpec.include(appFolder.getName() + "/" + executable.getName()); |
70 |
| - copySpec.include(appFolder.getName() + "/" + jreDirectoryName + "/bin/*"); |
71 |
| - copySpec.include(appFolder.getName() + "/scripts/*"); |
72 |
| - copySpec.setFileMode(0755); |
73 |
| - }); |
74 |
| - |
75 |
| - } |
76 |
| - |
77 |
| - // if zipball is for macos platform |
78 |
| - else if (Platform.mac.equals(platform)) { |
79 |
| - |
80 |
| - MacPackager macPackager = (MacPackager) packager; |
81 |
| - File appFile = macPackager.getAppFile(); |
82 |
| - |
83 |
| - tarTask.from(appFolder, copySpec -> { |
84 |
| - copySpec.include(appFile.getName() + "/**"); |
85 |
| - copySpec.exclude(appFile.getName() + "/Contents/MacOS/" + executable.getName()); |
86 |
| - copySpec.exclude(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub"); |
87 |
| - copySpec.exclude(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/*"); |
88 |
| - copySpec.exclude(appFile.getName() + "/Contents/Resources/scripts/*"); |
89 |
| - |
90 |
| - }); |
91 |
| - tarTask.from(appFolder, copySpec -> { |
92 |
| - copySpec.include(appFile.getName() + "/Contents/MacOS/" + executable.getName()); |
93 |
| - copySpec.include(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub"); |
94 |
| - copySpec.include(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/*"); |
95 |
| - copySpec.include(appFile.getName() + "/Contents/Resources/scripts/*"); |
96 |
| - copySpec.setFileMode(0755); |
97 |
| - }); |
98 |
| - |
| 47 | + try (OutputStream fos = Files.newOutputStream(tarFile.toPath()); |
| 48 | + BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 49 | + GzipCompressorOutputStream gcos = new GzipCompressorOutputStream(bos); |
| 50 | + TarArchiveOutputStream tarOut = new TarArchiveOutputStream(gcos)) { |
| 51 | + |
| 52 | + if (Platform.windows.equals(platform)) { |
| 53 | + Path basePath = appFolder.getParentFile().toPath(); |
| 54 | + try (Stream<Path> fileStream = Files.walk(appFolder.toPath())) { |
| 55 | + fileStream.forEach(path -> { |
| 56 | + if (path.equals(tarFile.toPath())) { |
| 57 | + return; |
| 58 | + } |
| 59 | + File file = path.toFile(); |
| 60 | + if (file.isFile()) { |
| 61 | + try { |
| 62 | + TarArchiveEntry entry = new TarArchiveEntry(path.toFile(), basePath.relativize(path).toString()); |
| 63 | + tarOut.putArchiveEntry(entry); |
| 64 | + Files.copy(path, tarOut); |
| 65 | + tarOut.closeArchiveEntry(); |
| 66 | + } catch (IOException e) { |
| 67 | + throw new UncheckedIOException(e); |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + } else if (Platform.linux.equals(platform)) { |
| 73 | + Path appPath = appFolder.getParentFile().toPath(); |
| 74 | + try (Stream<Path> fileStream = Files.walk(appPath)) { |
| 75 | + fileStream.forEach(path -> { |
| 76 | + if (path.equals(tarFile.toPath())) { |
| 77 | + return; |
| 78 | + } |
| 79 | + try { |
| 80 | + String relativePath = appPath.relativize(path).toString(); |
| 81 | + if (path.toFile().isFile()) { |
| 82 | + if (!(relativePath.equals(executable.getName()) |
| 83 | + || relativePath.startsWith(jreDirectoryName + "/bin/") |
| 84 | + || relativePath.startsWith("scripts/"))) { |
| 85 | + TarArchiveEntry entry = new TarArchiveEntry(path.toFile(), relativePath); |
| 86 | + if (relativePath.equals(executable.getName()) |
| 87 | + || relativePath.startsWith(jreDirectoryName + "/bin/") |
| 88 | + || relativePath.startsWith("scripts/")) { |
| 89 | + entry.setMode(0755); |
| 90 | + } |
| 91 | + tarOut.putArchiveEntry(entry); |
| 92 | + Files.copy(path, tarOut); |
| 93 | + tarOut.closeArchiveEntry(); |
| 94 | + } |
| 95 | + } |
| 96 | + } catch (IOException e) { |
| 97 | + throw new UncheckedIOException(e); |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | + } else if (Platform.mac.equals(platform)) { |
| 102 | + MacPackager macPackager = (MacPackager) packager; |
| 103 | + File appFile = macPackager.getAppFile(); |
| 104 | + |
| 105 | + Path appPath = appFolder.toPath(); |
| 106 | + try (Stream<Path> fileStream = Files.walk(appFolder.toPath())) { |
| 107 | + fileStream.forEach(path -> { |
| 108 | + if (path.equals(tarFile.toPath())) { |
| 109 | + return; |
| 110 | + } |
| 111 | + try { |
| 112 | + String relativePath = appPath.relativize(path).toString(); |
| 113 | + if (path.toFile().isFile()) { |
| 114 | + if (!(relativePath.startsWith(appFile.getName() + "/Contents/MacOS/" + executable.getName()) |
| 115 | + || relativePath.startsWith(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub") |
| 116 | + || relativePath.startsWith(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/") |
| 117 | + || relativePath.startsWith(appFile.getName() + "/Contents/Resources/scripts/"))) { |
| 118 | + TarArchiveEntry entry = new TarArchiveEntry(path.toFile(), relativePath); |
| 119 | + if (relativePath.equals(executable.getName()) |
| 120 | + || relativePath.startsWith(jreDirectoryName + "/bin/") |
| 121 | + || relativePath.startsWith("scripts/")) { |
| 122 | + entry.setMode(0755); |
| 123 | + } |
| 124 | + tarOut.putArchiveEntry(entry); |
| 125 | + Files.copy(path, tarOut); |
| 126 | + tarOut.closeArchiveEntry(); |
| 127 | + } |
| 128 | + } |
| 129 | + } catch (IOException e) { |
| 130 | + throw new UncheckedIOException(e); |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + } |
99 | 135 | }
|
100 |
| - |
101 |
| - tarTask.getActions().forEach(action -> action.execute(tarTask)); |
102 | 136 |
|
103 | 137 | return tarFile;
|
104 | 138 | }
|
105 |
| - |
106 |
| - private Tar createTarTask() { |
107 |
| - return Context.getGradleContext().getProject().getTasks().create("createTarball_" + UUID.randomUUID(), Tar.class); |
108 |
| - } |
109 | 139 |
|
110 | 140 | }
|
0 commit comments