Skip to content

Commit e172abd

Browse files
committed
Don't use tasks for zip/tar creation
1 parent 7311b4d commit e172abd

File tree

2 files changed

+188
-127
lines changed

2 files changed

+188
-127
lines changed
Lines changed: 95 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package io.github.fvarrui.javapackager.gradle;
22

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;
57

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;
811

912
import io.github.fvarrui.javapackager.model.Platform;
1013
import io.github.fvarrui.javapackager.packagers.ArtifactGenerator;
11-
import io.github.fvarrui.javapackager.packagers.Context;
1214
import io.github.fvarrui.javapackager.packagers.MacPackager;
1315
import io.github.fvarrui.javapackager.packagers.Packager;
1416

@@ -42,69 +44,97 @@ protected File doApply(Packager packager) throws Exception {
4244
String format = ".tar.gz";
4345
File tarFile = new File(outputDirectory, finalName + format);
4446

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+
}
99135
}
100-
101-
tarTask.getActions().forEach(action -> action.execute(tarTask));
102136

103137
return tarFile;
104138
}
105-
106-
private Tar createTarTask() {
107-
return Context.getGradleContext().getProject().getTasks().create("createTarball_" + UUID.randomUUID(), Tar.class);
108-
}
109139

110140
}
Lines changed: 93 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package io.github.fvarrui.javapackager.gradle;
22

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;
57

6-
import org.gradle.api.tasks.bundling.Zip;
8+
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
9+
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
710

811
import io.github.fvarrui.javapackager.model.Platform;
912
import io.github.fvarrui.javapackager.packagers.ArtifactGenerator;
10-
import io.github.fvarrui.javapackager.packagers.Context;
1113
import io.github.fvarrui.javapackager.packagers.MacPackager;
1214
import io.github.fvarrui.javapackager.packagers.Packager;
1315

