Skip to content

Commit

Permalink
check module descriptors hidden inside META-INF/versions folder
Browse files Browse the repository at this point in the history
  • Loading branch information
ghackenberg committed Oct 21, 2021
1 parent a8ff841 commit b762351
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.ghackenberg</groupId>
<artifactId>jigsaw-maven-plugin</artifactId>
<version>1.0.2</version>
<version>1.1.0</version>
<packaging>maven-plugin</packaging>
<name>Maven Java Platform Module System (JPMS) Plugin</name>
<description>Plugin to simplify software linking and packaging on top of the Java Platform Module system (JPMS).</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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) {
Expand All @@ -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<? extends ZipEntry> 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;
}
}
Expand Down

0 comments on commit b762351

Please sign in to comment.