Check if library will work on a specific verion of Java - java

What approach can be taken to verify that a library is compatible on a specific version of Java?
Example: Library X was compiled on Java 1.7, therefor it might not work on Java 1.7 or lower.
Thank you.

The best is to check the byte code via an enforcer rule which can be applied to your build by using the maven-enforcer-plugin...
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-bytecode-version</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<enforceBytecodeVersion>
<maxJdkVersion>1.7</maxJdkVersion>
</enforceBytecodeVersion>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>extra-enforcer-rules</artifactId>
<version>1.0-beta-4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
[...]
</project>

compile with the version you want:
javac -target ...
see that: How do i compile a .java with support for older versions of java?
and check jar dependencies: with Jdepend for example
Analyze JAR dependencies in a Java project

Related

Ignore maven execution block which uses toolchain if ~/.m2/toolchains.xml is not present

I'm working on an open-source library which must work correctly on jre7. Since java 9 has been released, we decided to provide our modules with module-info.java files so that users can make use of them if they prefer to use the library with sdk 9 on jre9 (in this case they'll need to manually put in pom.xml this piece of code:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
I've tried to implement the solution provided by maven (https://maven.apache.org/plugins/maven-compiler-plugin/examples/module-info.html) to compile the entire code with jdk7 and to compile only module-info.java files with jdk9. Here is a piece of pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<!-- compile everything to ensure module-info contains right entries -->
<!-- required when JAVA_HOME is JDK 8 or below -->
<jdkToolchain>
<version>9</version>
</jdkToolchain>
<release>9</release>
</configuration>
</execution>
<execution>
<id>base-compile</id>
<goals>
<goal>compile</goal>
</goals>
<!-- recompile everything for target VM except the module-info.java -->
<configuration>
<excludes>
<exclude>module-info.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
<!-- defaults for compile and testCompile -->
<configuration>
<!-- jdkToolchain required when JAVA_HOME is JDK 9 or above -->
<jdkToolchain>
<version>[1.7,9)</version>
</jdkToolchain>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>9</version>
</jdk>
</toolchains>
</configuration>
</plugin>
And I've properly configured ~/.m2/toolchains.xml file. It works perfectly well. But the thing is we need to skip the first execution block, where everything's compiled with jdk9 with module-info, because we don't want to make our users create toolchains.xml file and set up java 9. So what I am probably looking for is the workaround to skip the following code if toolchains.xml is not present in ~/.m2 directory and only execute the second execution block, in which module-info.java files are ignored. Is that even possible? I realise we can still keep two branches (one private with two executional blocks and use of toolchains and another public, which ignores module-info.java files completely, but it doesn't seem an elegant solution.
<execution>
<id>default-compile</id>
<configuration>
<!-- compile everything to ensure module-info contains right entries -->
<!-- required when JAVA_HOME is JDK 8 or below -->
<jdkToolchain>
<version>9</version>
</jdkToolchain>
<release>9</release>
</configuration>
</execution>
You can activate/deactivate certain parts of a pom using Maven profiles.
Wouldn't it be better to provide two different versions of your library, e.g. with version numbers 1.0.0-JDK7 and 1.0.0-JDK9. Then the consumer of your library can choose the right one for their purposes.

How to set custom version of jdk in maven?

I am new in Maven.
I work in Windows and when I try to do the next instructions mvn clean install in folder with pom.xml file it throw me errors like this:
CLASS_NAME.java error: diamond operator is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable diamond operator)
As I can understand from the message it occurs because maven use jdk version 1.5 (Actually I even didn't install it). In maven settings folder I found toolchains.xml file. If I understood right, it is possible to set custom version of jdk for user using this file. So I add this code to my pom.xml file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<configuration>
<toolchains>
<jdk>
<version>[1.8]</version>
</jdk>
</toolchains>
</configuration>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
</plugin>
and this to toolchains.xml
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>C:/Program Files/Java/jdk1.8.0_45</jdkHome>
</configuration>
</toolchain>
If somebody know can you tell me how to fix this? I will appreciate any help, idea or explanation.
P.S. JAVA_HOME is C:\Program Files\Java\jdk1.8.0_45
Try configuring the maven-compiler-plugin instead of the maven-toolchains-plugin in your pom.xml.
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Source

How to check that all Maven dependencies are compiled for Java 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.)
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>

How to pass arguments to GWT compiler using Maven GWT plugin?

I need to pass "-deploy src/main/webapp/WEB-INF/deploy/" argument to GWT Compiler in gwt-maven-plugin configuration.
My purpose is to integrate remote logger using gwt-log.
To achieve I need to pass above mentioned argument to GWT compiler.
Thanks!!
Bhavesh
See gwt-maven-plugin
<project>
[...]
<build>
<plugins>
[...]
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>2.4.0</version>
<executions>
<execution>
<configuration>
<deploy>src/main/webapp/WEB-INF/deploy/</deploy>
</configuration>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
[...]
</plugins>
</build>
[...]
</project>
As noted you'll need to upgrade.
It looks like this parameter is only available since 2.3.0-1 version of gwt-maven-plugin (see here).

Maven project dependency against JDK version

I have projects that need to be build with a specific version of the JDK.
The problem isn't in the source and target parameters but in the jars of the runtime used during compilation.
In some cases I get a compilation error if I try to compile with the wrong JDK, but sometimes the build is successful and I get runtime errors when using the jars.
For example in eclipse I have the ability to establish the execution enviroment for the project in the .classpath file.
Is there a way to handle such situation in maven?
What I would like to have is the ability to handle JRE dependency like other dependencies of the project in the POM file.
UPDATE:
The accepted solution was the best one when I asked this question, so I won't change it. Meanwhile a new solution to this kind of problems has been introduced: Maven Toolchain. Follow the link for further details.
I've found this article:
http://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable>${JAVA_1_4_HOME}/bin/javac</executable>
<compilerVersion>1.3</compilerVersion>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
I have projects that need to be build with a specific version of the JDK.
You can use the Maven Enforcer plugin to enforce the use of a particular version of the JDK:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>1.5</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
But I'm not sure I really understood the question. If this is not what you want, maybe you could declare your JDK specific dependencies in profiles and use an activation trigger based on the JDK version. For example:
<profiles>
<profile>
<activation>
<jdk>1.5</jdk>
</activation>
...
</profile>
</profiles>
This configuration will trigger the profile when the JDK's version starts with "1.5".
I believe that this can be solved with following plugin in your pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
Here you target version 1.6 , or write your own version

Categories