Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven project dependency against JDK version

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.

like image 203
Andrea Polci Avatar asked Apr 02 '10 14:04

Andrea Polci


People also ask

What happens if you don't specify a version in Maven?

Maven won't allow any other either. Build will fail if version is not found.

Does Maven work with Java 11?

Chances are that it just runs fine with Java 11. Hint: You can speed up multi-module Maven projects by using parallel builds, e.g. mvn -T 4 compile compiles all modules in parallel on 4 CPU cores. You will eventually face different compiler errors such as ClassNotFoundException .

Which JDK is used by Maven?

Maven Configuration The Maven tool uses JDK version 11.0.


2 Answers

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>
like image 122
Simone Vellei Avatar answered Sep 28 '22 16:09

Simone Vellei


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

like image 34
Pascal Thivent Avatar answered Sep 28 '22 17:09

Pascal Thivent