3636import java .util .Enumeration ;
3737import java .util .HashMap ;
3838import java .util .List ;
39+ import java .util .regex .Matcher ;
40+ import java .util .regex .Pattern ;
3941import java .util .zip .ZipEntry ;
4042import java .util .zip .ZipFile ;
4143
5557@ Mojo (name = "patch" , defaultPhase = LifecyclePhase .PACKAGE )
5658public class PatchMojo extends BaseMojo {
5759
60+ private static final Pattern PATTERN = Pattern .compile ("^META-INF/versions/([0-9]*)/module-info.class$" );
61+
62+ private static final String JAVA_VERSION = System .getProperty ("java.version" );
63+
64+ private static final String JAVA_VERSION_MAJOR = JAVA_VERSION .substring (0 , JAVA_VERSION .indexOf ('.' ));
65+
5866 @ Parameter
5967 private boolean ignoreMissingDeps ;
6068
@@ -64,6 +72,10 @@ public class PatchMojo extends BaseMojo {
6472 @ Override
6573 protected final void run () throws MojoExecutionException , MojoFailureException {
6674
75+ String targetJavaVersion = multiRelease != null ? multiRelease : JAVA_VERSION_MAJOR ;
76+
77+ int targetJavaVersionNumber = Integer .parseInt (targetJavaVersion );
78+
6779 File [] jars = modulePath .listFiles (file -> !file .isDirectory () && file .getName ().endsWith (".jar" ));
6880
6981 for (File jar : jars ) {
@@ -73,10 +85,38 @@ protected final void run() throws MojoExecutionException, MojoFailureException {
7385 System .out .println ("[" + jar .getName () + "] Checking module info" );
7486
7587 try (ZipFile zip = new ZipFile (jar )) {
76- if (zip .getEntry ("module-info.class" ) != null ) {
77- continue ;
88+ Enumeration <? extends ZipEntry > iterator = zip .entries ();
89+
90+ boolean found = false ;
91+
92+ while (iterator .hasMoreElements ()) {
93+ // Get entry
94+ ZipEntry entry = iterator .nextElement ();
95+ // Get name
96+ String name = entry .getName ();
97+ // Check name
98+ if (name .equals ("module-info.class" )) {
99+ found = true ;
100+ break ;
101+ } else {
102+ // Get match
103+ Matcher matcher = PATTERN .matcher (name );
104+ // Check match
105+ if (matcher .find ()) {
106+ // Get version
107+ String version = matcher .group (1 );
108+ // Parse version
109+ int versionNumber = Integer .parseInt (version );
110+ // Check version
111+ if (versionNumber <= targetJavaVersionNumber ) {
112+ found = true ;
113+ break ;
114+ }
115+ }
116+ }
78117 }
79- if (zip .getEntry ("META-INF/versions/" ) != null ) {
118+
119+ if (found ) {
80120 continue ;
81121 }
82122 }
0 commit comments