From e172abda03df10094bd2454b6c18d492e6e3d880 Mon Sep 17 00:00:00 2001 From: keastrid Date: Mon, 6 Jan 2025 00:29:54 -0500 Subject: [PATCH 1/4] Don't use tasks for zip/tar creation --- .../javapackager/gradle/CreateTarball.java | 160 +++++++++++------- .../javapackager/gradle/CreateZipball.java | 155 ++++++++++------- 2 files changed, 188 insertions(+), 127 deletions(-) diff --git a/src/main/java/io/github/fvarrui/javapackager/gradle/CreateTarball.java b/src/main/java/io/github/fvarrui/javapackager/gradle/CreateTarball.java index 917fac8d..69401c64 100644 --- a/src/main/java/io/github/fvarrui/javapackager/gradle/CreateTarball.java +++ b/src/main/java/io/github/fvarrui/javapackager/gradle/CreateTarball.java @@ -1,14 +1,16 @@ package io.github.fvarrui.javapackager.gradle; -import java.io.File; -import java.util.UUID; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.stream.Stream; -import org.gradle.api.tasks.bundling.Compression; -import org.gradle.api.tasks.bundling.Tar; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; +import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; import io.github.fvarrui.javapackager.model.Platform; import io.github.fvarrui.javapackager.packagers.ArtifactGenerator; -import io.github.fvarrui.javapackager.packagers.Context; import io.github.fvarrui.javapackager.packagers.MacPackager; import io.github.fvarrui.javapackager.packagers.Packager; @@ -42,69 +44,97 @@ protected File doApply(Packager packager) throws Exception { String format = ".tar.gz"; File tarFile = new File(outputDirectory, finalName + format); - Tar tarTask = createTarTask(); - tarTask.setProperty("archiveFileName", tarFile.getName()); - tarTask.setProperty("destinationDirectory", outputDirectory); - tarTask.setCompression(Compression.GZIP); - - // if zipball is for windows platform - if (Platform.windows.equals(platform)) { - - tarTask.from(appFolder.getParentFile(), copySpec -> { - copySpec.include(appFolder.getName() + "/**"); - }); - - } - - // if zipball is for linux platform - else if (Platform.linux.equals(platform)) { - - tarTask.from(appFolder.getParentFile(), copySpec -> { - copySpec.include(appFolder.getName() + "/**"); - copySpec.exclude(appFolder.getName() + "/" + executable.getName()); - copySpec.exclude(appFolder.getName() + "/" + jreDirectoryName + "/bin/*"); - copySpec.exclude(appFolder.getName() + "/scripts/*"); - }); - tarTask.from(appFolder.getParentFile(), copySpec -> { - copySpec.include(appFolder.getName() + "/" + executable.getName()); - copySpec.include(appFolder.getName() + "/" + jreDirectoryName + "/bin/*"); - copySpec.include(appFolder.getName() + "/scripts/*"); - copySpec.setFileMode(0755); - }); - - } - - // if zipball is for macos platform - else if (Platform.mac.equals(platform)) { - - MacPackager macPackager = (MacPackager) packager; - File appFile = macPackager.getAppFile(); - - tarTask.from(appFolder, copySpec -> { - copySpec.include(appFile.getName() + "/**"); - copySpec.exclude(appFile.getName() + "/Contents/MacOS/" + executable.getName()); - copySpec.exclude(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub"); - copySpec.exclude(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/*"); - copySpec.exclude(appFile.getName() + "/Contents/Resources/scripts/*"); - - }); - tarTask.from(appFolder, copySpec -> { - copySpec.include(appFile.getName() + "/Contents/MacOS/" + executable.getName()); - copySpec.include(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub"); - copySpec.include(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/*"); - copySpec.include(appFile.getName() + "/Contents/Resources/scripts/*"); - copySpec.setFileMode(0755); - }); - + try (OutputStream fos = Files.newOutputStream(tarFile.toPath()); + BufferedOutputStream bos = new BufferedOutputStream(fos); + GzipCompressorOutputStream gcos = new GzipCompressorOutputStream(bos); + TarArchiveOutputStream tarOut = new TarArchiveOutputStream(gcos)) { + + if (Platform.windows.equals(platform)) { + Path basePath = appFolder.getParentFile().toPath(); + try (Stream fileStream = Files.walk(appFolder.toPath())) { + fileStream.forEach(path -> { + if (path.equals(tarFile.toPath())) { + return; + } + File file = path.toFile(); + if (file.isFile()) { + try { + TarArchiveEntry entry = new TarArchiveEntry(path.toFile(), basePath.relativize(path).toString()); + tarOut.putArchiveEntry(entry); + Files.copy(path, tarOut); + tarOut.closeArchiveEntry(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + }); + } + } else if (Platform.linux.equals(platform)) { + Path appPath = appFolder.getParentFile().toPath(); + try (Stream fileStream = Files.walk(appPath)) { + fileStream.forEach(path -> { + if (path.equals(tarFile.toPath())) { + return; + } + try { + String relativePath = appPath.relativize(path).toString(); + if (path.toFile().isFile()) { + if (!(relativePath.equals(executable.getName()) + || relativePath.startsWith(jreDirectoryName + "/bin/") + || relativePath.startsWith("scripts/"))) { + TarArchiveEntry entry = new TarArchiveEntry(path.toFile(), relativePath); + if (relativePath.equals(executable.getName()) + || relativePath.startsWith(jreDirectoryName + "/bin/") + || relativePath.startsWith("scripts/")) { + entry.setMode(0755); + } + tarOut.putArchiveEntry(entry); + Files.copy(path, tarOut); + tarOut.closeArchiveEntry(); + } + } + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); + } + } else if (Platform.mac.equals(platform)) { + MacPackager macPackager = (MacPackager) packager; + File appFile = macPackager.getAppFile(); + + Path appPath = appFolder.toPath(); + try (Stream fileStream = Files.walk(appFolder.toPath())) { + fileStream.forEach(path -> { + if (path.equals(tarFile.toPath())) { + return; + } + try { + String relativePath = appPath.relativize(path).toString(); + if (path.toFile().isFile()) { + if (!(relativePath.startsWith(appFile.getName() + "/Contents/MacOS/" + executable.getName()) + || relativePath.startsWith(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub") + || relativePath.startsWith(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/") + || relativePath.startsWith(appFile.getName() + "/Contents/Resources/scripts/"))) { + TarArchiveEntry entry = new TarArchiveEntry(path.toFile(), relativePath); + if (relativePath.equals(executable.getName()) + || relativePath.startsWith(jreDirectoryName + "/bin/") + || relativePath.startsWith("scripts/")) { + entry.setMode(0755); + } + tarOut.putArchiveEntry(entry); + Files.copy(path, tarOut); + tarOut.closeArchiveEntry(); + } + } + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); + } + } } - - tarTask.getActions().forEach(action -> action.execute(tarTask)); return tarFile; } - - private Tar createTarTask() { - return Context.getGradleContext().getProject().getTasks().create("createTarball_" + UUID.randomUUID(), Tar.class); - } } diff --git a/src/main/java/io/github/fvarrui/javapackager/gradle/CreateZipball.java b/src/main/java/io/github/fvarrui/javapackager/gradle/CreateZipball.java index cdd85c46..fb72e16b 100644 --- a/src/main/java/io/github/fvarrui/javapackager/gradle/CreateZipball.java +++ b/src/main/java/io/github/fvarrui/javapackager/gradle/CreateZipball.java @@ -1,13 +1,15 @@ package io.github.fvarrui.javapackager.gradle; -import java.io.File; -import java.util.UUID; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.stream.Stream; -import org.gradle.api.tasks.bundling.Zip; +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import io.github.fvarrui.javapackager.model.Platform; import io.github.fvarrui.javapackager.packagers.ArtifactGenerator; -import io.github.fvarrui.javapackager.packagers.Context; import io.github.fvarrui.javapackager.packagers.MacPackager; import io.github.fvarrui.javapackager.packagers.Packager; @@ -39,67 +41,96 @@ protected File doApply(Packager packager) throws Exception { String zipFileName = packager.getZipballName() != null ? packager.getZipballName() : name + "-" + version + "-" + platform + ".zip"; File zipFile = new File(outputDirectory, zipFileName); - Zip zipTask = createZipTask(); - zipTask.setProperty("archiveFileName", zipFile.getName()); - zipTask.setProperty("destinationDirectory", outputDirectory); - - // if zipball is for windows platform - if (Platform.windows.equals(platform)) { - - zipTask.from(appFolder.getParentFile(), copySpec -> { - copySpec.include(appFolder.getName() + "/**"); - }); - - } - - // if zipball is for linux platform - else if (Platform.linux.equals(platform)) { - - zipTask.from(appFolder.getParentFile(), copySpec -> { - copySpec.include(appFolder.getName() + "/**"); - copySpec.exclude(appFolder.getName() + "/" + executable.getName()); - copySpec.exclude(appFolder.getName() + "/" + jreDirectoryName + "/bin/*"); - copySpec.exclude(appFolder.getName() + "/scripts/*"); - }); - zipTask.from(appFolder.getParentFile(), copySpec -> { - copySpec.include(appFolder.getName() + "/" + executable.getName()); - copySpec.include(appFolder.getName() + "/" + jreDirectoryName + "/bin/*"); - copySpec.include(appFolder.getName() + "/scripts/*"); - copySpec.setFileMode(0755); - }); - - } - - // if zipball is for macos platform - else if (Platform.mac.equals(platform)) { - - MacPackager macPackager = (MacPackager) packager; - File appFile = macPackager.getAppFile(); - - zipTask.from(appFolder, copySpec -> { - copySpec.include(appFile.getName() + "/**"); - copySpec.exclude(appFile.getName() + "/Contents/MacOS/" + executable.getName()); - copySpec.exclude(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub"); - copySpec.exclude(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/*"); - copySpec.exclude(appFile.getName() + "/Contents/Resources/scripts/*"); - }); - zipTask.from(appFolder, copySpec -> { - copySpec.include(appFile.getName() + "/Contents/MacOS/" + executable.getName()); - copySpec.include(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub"); - copySpec.include(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/*"); - copySpec.include(appFile.getName() + "/Contents/Resources/scripts/*"); - copySpec.setFileMode(0755); - }); - + try (OutputStream fos = Files.newOutputStream(zipFile.toPath()); + BufferedOutputStream bos = new BufferedOutputStream(fos); + ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(bos)) { + + if (Platform.windows.equals(platform)) { + Path basePath = appFolder.getParentFile().toPath(); + try (Stream fileStream = Files.walk(appFolder.toPath())) { + fileStream.forEach(path -> { + if (path.equals(zipFile.toPath())) { + return; + } + File file = path.toFile(); + if (file.isFile()) { + try { + ZipArchiveEntry entry = new ZipArchiveEntry(path.toFile(), basePath.relativize(path).toString()); + zipOut.putArchiveEntry(entry); + Files.copy(path, zipOut); + zipOut.closeArchiveEntry(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + }); + } + } else if (Platform.linux.equals(platform)) { + Path appPath = appFolder.getParentFile().toPath(); + try (Stream fileStream = Files.walk(appPath)) { + fileStream.forEach(path -> { + if (path.equals(zipFile.toPath())) { + return; + } + try { + String relativePath = appPath.relativize(path).toString(); + if (path.toFile().isFile()) { + if (!(relativePath.equals(executable.getName()) + || relativePath.startsWith(jreDirectoryName + "/bin/") + || relativePath.startsWith("scripts/"))) { + ZipArchiveEntry entry = new ZipArchiveEntry(path.toFile(), relativePath); + if (relativePath.equals(executable.getName()) + || relativePath.startsWith(jreDirectoryName + "/bin/") + || relativePath.startsWith("scripts/")) { + entry.setUnixMode(0755); + } + zipOut.putArchiveEntry(entry); + Files.copy(path, zipOut); + zipOut.closeArchiveEntry(); + } + } + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); + } + } else if (Platform.mac.equals(platform)) { + MacPackager macPackager = (MacPackager) packager; + File appFile = macPackager.getAppFile(); + + Path appPath = appFolder.toPath(); + try (Stream fileStream = Files.walk(appFolder.toPath())) { + fileStream.forEach(path -> { + if (path.equals(zipFile.toPath())) { + return; + } + try { + String relativePath = appPath.relativize(path).toString(); + if (path.toFile().isFile()) { + if (!(relativePath.startsWith(appFile.getName() + "/Contents/MacOS/" + executable.getName()) + || relativePath.startsWith(appFile.getName() + "/Contents/MacOS/universalJavaApplicationStub") + || relativePath.startsWith(appFile.getName() + "/Contents/PlugIns/" + jreDirectoryName + "/Contents/Home/bin/") + || relativePath.startsWith(appFile.getName() + "/Contents/Resources/scripts/"))) { + ZipArchiveEntry entry = new ZipArchiveEntry(path.toFile(), relativePath); + if (relativePath.equals(executable.getName()) + || relativePath.startsWith(jreDirectoryName + "/bin/") + || relativePath.startsWith("scripts/")) { + entry.setUnixMode(0755); + } + zipOut.putArchiveEntry(entry); + Files.copy(path, zipOut); + zipOut.closeArchiveEntry(); + } + } + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); + } + } } - - zipTask.getActions().forEach(action -> action.execute(zipTask)); return zipFile; } - - private Zip createZipTask() { - return Context.getGradleContext().getProject().getTasks().create("createZipball_" + UUID.randomUUID(), Zip.class); - } } From 548940cf131b8a48bfc1ef6d0e8c0c0a06e0051a Mon Sep 17 00:00:00 2001 From: keastrid Date: Mon, 6 Jan 2025 00:31:12 -0500 Subject: [PATCH 2/4] Save extension object --- .../github/fvarrui/javapackager/gradle/GradleContext.java | 8 ++++++++ .../github/fvarrui/javapackager/gradle/PackagePlugin.java | 3 ++- .../github/fvarrui/javapackager/gradle/PackageTask.java | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/fvarrui/javapackager/gradle/GradleContext.java b/src/main/java/io/github/fvarrui/javapackager/gradle/GradleContext.java index b2e1f206..18865953 100644 --- a/src/main/java/io/github/fvarrui/javapackager/gradle/GradleContext.java +++ b/src/main/java/io/github/fvarrui/javapackager/gradle/GradleContext.java @@ -23,6 +23,7 @@ public class GradleContext extends Context { private Project project; private Launch4jLibraryTask libraryTask; private DuplicatesStrategy duplicatesStrategy; + private PackagePluginExtension packagePluginExtension; public GradleContext(Project project) { super(); @@ -132,4 +133,11 @@ public File createWindowsExe(WindowsPackager packager) throws Exception { return null; } + public PackagePluginExtension getPackagePluginExtension() { + return packagePluginExtension; + } + + public void setPackagePluginExtension(PackagePluginExtension packagePluginExtension) { + this.packagePluginExtension = packagePluginExtension; + } } diff --git a/src/main/java/io/github/fvarrui/javapackager/gradle/PackagePlugin.java b/src/main/java/io/github/fvarrui/javapackager/gradle/PackagePlugin.java index b817168b..f31d435e 100644 --- a/src/main/java/io/github/fvarrui/javapackager/gradle/PackagePlugin.java +++ b/src/main/java/io/github/fvarrui/javapackager/gradle/PackagePlugin.java @@ -24,11 +24,12 @@ public void apply(Project project) { project.getPluginManager().apply("java"); project.getPluginManager().apply("edu.sc.seis.launch4j"); - project.getExtensions().create(SETTINGS_EXT_NAME, PackagePluginExtension.class, project); + PackagePluginExtension extension = project.getExtensions().create(SETTINGS_EXT_NAME, PackagePluginExtension.class, project); project.getTasks().create(PACKAGE_TASK_NAME, PackageTask.class).dependsOn("build"); Context.getGradleContext().setLibraryTask(project.getTasks().create("launch4j_" + UUID.randomUUID(), Launch4jLibraryTask.class)); + Context.getGradleContext().setPackagePluginExtension(extension); } } diff --git a/src/main/java/io/github/fvarrui/javapackager/gradle/PackageTask.java b/src/main/java/io/github/fvarrui/javapackager/gradle/PackageTask.java index ce6a321a..269b8730 100644 --- a/src/main/java/io/github/fvarrui/javapackager/gradle/PackageTask.java +++ b/src/main/java/io/github/fvarrui/javapackager/gradle/PackageTask.java @@ -607,7 +607,7 @@ public void setDuplicatesStrategy(DuplicatesStrategy duplicatesStrategy) { @Override protected Packager createPackager() throws Exception { - PackagePluginExtension extension = getProject().getExtensions().findByType(PackagePluginExtension.class); + PackagePluginExtension extension = Context.getGradleContext().getPackagePluginExtension(); Context.getGradleContext().setDuplicatesStrategy(defaultIfNull(duplicatesStrategy, extension.getDuplicatesStrategy())); From d107132d5fb9df06ee967cf3c876293ac6825a80 Mon Sep 17 00:00:00 2001 From: keastrid Date: Mon, 6 Jan 2025 00:36:05 -0500 Subject: [PATCH 3/4] Store project version for use as a default --- .../fvarrui/javapackager/gradle/GradleContext.java | 10 ++++++++++ .../fvarrui/javapackager/gradle/PackageTask.java | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/github/fvarrui/javapackager/gradle/GradleContext.java b/src/main/java/io/github/fvarrui/javapackager/gradle/GradleContext.java index 18865953..e9f70238 100644 --- a/src/main/java/io/github/fvarrui/javapackager/gradle/GradleContext.java +++ b/src/main/java/io/github/fvarrui/javapackager/gradle/GradleContext.java @@ -24,10 +24,12 @@ public class GradleContext extends Context { private Launch4jLibraryTask libraryTask; private DuplicatesStrategy duplicatesStrategy; private PackagePluginExtension packagePluginExtension; + private String defaultVersion; public GradleContext(Project project) { super(); this.project = project; + defaultVersion = project.getVersion().toString(); } public Logger getLogger() { @@ -140,4 +142,12 @@ public PackagePluginExtension getPackagePluginExtension() { public void setPackagePluginExtension(PackagePluginExtension packagePluginExtension) { this.packagePluginExtension = packagePluginExtension; } + + public String getDefaultVersion() { + return defaultVersion; + } + + public void setDefaultVersion(String defaultVersion) { + this.defaultVersion = defaultVersion; + } } diff --git a/src/main/java/io/github/fvarrui/javapackager/gradle/PackageTask.java b/src/main/java/io/github/fvarrui/javapackager/gradle/PackageTask.java index 269b8730..1573fd09 100644 --- a/src/main/java/io/github/fvarrui/javapackager/gradle/PackageTask.java +++ b/src/main/java/io/github/fvarrui/javapackager/gradle/PackageTask.java @@ -654,7 +654,7 @@ protected Packager createPackager() throws Exception { .templates(defaultIfNull(templates, extension.getTemplates())) .useResourcesAsWorkingDir(defaultIfNull(useResourcesAsWorkingDir, extension.isUseResourcesAsWorkingDir())) .url(defaultIfNull(url, extension.getUrl())) - .version(defaultIfNull(version, extension.getVersion(), getProject().getVersion().toString())) + .version(defaultIfNull(version, extension.getVersion(), Context.getGradleContext().getDefaultVersion())) .vmArgs(defaultIfNull(vmArgs, extension.getVmArgs())) .winConfig(defaultIfNull(winConfig, extension.getWinConfig())); From 6ef0a7cb7cb7bf6dd518f77d5565908b9e7ff881 Mon Sep 17 00:00:00 2001 From: keastrid Date: Mon, 6 Jan 2025 13:44:58 -0500 Subject: [PATCH 4/4] Add some rudimentary support for a few gradle types --- .../gradle/GradleLocalContext.java | 75 +++++++++++++++++++ .../javapackager/gradle/PackagePlugin.java | 17 +++++ .../gradle/PackagePluginExtension.java | 9 +++ .../javapackager/gradle/PackageTask.java | 48 ++++++++++-- .../javapackager/packagers/LocalContext.java | 4 + .../javapackager/packagers/Packager.java | 39 ++++++++-- .../packagers/PackagerSettings.java | 40 +++++++++- 7 files changed, 221 insertions(+), 11 deletions(-) create mode 100644 src/main/java/io/github/fvarrui/javapackager/gradle/GradleLocalContext.java create mode 100644 src/main/java/io/github/fvarrui/javapackager/packagers/LocalContext.java diff --git a/src/main/java/io/github/fvarrui/javapackager/gradle/GradleLocalContext.java b/src/main/java/io/github/fvarrui/javapackager/gradle/GradleLocalContext.java new file mode 100644 index 00000000..86ca56a7 --- /dev/null +++ b/src/main/java/io/github/fvarrui/javapackager/gradle/GradleLocalContext.java @@ -0,0 +1,75 @@ +package io.github.fvarrui.javapackager.gradle; + +import io.github.fvarrui.javapackager.packagers.LocalContext; +import io.github.fvarrui.javapackager.utils.Logger; +import org.gradle.api.Task; +import org.gradle.api.file.*; + +import java.io.File; + +public class GradleLocalContext implements LocalContext { + final FileSystemOperations fileSystemOperations; + + public GradleLocalContext(FileSystemOperations fileSystemOperations) { + this.fileSystemOperations = fileSystemOperations; + } + + public FileSystemOperations getFileSystemOperations() { + return fileSystemOperations; + } + + public void copyAdditionalResources(Object o, File destination) throws Exception { + if (o instanceof CopySpec) { + getFileSystemOperations().copy(cs -> { + cs.with((CopySpec) o); + cs.into(destination); + cs.eachFile(fcd -> { + if (fcd.isDirectory()) { + Logger.info("Copying folder [" + fcd.getPath() + "] to folder [" + destination + "]"); + } else { + Logger.info("Copying file [" + fcd.getPath() + "] to folder [" + destination + "]"); + } + }); + }); + } else if (o instanceof FileCollection) { + getFileSystemOperations().copy(cs -> { + cs.from((FileCollection) o); + cs.into(destination); + cs.eachFile(fcd -> { + if (fcd.isDirectory()) { + Logger.info("Copying folder [" + fcd.getPath() + "] to folder [" + destination + "]"); + } else { + Logger.info("Copying file [" + fcd.getPath() + "] to folder [" + destination + "]"); + } + }); + }); + } else if (o instanceof Task) { + getFileSystemOperations().copy(cs -> { + cs.from((Task) o); + cs.into(destination); + cs.eachFile(fcd -> { + if (fcd.isDirectory()) { + Logger.info("Copying folder [" + fcd.getPath() + "] to folder [" + destination + "]"); + } else { + Logger.info("Copying file [" + fcd.getPath() + "] to folder [" + destination + "]"); + } + }); + }); + } else { + throw new IllegalArgumentException("Unknown resource type: " + o); + } + } + + public File getSingleFile(Object o) { + if (o instanceof FileCollection) { + return ((FileCollection) o).getSingleFile(); + } else if (o instanceof Task) { + return ((Task) o).getOutputs().getFiles().getSingleFile(); + } /*else if (o instanceof Configuration) { + Configuration c = ((Configuration) o); + c.resolve() + }*/ + + return null; + } +} diff --git a/src/main/java/io/github/fvarrui/javapackager/gradle/PackagePlugin.java b/src/main/java/io/github/fvarrui/javapackager/gradle/PackagePlugin.java index f31d435e..97b159ec 100644 --- a/src/main/java/io/github/fvarrui/javapackager/gradle/PackagePlugin.java +++ b/src/main/java/io/github/fvarrui/javapackager/gradle/PackagePlugin.java @@ -30,6 +30,23 @@ public void apply(Project project) { Context.getGradleContext().setLibraryTask(project.getTasks().create("launch4j_" + UUID.randomUUID(), Launch4jLibraryTask.class)); Context.getGradleContext().setPackagePluginExtension(extension); + + // Pass along custom gradle types set in the extension to the task in this manor so that gradle's automatic + // task dependency magic can occur, eg. you can specify a task or configuration as an input and gradle will + // run it for us + project.afterEvaluate(p -> { + project.getTasks().withType(PackageTask.class, packageTask -> { + if (packageTask.getAdditionalResourceCollection() == null) { + packageTask.setAdditionalResourceCollection(extension.getAdditionalResourceCollection()); + } + + if (packageTask.getRunnableJar() == null && + packageTask.getRunnableJarSource() == null && extension.getRunnableJar() == null) { + packageTask.setRunnableJar(extension.getRunnableJarSource()); + } + }); + }); + } } diff --git a/src/main/java/io/github/fvarrui/javapackager/gradle/PackagePluginExtension.java b/src/main/java/io/github/fvarrui/javapackager/gradle/PackagePluginExtension.java index da793647..e072da94 100644 --- a/src/main/java/io/github/fvarrui/javapackager/gradle/PackagePluginExtension.java +++ b/src/main/java/io/github/fvarrui/javapackager/gradle/PackagePluginExtension.java @@ -16,6 +16,7 @@ import io.github.fvarrui.javapackager.model.Scripts; import io.github.fvarrui.javapackager.model.WindowsConfig; import io.github.fvarrui.javapackager.packagers.PackagerSettings; +import org.gradle.api.tasks.TaskProvider; /** * JavaPackager plugin extension for Gradle @@ -32,6 +33,7 @@ public PackagePluginExtension(Project project) { this.additionalModules = new ArrayList<>(); this.additionalModulePaths = new ArrayList<>(); this.additionalResources = new ArrayList<>(); + this.additionalResourceCollection = null; this.administratorRequired = false; this.assetsDir = new File(project.getProjectDir(), "assets"); this.bundleJre = true; @@ -97,4 +99,11 @@ public DuplicatesStrategy getDuplicatesStrategy() { return duplicatesStrategy; } + @Override + public PackagerSettings additionalResourceCollection(Object additionalResources) { + if (additionalResources instanceof TaskProvider) { + additionalResources = ((TaskProvider) additionalResources).get(); + } + return super.additionalResourceCollection(additionalResources); + } } \ No newline at end of file diff --git a/src/main/java/io/github/fvarrui/javapackager/gradle/PackageTask.java b/src/main/java/io/github/fvarrui/javapackager/gradle/PackageTask.java index 1573fd09..6350860c 100644 --- a/src/main/java/io/github/fvarrui/javapackager/gradle/PackageTask.java +++ b/src/main/java/io/github/fvarrui/javapackager/gradle/PackageTask.java @@ -7,12 +7,10 @@ import java.util.List; import java.util.Map; +import org.gradle.api.Task; import org.gradle.api.file.DuplicatesStrategy; -import org.gradle.api.tasks.Input; -import org.gradle.api.tasks.InputDirectory; -import org.gradle.api.tasks.InputFile; -import org.gradle.api.tasks.Optional; -import org.gradle.api.tasks.OutputDirectory; +import org.gradle.api.file.FileSystemOperations; +import org.gradle.api.tasks.*; import groovy.lang.Closure; import io.github.fvarrui.javapackager.model.Arch; @@ -28,6 +26,8 @@ import io.github.fvarrui.javapackager.packagers.Packager; import io.github.fvarrui.javapackager.packagers.PackagerFactory; +import javax.inject.Inject; + /** * Packaging task fro Gradle */ @@ -72,6 +72,23 @@ public List getAdditionalResources() { public void setAdditionalResources(List additionalResources) { this.additionalResources = additionalResources; } + + @InputFiles + @Optional + private Object additionalResourceCollection; + + public Object getAdditionalResourceCollection() { + return additionalResourceCollection; + } + + public void setAdditionalResourceCollection(Object additionalResources) { + if (additionalResources instanceof Task) { + additionalResources = ((Task) additionalResources).getOutputs().getFiles(); + } else if (additionalResources instanceof TaskProvider) { + additionalResources = ((TaskProvider) additionalResources).get().getOutputs().getFiles(); + } + this.additionalResourceCollection = additionalResources; + } @Input @Optional @@ -408,6 +425,18 @@ public File getRunnableJar() { public void setRunnableJar(File runnableJar) { this.runnableJar = runnableJar; } + + @Optional + @InputFiles + private Object runnableJarSource; + + public Object getRunnableJarSource() { + return runnableJarSource; + } + + public void setRunnableJar(Object runnableJar) { + this.runnableJarSource = runnableJar; + } @Input @Optional @@ -599,6 +628,12 @@ public void setDuplicatesStrategy(DuplicatesStrategy duplicatesStrategy) { this.duplicatesStrategy = duplicatesStrategy; } + @Inject + protected FileSystemOperations getFileSystemOperator() { + // Method body is ignored + throw new UnsupportedOperationException(); + } + // =============== // create packager // =============== @@ -614,9 +649,11 @@ protected Packager createPackager() throws Exception { return (Packager) PackagerFactory .createPackager(defaultIfNull(platform, extension.getPlatform())) + .setLocalContext(new GradleLocalContext(getFileSystemOperator())) .additionalModules(defaultIfNull(additionalModules, extension.getAdditionalModules())) .additionalModulePaths(defaultIfNull(additionalModulePaths, extension.getAdditionalModulePaths())) .additionalResources(defaultIfNull(additionalResources, extension.getAdditionalResources())) + .additionalResourceCollection(defaultIfNull(additionalResourceCollection, extension.getAdditionalResourceCollection())) .administratorRequired(defaultIfNull(administratorRequired, extension.getAdministratorRequired())) .arch(defaultIfNull(arch, extension.getArch())) .assetsDir(defaultIfNull(assetsDir, extension.getAssetsDir())) @@ -650,6 +687,7 @@ protected Packager createPackager() throws Exception { .outputDirectory(defaultIfNull(outputDirectory, extension.getOutputDirectory())) .packagingJdk(defaultIfNull(packagingJdk, extension.getPackagingJdk(), Context.getGradleContext().getDefaultToolchain())) .runnableJar(defaultIfNull(runnableJar, extension.getRunnableJar())) + .runnableJar(defaultIfNull(runnableJarSource, extension.getRunnableJarSource())) .scripts(defaultIfNull(scripts, extension.getScripts())) .templates(defaultIfNull(templates, extension.getTemplates())) .useResourcesAsWorkingDir(defaultIfNull(useResourcesAsWorkingDir, extension.isUseResourcesAsWorkingDir())) diff --git a/src/main/java/io/github/fvarrui/javapackager/packagers/LocalContext.java b/src/main/java/io/github/fvarrui/javapackager/packagers/LocalContext.java new file mode 100644 index 00000000..29e27378 --- /dev/null +++ b/src/main/java/io/github/fvarrui/javapackager/packagers/LocalContext.java @@ -0,0 +1,4 @@ +package io.github.fvarrui.javapackager.packagers; + +public interface LocalContext { +} diff --git a/src/main/java/io/github/fvarrui/javapackager/packagers/Packager.java b/src/main/java/io/github/fvarrui/javapackager/packagers/Packager.java index e93493d4..d0a0be23 100644 --- a/src/main/java/io/github/fvarrui/javapackager/packagers/Packager.java +++ b/src/main/java/io/github/fvarrui/javapackager/packagers/Packager.java @@ -10,6 +10,7 @@ import java.util.ArrayList; import java.util.List; +import io.github.fvarrui.javapackager.gradle.GradleLocalContext; import io.github.fvarrui.javapackager.model.Platform; import io.github.fvarrui.javapackager.utils.FileUtils; import io.github.fvarrui.javapackager.utils.IconUtils; @@ -44,6 +45,9 @@ public abstract class Packager extends PackagerSettings { // processed classpaths list protected List classpaths = new ArrayList<>(); + // Extra local context for packager + protected LocalContext localContext; + // =============================================== public File getAppFolder() { @@ -86,6 +90,15 @@ public File getBootstrapFile() { return bootstrapFile; } + public Packager setLocalContext(LocalContext localContext) { + this.localContext = localContext; + return this; + } + + public LocalContext getLocalContext() { + return localContext; + } + // =============================================== public Packager() { @@ -202,7 +215,7 @@ public void resolveResources() throws Exception { * here * @throws Exception */ - protected void copyAdditionalResources(List resources, File destination) throws Exception { + protected void copyAdditionalResources(List resources, Object o, File destination) throws Exception { Logger.infoIndent("Copying additional resources"); @@ -216,6 +229,12 @@ protected void copyAdditionalResources(List resources, File destination) t } } + if (Context.isGradle() && o != null) { + if (getLocalContext() instanceof GradleLocalContext) { + ((GradleLocalContext) getLocalContext()).copyAdditionalResources(o, destination); + } + } + // copy bootstrap script if (FileUtils.exists(getScripts().getBootstrap())) { String scriptExtension = getExtension(getScripts().getBootstrap().getName()); @@ -387,7 +406,7 @@ public File createApp() throws Exception { resolveResources(); // copies additional resources - copyAdditionalResources(additionalResources, resourcesDestinationFolder); + copyAdditionalResources(additionalResources, additionalResourceCollection, resourcesDestinationFolder); // copies all dependencies to Java folder Logger.infoIndent("Copying all dependencies ..."); @@ -396,9 +415,19 @@ public File createApp() throws Exception { // creates a runnable jar file if (runnableJar == null) { - Logger.infoIndent("Creating runnable JAR..."); - jarFile = Context.getContext().createRunnableJar(this); - Logger.infoUnindent("Runnable jar created in " + jarFile + "!"); + if (Context.isGradle() && getRunnableJarSource() != null) { + jarFile = ((GradleLocalContext) localContext).getSingleFile(getRunnableJarSource()); + + if (jarFile != null && jarFile.exists()) { + Logger.info("Using runnable JAR: " + jarFile); + } else { + throw new Exception("Runnable JAR doesn't exist: " + getRunnableJarSource()); + } + } else { + Logger.infoIndent("Creating runnable JAR..."); + jarFile = Context.getContext().createRunnableJar(this); + Logger.infoUnindent("Runnable jar created in " + jarFile + "!"); + } } else if (runnableJar.exists()) { Logger.info("Using runnable JAR: " + runnableJar); jarFile = runnableJar; diff --git a/src/main/java/io/github/fvarrui/javapackager/packagers/PackagerSettings.java b/src/main/java/io/github/fvarrui/javapackager/packagers/PackagerSettings.java index 4d560031..73af21b4 100644 --- a/src/main/java/io/github/fvarrui/javapackager/packagers/PackagerSettings.java +++ b/src/main/java/io/github/fvarrui/javapackager/packagers/PackagerSettings.java @@ -43,6 +43,7 @@ public class PackagerSettings { protected File jrePath; protected File jdkPath; protected List additionalResources; + protected Object additionalResourceCollection; protected List modules; protected List additionalModules; protected Platform platform; @@ -70,7 +71,8 @@ public class PackagerSettings { protected Scripts scripts; protected Arch arch; protected List