From ad82d2aa84bbf10fcc0dd001da94532e70ddbe6a Mon Sep 17 00:00:00 2001 From: Marc Philipp Date: Sun, 25 Feb 2024 18:07:25 +0100 Subject: [PATCH] Use jar --date to set ZIP entry timestamps rather than repackaging --- .../junitbuild.java-repackage-jars.gradle.kts | 52 ------------------- .../{ExecJarAction.kt => UpdateJarAction.kt} | 13 ++++- .../junit-platform-commons.gradle.kts | 6 +-- .../junit-platform-console.gradle.kts | 6 +-- 4 files changed, 16 insertions(+), 61 deletions(-) delete mode 100644 gradle/plugins/common/src/main/kotlin/junitbuild.java-repackage-jars.gradle.kts rename gradle/plugins/common/src/main/kotlin/junitbuild/java/{ExecJarAction.kt => UpdateJarAction.kt} (59%) diff --git a/gradle/plugins/common/src/main/kotlin/junitbuild.java-repackage-jars.gradle.kts b/gradle/plugins/common/src/main/kotlin/junitbuild.java-repackage-jars.gradle.kts deleted file mode 100644 index a000989b032..00000000000 --- a/gradle/plugins/common/src/main/kotlin/junitbuild.java-repackage-jars.gradle.kts +++ /dev/null @@ -1,52 +0,0 @@ -import java.util.jar.JarEntry -import java.util.jar.JarFile -import java.util.jar.JarOutputStream -import org.gradle.api.internal.file.archive.ZipCopyAction -import java.nio.file.Files - -// This registers a `doLast` action to rewrite the timestamps of the project's output JAR -afterEvaluate { - val jarTask = (tasks.findByName("shadowJar") ?: tasks["jar"]) as Jar - - jarTask.doLast { - - val newFile = Files.createTempFile("rewrite-timestamp", null).toFile() - val originalOutput = jarTask.archiveFile.get().asFile - - newFile.outputStream().use { os -> - - val newJarStream = JarOutputStream(os) - val oldJar = JarFile(originalOutput) - - fun sortAlwaysFirst(name: String): Comparator = - Comparator { a, b -> - when { - a.name == name -> -1 - b.name == name -> 1 - else -> 0 - } - } - - oldJar.entries() - .toList() - .distinctBy { it.name } - .sortedWith(sortAlwaysFirst("META-INF/") - .then(sortAlwaysFirst("META-INF/MANIFEST.MF")) - .thenBy { it.name }) - .forEach { entry -> - val jarEntry = JarEntry(entry.name) - - // Use the same constant as the fixed timestamps in normal copy actions - jarEntry.time = ZipCopyAction.CONSTANT_TIME_FOR_ZIP_ENTRIES - - newJarStream.putNextEntry(jarEntry) - - oldJar.getInputStream(entry).copyTo(newJarStream) - } - - newJarStream.finish() - } - - newFile.renameTo(originalOutput) - } -} diff --git a/gradle/plugins/common/src/main/kotlin/junitbuild/java/ExecJarAction.kt b/gradle/plugins/common/src/main/kotlin/junitbuild/java/UpdateJarAction.kt similarity index 59% rename from gradle/plugins/common/src/main/kotlin/junitbuild/java/ExecJarAction.kt rename to gradle/plugins/common/src/main/kotlin/junitbuild/java/UpdateJarAction.kt index 8a3cf49ad70..dce28e03fe8 100644 --- a/gradle/plugins/common/src/main/kotlin/junitbuild/java/ExecJarAction.kt +++ b/gradle/plugins/common/src/main/kotlin/junitbuild/java/UpdateJarAction.kt @@ -2,23 +2,32 @@ package junitbuild.java import org.gradle.api.Action import org.gradle.api.Task +import org.gradle.api.internal.file.archive.ZipCopyAction import org.gradle.api.provider.ListProperty import org.gradle.api.provider.Property import org.gradle.jvm.toolchain.JavaLauncher import org.gradle.process.ExecOperations +import java.time.Instant import javax.inject.Inject -abstract class ExecJarAction @Inject constructor(private val operations: ExecOperations): Action { +abstract class UpdateJarAction @Inject constructor(private val operations: ExecOperations): Action { abstract val javaLauncher: Property abstract val args: ListProperty + init { + args.addAll( + "--update", + "--date=${Instant.ofEpochMilli(ZipCopyAction.CONSTANT_TIME_FOR_ZIP_ENTRIES)}", + ) + } + override fun execute(t: Task) { operations.exec { executable = javaLauncher.get() .metadata.installationPath.file("bin/jar").asFile.absolutePath - args = this@ExecJarAction.args.get() + args = this@UpdateJarAction.args.get() } } } diff --git a/junit-platform-commons/junit-platform-commons.gradle.kts b/junit-platform-commons/junit-platform-commons.gradle.kts index bb6a7c336ea..c0d1404a6f6 100644 --- a/junit-platform-commons/junit-platform-commons.gradle.kts +++ b/junit-platform-commons/junit-platform-commons.gradle.kts @@ -1,9 +1,8 @@ -import junitbuild.java.ExecJarAction +import junitbuild.java.UpdateJarAction plugins { id("junitbuild.java-library-conventions") id("junitbuild.java-multi-release-sources") - id("junitbuild.java-repackage-jars") `java-test-fixtures` } @@ -18,10 +17,9 @@ dependencies { tasks.jar { val release9ClassesDir = sourceSets.mainRelease9.get().output.classesDirs.singleFile inputs.dir(release9ClassesDir).withPathSensitivity(PathSensitivity.RELATIVE) - doLast(objects.newInstance(ExecJarAction::class).apply { + doLast(objects.newInstance(UpdateJarAction::class).apply { javaLauncher = javaToolchains.launcherFor(java.toolchain) args.addAll( - "--update", "--file", archiveFile.get().asFile.absolutePath, "--release", "9", "-C", release9ClassesDir.absolutePath, "." diff --git a/junit-platform-console/junit-platform-console.gradle.kts b/junit-platform-console/junit-platform-console.gradle.kts index 91acfd91b6d..be24d568d80 100644 --- a/junit-platform-console/junit-platform-console.gradle.kts +++ b/junit-platform-console/junit-platform-console.gradle.kts @@ -1,8 +1,9 @@ +import junitbuild.java.UpdateJarAction + plugins { id("junitbuild.java-library-conventions") id("junitbuild.shadow-conventions") id("junitbuild.java-multi-release-sources") - id("junitbuild.java-repackage-jars") } description = "JUnit Platform Console" @@ -40,10 +41,9 @@ tasks { into("META-INF") } from(sourceSets.mainRelease9.get().output.classesDirs) - doLast(objects.newInstance(junitbuild.java.ExecJarAction::class).apply { + doLast(objects.newInstance(UpdateJarAction::class).apply { javaLauncher = project.javaToolchains.launcherFor(java.toolchain) args.addAll( - "--update", "--file", archiveFile.get().asFile.absolutePath, "--main-class", "org.junit.platform.console.ConsoleLauncher", "--release", "17",