Compare commits

...
Author SHA1 Message Date
BenCodez 6b723311d2 Recompress downloadable JAR without changing entry payloads 2026-11-18 03:04:03 +00:00
3 changed files with 94 additions and 0 deletions

No files matched your search

+34
View File
@@ -12,6 +12,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<build.number>NOTSET</build.number>
<packaged.jar.name>${project.name}</packaged.jar.name>
<!-- Shade and recompression must always start from a fresh project JAR. -->
<maven.jar.forceCreation>true</maven.jar.forceCreation>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.release>21</maven.compiler.release>
@@ -268,6 +270,37 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>recompress-downloadable-jar</id>
<phase>package</phase>
<goals><goal>run</goal></goals>
<configuration>
<target>
<!-- Retain the shaded input for the entry-by-entry package check. -->
<copy file="${project.build.directory}/${packaged.jar.name}.jar"
tofile="${project.build.directory}/jar-recompression-input.jar" overwrite="true" />
<delete file="${project.build.directory}/jar-recompression-staged.jar" />
<zip destfile="${project.build.directory}/jar-recompression-staged.jar"
level="9" keepcompression="false" encoding="UTF8"
duplicate="fail" defaultexcludes="false">
<!-- JarInputStream requires the manifest before other files. -->
<zipfileset src="${project.build.directory}/jar-recompression-input.jar"
includes="META-INF/MANIFEST.MF" defaultexcludes="false" />
<zipfileset src="${project.build.directory}/jar-recompression-input.jar"
excludes="META-INF/MANIFEST.MF" defaultexcludes="false" />
</zip>
<move file="${project.build.directory}/jar-recompression-staged.jar"
tofile="${project.build.directory}/${packaged.jar.name}.jar" overwrite="true" />
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
@@ -290,6 +323,7 @@
<reportsDirectory>${project.build.directory}/packaged-artifact-reports</reportsDirectory>
<systemPropertyVariables>
<votingplugin.packagedJar>${project.build.directory}/${packaged.jar.name}.jar</votingplugin.packagedJar>
<votingplugin.precompressionJar>${project.build.directory}/jar-recompression-input.jar</votingplugin.precompressionJar>
</systemPropertyVariables>
</configuration>
</execution>
@@ -17,6 +17,8 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.stream.Collectors;
import javax.tools.ToolProvider;
import org.junit.jupiter.api.Test;
@@ -26,6 +28,33 @@ import org.junit.jupiter.api.io.TempDir;
public class PackagedArtifactTest {
private static final long ARTIFACT_SIZE_TARGET_BYTES = 10L * 1024L * 1024L;
@Test
void recompressionPreservesEveryEntryAndStreamingManifest() throws Exception {
Path input = Path.of(System.getProperty("votingplugin.precompressionJar"));
Path output = packagedJar();
try (JarFile before = new JarFile(input.toFile()); JarFile after = new JarFile(output.toFile())) {
var beforeNames = before.stream().map(entry -> entry.getName()).collect(Collectors.toList());
var afterNames = after.stream().map(entry -> entry.getName()).collect(Collectors.toList());
assertEquals(beforeNames.size(), new java.util.HashSet<>(beforeNames).size(),
"The shaded input must not contain duplicate entry names");
assertEquals(afterNames.size(), new java.util.HashSet<>(afterNames).size(),
"Recompression must not create duplicate entry names");
assertEquals(new java.util.HashSet<>(beforeNames), new java.util.HashSet<>(afterNames),
"Recompression must retain every class, directory and resource");
for (String name : beforeNames) {
try (var original = before.getInputStream(before.getEntry(name));
var recompressed = after.getInputStream(after.getEntry(name))) {
assertArrayEquals(original.readAllBytes(), recompressed.readAllBytes(), name);
}
}
assertEquals(before.getComment(), after.getComment(), "The archive comment must be retained");
}
try (JarInputStream streaming = new JarInputStream(Files.newInputStream(output))) {
assertNotNull(streaming.getManifest(), "Streaming readers must see the manifest first");
}
System.out.printf("Lossless JAR recompression: %,d -> %,d bytes%n", Files.size(input), Files.size(output));
}
@Test
void containsOneRelocatedRuntimeWithoutUnusedHttpCrypto() throws Exception {
Path artifactPath = packagedJar();
@@ -58,6 +87,10 @@ public class PackagedArtifactTest {
"com/bencodez/votingplugin/simpleapi/scheduler/BukkitScheduler.class"));
assertNotNull(artifact.getEntry(
"com/bencodez/votingplugin/simpleapi/hikari/HikariDataSource.class"));
assertNotNull(artifact.getEntry("com/bencodez/simpleapi/configurate/ConfigurationNode.class"));
assertFalse(artifact.stream().anyMatch(entry -> entry.getName()
.startsWith("com/bencodez/votingplugin/simpleapi/configurate/")),
"A repeated package must not shade the previous output into a second Configurate namespace");
assertNull(artifact.getEntry("org/mozilla/javascript/Context.class"));
assertNull(artifact.getEntry("com/zaxxer/hikari/HikariDataSource.class"));
+27
View File
@@ -0,0 +1,27 @@
# Lossless downloadable JAR recompression
After shading, the package phase repacks the downloadable JAR with standard
DEFLATE level 9. Entry names and uncompressed content remain identical,
including services, licenses, manifests and native libraries. ZIP attributes
and entry timestamps may be normalized. A package-phase comparison checks the complete
input/output inventories and bytes, and verifies that streaming JAR readers
still see the manifest first. The artifact name and runtime dependencies stay
the same; the added Ant task is build-only.
The repacker follows Shade and precedes the packaged-artifact tests. It keeps
ZIP64 support automatic, uses UTF-8 entry names, rejects duplicate names, and
includes the existing manifest before the remaining resources. It does not
rewrite the manifest. Deployment profiles use the existing `packaged.jar.name`
property, so the normal artifact path is preserved.
All profiles force Maven to recreate the project JAR before Shade, even when
inputs are unchanged. This prevents a previous shaded/recompressed artifact
from being reused as the next Shade input. The package test also rejects a
second Configurate namespace created by shading an already shaded JAR.
The shaded comparison input remains in `target/` without being attached as a
publication artifact. The staged archive is moved to the normal downloadable
path after repacking. The package test compares every input/output
entry and its bytes, then opens the final JAR through `JarInputStream` to check
streaming manifest discovery. Compression savings are reported without adding
a build-failing size threshold. The existing advisory size warning still applies.