@@ -39,67 +41,96 @@ protected File doApply(Packager packager) throws Exception {
3941
String zipFileName = packager.getZipballName() != null ? packager.getZipballName() : name + "-" + version + "-" + platform + ".zip";
4042
File zipFile = new File(outputDirectory, zipFileName);
4143

42-
Zip zipTask = createZipTask();
43-
zipTask.setProperty("archiveFileName", zipFile.getName());
44-
zipTask.setProperty("destinationDirectory", outputDirectory);
45-
46-
// if zipball is for windows platform
47-
if (Platform.windows.equals(platform)) {
48-
49-
zipTask.from(appFolder.getParentFile(), copySpec -> {
50-
copySpec.include(appFolder.getName() + "/**");
51-
});
52-
53-
}
54-
55-
// if zipball is for linux platform
56-
else if (Platform.linux.equals(platform)) {
57-
58-
zipTask.from(appFolder.getParentFile(), copySpec -> {
59-
copySpec.include(appFolder.getName() + "/**");
60-
copySpec.exclude(appFolder.getName() + "/" + executable.getName());
61-
copySpec.exclude(appFolder.getName() + "/" + jreDirectoryName + "/bin/*");
62-
copySpec.exclude(appFolder.getName() + "/scripts/*");
63-
});
64-
zipTask.from(appFolder.getParentFile(), copySpec -> {
65-
copySpec.include(appFolder.getName() + "/" + executable.getName());
66-
copySpec.include(appFolder.getName() + "/" + jreDirectoryName + "/bin/*");
67-
copySpec.include(appFolder.getName() + "/scripts/*");
68-
copySpec.setFileMode(0755);
69-
});
70-
71-
}
72-
73-
// if zipball is for macos platform
74-
else if (Platform.mac.equals(platform)) {
75-
76-
MacPackager macPackager = (MacPackager) packager;
77-
File appFile = macPackager.getAppFile();
78-
79-
zipTask.from(appFolder, copySpec -> {
80-
copySpec.include(appFile.getName() + "/**");
81-
copySpec.exclude(appFile.getName() + "/Contents/MacOS/" + executable.getName());
82-
copySpec.exclude(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub");
83-
copySpec.exclude(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/*");
84-
copySpec.exclude(appFile.getName() + "/Contents/Resources/scripts/*");
85-
});
86-
zipTask.from(appFolder, copySpec -> {
87-
copySpec.include(appFile.getName() + "/Contents/MacOS/" + executable.getName());
88-
copySpec.include(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub");
89-
copySpec.include(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/*");
90-
copySpec.include(appFile.getName() + "/Contents/Resources/scripts/*");
91-
copySpec.setFileMode(0755);
92-
});
93-
44+
try (OutputStream fos = Files.newOutputStream(zipFile.toPath());
45+
BufferedOutputStream bos = new BufferedOutputStream(fos);
46+
ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(bos)) {
47+
48+
if (Platform.windows.equals(platform)) {
49+
Path basePath = appFolder.getParentFile().toPath();
50+
try (Stream<Path> fileStream = Files.walk(appFolder.toPath())) {
51+
fileStream.forEach(path -> {
52+
if (path.equals(zipFile.toPath())) {
53+
return;
54+
}
55+
File file = path.toFile();
56+
if (file.isFile()) {
57+
try {
58+
ZipArchiveEntry entry = new ZipArchiveEntry(path.toFile(), basePath.relativize(path).toString());
59+
zipOut.putArchiveEntry(entry);
60+
Files.copy(path, zipOut);
61+
zipOut.closeArchiveEntry();
62+
} catch (IOException e) {
63+
throw new UncheckedIOException(e);
64+
}
65+
}
66+
});
67+
}
68+
} else if (Platform.linux.equals(platform)) {
69+
Path appPath = appFolder.getParentFile().toPath();
70+
try (Stream<Path> fileStream = Files.walk(appPath)) {
71+
fileStream.forEach(path -> {
72+
if (path.equals(zipFile.toPath())) {
73+
return;
74+
}
75+
try {
76+
String relativePath = appPath.relativize(path).toString();
77+
if (path.toFile().isFile()) {
78+
if (!(relativePath.equals(executable.getName())
79+
|| relativePath.startsWith(jreDirectoryName + "/bin/")
80+
|| relativePath.startsWith("scripts/"))) {
81+
ZipArchiveEntry entry = new ZipArchiveEntry(path.toFile(), relativePath);
82+
if (relativePath.equals(executable.getName())
83+
|| relativePath.startsWith(jreDirectoryName + "/bin/")
84+
|| relativePath.startsWith("scripts/")) {
85+
entry.setUnixMode(0755);
86+
}
87+
zipOut.putArchiveEntry(entry);
88+
Files.copy(path, zipOut);
89+
zipOut.closeArchiveEntry();
90+
}
91+
}
92+
} catch (IOException e) {
93+
throw new UncheckedIOException(e);
94+
}
95+
});
96+
}
97+
} else if (Platform.mac.equals(platform)) {
98+
MacPackager macPackager = (MacPackager) packager;
99+
File appFile = macPackager.getAppFile();
100+
101+
Path appPath = appFolder.toPath();
102+
try (Stream<Path> fileStream = Files.walk(appFolder.toPath())) {
103+
fileStream.forEach(path -> {
104+
if (path.equals(zipFile.toPath())) {
105+
return;
106+
}
107+
try {
108+
String relativePath = appPath.relativize(path).toString();
109+
if (path.toFile().isFile()) {
110+
if (!(relativePath.startsWith(appFile.getName() + "/Contents/MacOS/" + executable.getName())
111+
|| relativePath.startsWith(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub")
112+
|| relativePath.startsWith(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/")
113+
|| relativePath.startsWith(appFile.getName() + "/Contents/Resources/scripts/"))) {
114+
ZipArchiveEntry entry = new ZipArchiveEntry(path.toFile(), relativePath);
115+
if (relativePath.equals(executable.getName())
116+
|| relativePath.startsWith(jreDirectoryName + "/bin/")
117+
|| relativePath.startsWith("scripts/")) {
118+
entry.setUnixMode(0755);
119+
}
120+
zipOut.putArchiveEntry(entry);
121+
Files.copy(path, zipOut);
122+
zipOut.closeArchiveEntry();
123+
}
124+
}
125+
} catch (IOException e) {
126+
throw new UncheckedIOException(e);
127+
}
128+
});
129+
}
130+
}
94131
}
95-
96-
zipTask.getActions().forEach(action -> action.execute(zipTask));
97132

98133
return zipFile;
99134
}
100-
101-
private Zip createZipTask() {
102-
return Context.getGradleContext().getProject().getTasks().create("createZipball_" + UUID.randomUUID(), Zip.class);
103-
}
104135

105136
}

0 commit comments

Comments
 (0)