7

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.

cb4
  • 6,689
  • 7
  • 45
  • 57
Andrea Polci
  • 1,011
  • 13
  • 27

3 Answers3

2

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

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
2

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>
Simone Vellei
  • 342
  • 1
  • 4
  • 15
1

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

ant
  • 22,634
  • 36
  • 132
  • 182
  • 1
    No, as I said it's not a question of source and target parameter because this influence only how the compiler read the language (source) and generate the bytecode (target) but not the libraries used in the compilation. If you compile using a 1.6 jdk a project with 1.5 as source/target parameter you still can get a jar that is not valid for execution with a 1.5 jre (for example there can be problems with overloaded methods in library classes). – Andrea Polci Apr 02 '10 at 14:25
  • @Andrea then Pascal solution is the answer – ant Apr 02 '10 at 14:27
  • I don't think profiles solve my problem. I need to force maven to use a specific jdk when compiling my project. The best solution seems to be the one proposed by Simone. – Andrea Polci Apr 02 '10 at 15:23