From 31350a94589397bd90547b3d3ceea07a43a4362c Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Wed, 20 Mar 2019 10:40:26 +0100 Subject: [PATCH] Ignore automatically module-info.class when maxJdkVersion < 1.9 Context: #46, #36, #75 --- .../invoker.properties | 2 + .../pom.xml | 48 +++++++++++++++++++ .../verify.groovy | 9 ++++ .../invoker.properties | 2 + .../pom.xml | 48 +++++++++++++++++++ .../verify.groovy | 8 ++++ .../pom.xml | 35 -------------- .../verify.groovy | 1 + .../enforcer/EnforceBytecodeVersion.java | 23 +++++---- 9 files changed, 132 insertions(+), 44 deletions(-) create mode 100644 src/it/enforce-bytecode-version-module-info-jdk8/invoker.properties create mode 100644 src/it/enforce-bytecode-version-module-info-jdk8/pom.xml create mode 100644 src/it/enforce-bytecode-version-module-info-jdk8/verify.groovy create mode 100644 src/it/enforce-bytecode-version-module-info-jdk9/invoker.properties create mode 100644 src/it/enforce-bytecode-version-module-info-jdk9/pom.xml create mode 100644 src/it/enforce-bytecode-version-module-info-jdk9/verify.groovy diff --git a/src/it/enforce-bytecode-version-module-info-jdk8/invoker.properties b/src/it/enforce-bytecode-version-module-info-jdk8/invoker.properties new file mode 100644 index 00000000..a30e3767 --- /dev/null +++ b/src/it/enforce-bytecode-version-module-info-jdk8/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = enforcer:enforce +invoker.buildResult = success diff --git a/src/it/enforce-bytecode-version-module-info-jdk8/pom.xml b/src/it/enforce-bytecode-version-module-info-jdk8/pom.xml new file mode 100644 index 00000000..436a31ba --- /dev/null +++ b/src/it/enforce-bytecode-version-module-info-jdk8/pom.xml @@ -0,0 +1,48 @@ + + 4.0.0 + + pim.pam.poum + smoking + 1.0-SNAPSHOT + + + + + maven-enforcer-plugin + @enforcerPluginVersion@ + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + + + 1.8 + + + + + + compile + + + + + + + UTF-8 + + + + + org.ow2.asm + asm + 6.0 + runtime + + + diff --git a/src/it/enforce-bytecode-version-module-info-jdk8/verify.groovy b/src/it/enforce-bytecode-version-module-info-jdk8/verify.groovy new file mode 100644 index 00000000..72ed932d --- /dev/null +++ b/src/it/enforce-bytecode-version-module-info-jdk8/verify.groovy @@ -0,0 +1,9 @@ +File file = new File( basedir, "build.log" ); +assert file.exists(); + +String text = file.getText("utf-8"); + +assert text.contains( '[INFO] Adding ignore: module-info' ) +assert text.contains( '[DEBUG] Ignore: module-info maps to regex ^module-info(\\.class)?$' ) + +return true; diff --git a/src/it/enforce-bytecode-version-module-info-jdk9/invoker.properties b/src/it/enforce-bytecode-version-module-info-jdk9/invoker.properties new file mode 100644 index 00000000..a30e3767 --- /dev/null +++ b/src/it/enforce-bytecode-version-module-info-jdk9/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = enforcer:enforce +invoker.buildResult = success diff --git a/src/it/enforce-bytecode-version-module-info-jdk9/pom.xml b/src/it/enforce-bytecode-version-module-info-jdk9/pom.xml new file mode 100644 index 00000000..bf2a866f --- /dev/null +++ b/src/it/enforce-bytecode-version-module-info-jdk9/pom.xml @@ -0,0 +1,48 @@ + + 4.0.0 + + pim.pam.poum + smoking + 1.0-SNAPSHOT + + + + + maven-enforcer-plugin + @enforcerPluginVersion@ + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + + + + 1.9 + + + + + + compile + + + + + + + UTF-8 + + + + + org.ow2.asm + asm + 6.0 + runtime + + + diff --git a/src/it/enforce-bytecode-version-module-info-jdk9/verify.groovy b/src/it/enforce-bytecode-version-module-info-jdk9/verify.groovy new file mode 100644 index 00000000..6c603afe --- /dev/null +++ b/src/it/enforce-bytecode-version-module-info-jdk9/verify.groovy @@ -0,0 +1,8 @@ +File file = new File( basedir, "build.log" ); +assert file.exists(); + +String text = file.getText("utf-8"); + +assert ! text.contains( '[INFO] Adding ignore: module-info' ) + +return true; diff --git a/src/it/enforce-bytecode-version-multirelease/pom.xml b/src/it/enforce-bytecode-version-multirelease/pom.xml index 59403bac..f4e6871c 100644 --- a/src/it/enforce-bytecode-version-multirelease/pom.xml +++ b/src/it/enforce-bytecode-version-multirelease/pom.xml @@ -44,40 +44,5 @@ 2.9.0 runtime - - - org.glassfish.hk2.external - asm-all-repackaged - 2.5.0-b32 - runtime - - - - org.ow2.asm - asm-util - 6.0 - runtime - - - - org.ow2.asm - asm-tree - 6.0 - runtime - - - - org.ow2.asm - asm - 6.0 - runtime - - - - org.ow2.asm - asm-analysis - 6.0 - runtime - diff --git a/src/it/enforce-bytecode-version-multirelease/verify.groovy b/src/it/enforce-bytecode-version-multirelease/verify.groovy index d8e8e851..9159b0f8 100644 --- a/src/it/enforce-bytecode-version-multirelease/verify.groovy +++ b/src/it/enforce-bytecode-version-multirelease/verify.groovy @@ -3,6 +3,7 @@ assert file.exists(); String text = file.getText("utf-8"); +assert text.contains( '[INFO] Adding ignore: module-info' ) assert text.contains( '[DEBUG] log4j-api-2.9.0.jar => ' ) return true; diff --git a/src/main/java/org/apache/maven/plugins/enforcer/EnforceBytecodeVersion.java b/src/main/java/org/apache/maven/plugins/enforcer/EnforceBytecodeVersion.java index 6fa0ff8c..f81b3eef 100644 --- a/src/main/java/org/apache/maven/plugins/enforcer/EnforceBytecodeVersion.java +++ b/src/main/java/org/apache/maven/plugins/enforcer/EnforceBytecodeVersion.java @@ -57,9 +57,12 @@ public class EnforceBytecodeVersion extends AbstractResolveDependencies { private static final Map JDK_TO_MAJOR_VERSION_NUMBER_MAPPING = new LinkedHashMap(); - + /** + * Default ignores when validating against jdk < 9 because module-info.class will always have level 1.9. + */ + private static final String[] DEFAULT_CLASSES_IGNORE_BEFORE_JDK_9 = {"module-info"}; + private final Pattern MULTIRELEASE = Pattern.compile( "META-INF/versions/(\\d+)/.*" ); - private final String MODULE_INFO_CLASS = "module-info.class"; static { @@ -218,6 +221,12 @@ private void computeParameters() "\"1.7\", \"8\", \"11\", \"12\"" ); } maxJavaMajorVersionNumber = needle; + if ( needle < 53 ) + { + IgnorableDependency ignoreModuleInfoDependency = new IgnorableDependency(); + ignoreModuleInfoDependency.applyIgnoreClasses(DEFAULT_CLASSES_IGNORE_BEFORE_JDK_9, false ); + ignorableDependencies.add( ignoreModuleInfoDependency ); + } } if ( maxJavaMajorVersionNumber == -1 ) { @@ -322,18 +331,14 @@ private String isBadArtifact( Artifact a ) Matcher matcher = MULTIRELEASE.matcher( entry.getName() ); - if ( MODULE_INFO_CLASS.equals( entry.getName() ) ) { - getLog().warn( "Invalid bytecodeVersion for " + entry.getName() + ": expected " - + maxJavaMajorVersionNumber + ", but was " + major); - } - else if ( matcher.matches() ) + if ( matcher.matches() ) { int expectedMajor = JDK_TO_MAJOR_VERSION_NUMBER_MAPPING.get( matcher.group( 1 ) ); if ( major != expectedMajor ) { - getLog().warn( "Invalid bytecodeVersion for " + entry.getName() + ": expected " - + expectedMajor + ", but was " + major ); + getLog().warn( "Invalid bytecodeVersion for " + a + " : " + + entry.getName() + ": expected " + expectedMajor + ", but was " + major ); } } else