Maven project dependency against JDK version

2019-04-07 04:12发布

问题:

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.

回答1:

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>


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



回答3:

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