How to check that all Maven dependencies are compiled for Java 6
Asked Answered
N

1

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.)

Noll answered 29/7, 2014 at 11:49 Comment(3)
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?Musketry
Are you wanting to ensure that Maven compiles your modules using Java 6? In that case, see #6833819 Or are you wanting to check that third-party modules are compiled using Java 6?Kristelkristen
I asked How can I verify all class files and Jar files used in my app are compiled for Java 8? at #75795435Mucor
O
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>
Ohmmeter answered 29/7, 2014 at 12:0 Comment(2)
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.Mucor
If you set the the rule to require maxJdkVersion means to have max target compiled classes ... if you like to define a minimumJDk version (mojohaus.org/extra-enforcer-rules/enforceBytecodeVersion.html). Furthermore the target might not be neccesary to be JDK8 or JDK11.. to run on JDK11...Ohmmeter

© 2022 - 2024 — McMap. All rights reserved.