Skip to content

Commit

Permalink
Add strict option to EnforceBytecodeVersion to not ignore module-info…
Browse files Browse the repository at this point in the history
… and Multi-Release JAR classes
  • Loading branch information
Clement Cherlin authored and slawekjaranowski committed Jun 8, 2022
1 parent c795041 commit a41a85d
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = enforcer:enforce
invoker.buildResult = failure
51 changes: 51 additions & 0 deletions src/it/enforce-bytecode-version-module-info-jdk8-strict/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>pim.pam.poum</groupId>
<artifactId>smoking</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>6.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>@enforcerPluginVersion@</version>
<dependencies>
<dependency>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
</dependency>
</dependencies>
<configuration>
<rules>
<enforceBytecodeVersion>
<maxJdkVersion>1.8</maxJdkVersion>
<strict>true</strict>
</enforceBytecodeVersion>
</rules>
</configuration>
<executions>
<execution>
<phase>compile</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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( 'Found Banned Dependency: org.ow2.asm:asm:jar:6.0' )

return true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = enforcer:enforce
invoker.buildResult = success
51 changes: 51 additions & 0 deletions src/it/enforce-bytecode-version-module-info-jdk9-strict/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>pim.pam.poum</groupId>
<artifactId>smoking</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>6.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>@enforcerPluginVersion@</version>
<dependencies>
<dependency>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
</dependency>
</dependencies>
<configuration>
<rules>
<enforceBytecodeVersion>
<maxJdkVersion>1.9</maxJdkVersion>
<strict>true</strict>
</enforceBytecodeVersion>
</rules>
</configuration>
<executions>
<execution>
<phase>compile</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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( 'Found Banned Dependency: org.ow2.asm:asm:jar:6.0' )

return true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = enforcer:enforce
invoker.buildResult = failure
54 changes: 54 additions & 0 deletions src/it/enforce-bytecode-version-multirelease-strict/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>pim.pam.poum</groupId>
<artifactId>smoking</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.2</version>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>@enforcerPluginVersion@</version>

<dependencies>
<dependency>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
</dependency>
</dependencies>

<configuration>
<rules>
<enforceBytecodeVersion>
<maxJdkVersion>1.8</maxJdkVersion>
<strict>true</strict>
</enforceBytecodeVersion>
</rules>
</configuration>

<executions>
<execution>
<phase>compile</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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( 'Found Banned Dependency: org.apache.logging.log4j:log4j-api:jar:2.17.2' )

return true;
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

/**
* Enforcer rule that will check the bytecode version of each class of each dependency.
*
*
* @see <a href="http://en.wikipedia.org/wiki/Java_class_file#General_layout">Java class file general layout</a>
* @since 1.0-alpha-4
*/
Expand Down Expand Up @@ -148,7 +148,7 @@ static String renderVersion( int major, int minor )

/**
* This parameter is here for potentially advanced use cases, but it seems like it is actually always 0.
*
*
* @see #maxJavaMajorVersionNumber
* @see <a href="https://en.wikipedia.org/wiki/Java_class_file#General_layout">Java class file general layout</a>
*/
Expand All @@ -167,6 +167,11 @@ static String renderVersion( int major, int minor )
*/
private String[] ignoreClasses;

/**
* Process module-info and Multi-Release JAR classes if true
*/
private boolean strict = false;

/**
* Optional list of dependency scopes to ignore. {@code test} and {@code provided} make sense here.
*/
Expand Down Expand Up @@ -240,7 +245,7 @@ private void computeParameters()
"\"1.7\", \"8\", \"11\", \"12\", \"13\", \"14\", \"15\", \"16\", \"17\"" );
}
maxJavaMajorVersionNumber = needle;
if ( needle < 53 )
if ( !strict && needle < 53 )
{
IgnorableDependency ignoreModuleInfoDependency = new IgnorableDependency();
ignoreModuleInfoDependency.applyIgnoreClasses(DEFAULT_CLASSES_IGNORE_BEFORE_JDK_9, false );
Expand Down Expand Up @@ -342,17 +347,17 @@ private String isBadArtifact( Artifact a )
int major = ( magicAndClassFileVersion[6] << 8 ) + magicAndClassFileVersion[7];

// Assuming regex match is more expensive, verify bytecode versions first

if ( ( major > maxJavaMajorVersionNumber )
|| ( major == maxJavaMajorVersionNumber && minor > maxJavaMinorVersionNumber ) )
{

Matcher matcher = MULTIRELEASE.matcher( entry.getName() );
if ( matcher.matches() )

if ( !strict && matcher.matches() )
{
Integer maxExpectedMajor = JDK_TO_MAJOR_VERSION_NUMBER_MAPPING.get( matcher.group( 1 ) );

if (maxExpectedMajor == null) {
getLog().warn( "Unknown bytecodeVersion for " + a + " : "
+ entry.getName() + ": got " + maxExpectedMajor + " class-file-version" );
Expand Down Expand Up @@ -423,6 +428,15 @@ public void setSearchTransitive( boolean theSearchTransitive )
this.searchTransitive = theSearchTransitive;
}

/**
* Process module-info and Multi-Release JAR classes if true
* @param strict the strictness to set
*/
public void setStrict( boolean strict )
{
this.strict = strict;
}

// copied from RequireReleaseDeps
/*
* Filter the dependency artifacts according to the includes and excludes If includes and excludes are both null,
Expand Down
4 changes: 3 additions & 1 deletion src/site/apt/enforceBytecodeVersion.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ Enforce Bytecode Version

* ignoreOptionals - a boolean, if <<<true>>> all dependencies which have <<<<optional>true</optional>>>> are ignored.

* strict - a boolean, if <<<true>>> process module-info and Multi-Release JAR classes

[]

Note
Note
Sample Plugin Configuration:

+---+
Expand Down

0 comments on commit a41a85d

Please sign in to comment.