From b762351654d8f010652e244dcf8d6143382ca98c Mon Sep 17 00:00:00 2001 From: Georg Hackenberg Date: Thu, 21 Oct 2021 17:41:03 +0200 Subject: [PATCH] check module descriptors hidden inside META-INF/versions folder --- pom.xml | 2 +- .../maven/plugins/jigsaw/PatchMojo.java | 46 +++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 7af4e3f..a15f505 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 io.github.ghackenberg jigsaw-maven-plugin - 1.0.2 + 1.1.0 maven-plugin Maven Java Platform Module System (JPMS) Plugin Plugin to simplify software linking and packaging on top of the Java Platform Module system (JPMS). diff --git a/src/main/java/io/github/ghackenberg/maven/plugins/jigsaw/PatchMojo.java b/src/main/java/io/github/ghackenberg/maven/plugins/jigsaw/PatchMojo.java index 820883f..8df5cdc 100644 --- a/src/main/java/io/github/ghackenberg/maven/plugins/jigsaw/PatchMojo.java +++ b/src/main/java/io/github/ghackenberg/maven/plugins/jigsaw/PatchMojo.java @@ -36,6 +36,8 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -55,6 +57,12 @@ @Mojo(name = "patch", defaultPhase = LifecyclePhase.PACKAGE) public class PatchMojo extends BaseMojo { + private static final Pattern PATTERN = Pattern.compile("^META-INF/versions/([0-9]*)/module-info.class$"); + + private static final String JAVA_VERSION = System.getProperty("java.version"); + + private static final String JAVA_VERSION_MAJOR = JAVA_VERSION.substring(0, JAVA_VERSION.indexOf('.')); + @Parameter private boolean ignoreMissingDeps; @@ -64,6 +72,10 @@ public class PatchMojo extends BaseMojo { @Override protected final void run() throws MojoExecutionException, MojoFailureException { + String targetJavaVersion = multiRelease != null ? multiRelease : JAVA_VERSION_MAJOR; + + int targetJavaVersionNumber = Integer.parseInt(targetJavaVersion); + File[] jars = modulePath.listFiles(file -> !file.isDirectory() && file.getName().endsWith(".jar")); for (File jar : jars) { @@ -73,10 +85,38 @@ protected final void run() throws MojoExecutionException, MojoFailureException { System.out.println("[" + jar.getName() + "] Checking module info"); try (ZipFile zip = new ZipFile(jar)) { - if (zip.getEntry("module-info.class") != null) { - continue; + Enumeration iterator = zip.entries(); + + boolean found = false; + + while (iterator.hasMoreElements()) { + // Get entry + ZipEntry entry = iterator.nextElement(); + // Get name + String name = entry.getName(); + // Check name + if (name.equals("module-info.class")) { + found = true; + break; + } else { + // Get match + Matcher matcher = PATTERN.matcher(name); + // Check match + if (matcher.find()) { + // Get version + String version = matcher.group(1); + // Parse version + int versionNumber = Integer.parseInt(version); + // Check version + if (versionNumber <= targetJavaVersionNumber) { + found = true; + break; + } + } + } } - if (zip.getEntry("META-INF/versions/") != null) { + + if (found) { continue; } }