Travis Java build using different SDK for compilation and tests - java

I have a library that uses Java 8 classes if they are available and for older JRE versions provides a fallback implementation. It means I have to compile using Java 8 (or higher) but I want to execute the tests with JDK 7 to test the fallback. I can not figure out how to do it in Travis.

The easiest way is to create special Maven profile for Travis in pom.xml
<profile>
<id>travis</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable>/usr/lib/jvm/java-8-oracle/bin/javac</executable>
</configuration>
</plugin>
</plugins>
</build>
</profile>
And then activate it in .travis.yml
script: mvn install -P travis

Related

Maven incremental compilation for tests only

I know, that by using the useIncrementalCompilation parameter, it's possible to enable incremental compilation, for example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
But in this case, both classes in src and tests are compiled incrementally.
I would like it to be for tests only. Is it possible?
My environment:
Java 1.8
Apache Maven 3.8.2
maven-compiler-plugin 3.1
I'm build project by this way: mvn clean install
Maybe there is some plugin that would allow me to compile tests (tests only) incrementally?

after upgrading to java 12, in which syntax and for what to use for surefire plugin and maven

i upgraded my java version to 12.0.2 in my system, and i have few projects where i have used maven 3.6.2, and some i used maven + surefire plugin 3.0.0-M8.
since i know that maven is working with java 1.6 and i need to update the compiler version to work with these, also i need to update the surefire plugin to work with the java 12 version,
so i want to understand in what to use, and on what (Maven or surefire or both) and also if the syntax is the right one?
also if these is not the right way, so what is the right one?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable> ${JAVA_HOME}/bin/javac </executable>
</configuration>
</plugin>
also
<properties>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
and also
<properties>
<maven.compiler.release>12</maven.compiler.release>
</properties>
and also
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>12</release>
</configuration>
</plugin>
</plugins>
</build>

Problem running tests with enabled preview features in surefire and failsafe

I'm trying to migrate a project to Java 12, with --enable-preview.
I added --enable-preview in compiler settings:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>12</release>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>
And also added it in argLine for surefire and failsafe:
<properties>
<argLine>--enable-preview</argLine>
</properties>
And do a mvn clean verify results in:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M3:test (default-test) on project lombok-jdk10: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M3:test failed: java.lang.UnsupportedClassVersionError: Preview features are not enabled for com/kirela/lombok/BarTest (class file version 56.65535). Try running with '--enable-preview' -> [Help 1]
I also tried adding argLine directly to surefire/failsafe configuration, but the result is same.
What am I missing here?
I this a bug in surefire/failsafe?
EDIT2: Surefire and failsafe config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<forkCount>2</forkCount>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<forkCount>2</forkCount>
</configuration>
</plugin>
EDIT3:
Minimal working example is here: https://github.com/krzyk/lombok-jdk10-example
The project fails with --enable-preview, but works when I remove it.
This works for me:
mvn clean install works (with junit tests)
IDEA recognizes module language level correctly as 12 (Preview) - Switch expressions
junit tests in IDEA work
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>12</release>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
Environment:
Ubuntu 18.04.3 x64
IDEA 2019.2.1
Maven 3.6.0
jdk:
IMPLEMENTOR="Oracle Corporation"
JAVA_VERSION="12"
JAVA_VERSION_DATE="2019-03-19"
ADDITION: Similarly this approach works for java 13.
ADDITION: Similarly this approach works for java 17.
There are two solutions:
Add --enable-preview to MAVEN_OPTS environment variable.
Explanation by the maintainer of surefire:
The argLine does what it has to do without any issue.
The plugin runs JUnit filter which finally selects relevant classes to run in one or multiple JVMs.
So the JUnit engine runs twice. Once in plugin JVM, and second in the forked JVM.
Due to the classes are compiled with different major or minor version (in bytecode of *.class files) than the version of Java runtime supports in Maven, this JRE fails because Java in Maven does not understand the bytecode. So, it is curious that the same JVM (javac) produced two major versions depending on JVM option and java from the same JVM does not understand it been incompatible for itself. Although version in forked JVM is totally fine and understands the the classes compiled by javac because javac and forked JVM start with the same option --enable-preview.
It is the same situation as if you compiled your sources with Java 12 by maven-compiler-plugin using the toolchain and run the whole Maven build with Java 11. So the classes would be compiled with higher version (in bytecode) than the JRE could understand in Maven process.
We have a wish to rework providers and perform the filtering inside of the forked JVM but this is very compilicated change and still questionary.
The issue is that I used forkCount, it appears surefire doesn't pass parameters to JVM run in fork.
Remove the forkCount parameter from surefire/failsafe configuration.
This will of course cause the tests to run in a single JVM, so if you wanted to speed up the tests using the forks, it won't work now.
Add a configuration for surefire and failsafe maven plugin
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>

How to compile maven web project in different version and run in different version

I have created a maven web project in eclipse. I want to compile it in jdk 1.6 and have its run time environment as 1.8.I have tomcat as the web container to run the project. For compiling I am aware of the Compliance level option in the project settings. Is it the correct way to go about the compile time requirement? And what settings do I have to change to make it run on java 1.8?
Use the Maven compiler plugin to set the source and runtime version of jdk & jvm respectively.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.6</source>
<target>1.8</target>
</configuration>
</plugin>
<plugins>
<pluginManagement>
<build>

"The command line is too long" -- when running maven test

Running $mvn test on a 64-bit Windows gives me the following error, even if I do $mvn test -Dgwt.genParam=false:
The command line is too long
Make sure you are using version 2.16 and that you have the useManifestOnlyJar option (as documented here).
For example:
<project>
[...]
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<useManifestOnlyJar>true</useManifestOnlyJar>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
[...]
</project>
This will create jar with a manifest that re-creates your classpath (as opposed to setting it via the CLASSPATH variable which is an approach that is affected by Windows' command-line limit problem).

Categories