6

I am looking for a Maven-Plugin (or an other maven way) to enforce that all dependencies of a Maven project are compiled for the right java major version class file format.

Background: I am downgrading an existing project from Java 7 to Java 6, and I need to check that the libs are compiled for Java 6 (major version 50)

(Using jdk6 and hope that every library is used in at least one test, is not the solution I am looking for.)

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • Major architectural changes without ending at least the version can lead to problems. Why don't you simply update the version of all projects that migrate to Java 6, or change the artifact name? – Danubian Sailor Jul 29 '14 at 11:56
  • Are you wanting to ensure that Maven compiles *your* modules using Java 6? In that case, see http://stackoverflow.com/questions/6833319/maven-is-not-using-the-proper-java-compiler Or are you wanting to check that third-party modules are compiled using Java 6? – Raedwald Jul 29 '14 at 11:59
  • I asked **How can I verify all class files and Jar files used in my app are compiled for Java 8?** at https://stackoverflow.com/questions/75794935/how-can-i-verify-all-class-files-and-jar-files-used-in-my-app-are-compiled-for-j – PatS Mar 25 '23 at 18:28

1 Answers1

10

I would suggest to use the maven-enforcer-plugin in relationship with the extra-enforcer-rules for byte code version.

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.0</version> <!-- find the latest version at http://maven.apache.org/plugins/maven-enforcer-plugin/ -->
        <executions>
          <execution>
            <id>enforce-bytecode-version</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <enforceBytecodeVersion>
                  <maxJdkVersion>1.5</maxJdkVersion>
                  <excludes>
                    <exclude>org.mindrot:jbcrypt</exclude>
                  </excludes>
                </enforceBytecodeVersion>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>extra-enforcer-rules</artifactId>
            <version>1.0-beta-2</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
prunge
  • 22,460
  • 3
  • 73
  • 80
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Is there a newer version of this answer for Java 8? I'm using Java 11 and targeting Java8 but finding some libraries are not being compiled with target set to Java 8. – PatS Mar 20 '23 at 20:07
  • If you set the the rule to require maxJdkVersion means to have max target compiled classes ... if you like to define a minimumJDk version (https://www.mojohaus.org/extra-enforcer-rules/enforceBytecodeVersion.html). Furthermore the target might not be neccesary to be JDK8 or JDK11.. to run on JDK11... – khmarbaise Mar 24 '23 at 17:41