1

I would like to use java 7 in my tests and java 6 for the code. How to achieve this?

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
pixel
  • 24,905
  • 36
  • 149
  • 251
  • 3
    It's a really bad idea, IMHO. – Konstantin Yovkov Oct 07 '13 at 08:37
  • If you have any control over the environments, then it'd make sense to get them both on the same JDK version. – DaveH Oct 07 '13 at 09:25
  • I don't have any control. On code I'm dependent on java 1.6 and in test I want to use newer syntax. – pixel Oct 07 '13 at 09:57
  • possible duplicate of [Different maven compiler versions for test and main](http://stackoverflow.com/questions/1213897/different-maven-compiler-versions-for-test-and-main) – Joe Oct 07 '13 at 11:10

2 Answers2

1

You can set the jvm that is used by the maven-surefire-plugin to point to the java 7 executable.

See http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#jvm

René Link
  • 48,224
  • 13
  • 108
  • 140
  • Maven fails on: `org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project MyProject: Compilation failure` – pixel Oct 07 '13 at 08:51
  • You should set the source and target properties of the maven-compiler-plugin to 1.6 and ensure that the mvn is started using a jdk 6. It seems that your source code is not compatible with the jdk version you started the mvn build. Could you please update the question and add: jdk mvn is started with, maven-compiler-plugin config, maven-surefire-plugin config – René Link Oct 07 '13 at 08:57
1

You can also set source and target in the execution phase testCompile of the maven-compiler-plugin. So try something like this

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <executions>
       <execution>
           <id>testCompileWithJDK7</id>
           <phase>test-compile</phase>
           <configuration>
              <source>1.7</source>
              <target>1.7</target>              
           </configuration> 
       </execution>
    </executions>
</plugin>
Petr Mensik
  • 26,874
  • 17
  • 90
  • 115