2
votes

In Maven, I have a codebase that I want to build that needs to target the 1.4 JVM. This is very easy to do in the pom, but I have one problem: The tests for this codebase use 1.5+ constructs.

Is it possible to have Maven compile/run the tests inside a 1.6 JVM, but build the main codebase to target 1.4?

Setting source to 1.6 and target to 1.4 don't work. Maven/Java don't allow this combination.

3

3 Answers

1
votes

This is possible, but you need to set the parameters for testCompile rather than compile. You can specify a different target/source combination for the testCompile that you use for the compile.

So for compile you've have a target of 1.4, and testCompile 1.5 or 1.6.

Also, to run the unit tests, you can specify the jvm to use in surefire by using the jvm parameter. This would point to a 1.6 jvm.

1
votes

It's been awhile since I've done this, but the plugin dependency you want is something like this:

<plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <executions>
    <execution>
      <id>default-compile</id>
      <configuration>
        <source>1.3</source>
        <target>1.3</target>
      </configuration>
    </execution>
    <execution>
      <id>default-testCompile</id>
      <configuration>
        <source>1.5</source>
        <target>1.5</target>
      </configuration>
    </execution>
  </executions>
</plugin>

link to source

1
votes

I was able to achieve what I wanted with this:

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.3.2</version>
          <configuration>
            <testSource>1.6</testSource>
            <testTarget>1.6</testTarget>
            <target>1.4</target>
            <source>1.4</source>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>