views:

37

answers:

3

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.

+1  A: 

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

c0mrade
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
@Andrea then Pascal solution is the answer
c0mrade
yes, I think profiles are what you need.
Ken Liu
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
+2  A: 

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
Thanks the enforcer plugin is really useful to me even if it doesn't solve completely my problem.
Andrea Polci
+2  A: 

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
Thanks, this solves my problem
Andrea Polci