Maven依赖的必需Java版本

Maven依赖的必需Java版本

本文介绍了Maven依赖的必需Java版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Maven开发并构建了我的Java应用程序。我需要支持Java 1.6,所以我使用以下属性:

I developed and built my Java application using Maven. I need to support Java 1.6, so I use the following properties:

<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.source>1.6</maven.compiler.source>

但是,当我运行应用程序时,我收到不支持的major.minor版本错误,我怀疑我的一个依赖项库是使用比我需要支持的Java版本更新的Java版本编译的。

Still, when I run the application I get an "Unsupported major.minor version" error and I suspect that one of my dependency jars is compiled with a Java version newer than the one I need to support.

我的问题:


  1. 这甚至可能吗?我认为Maven会处理这种依赖版本的问题。

  1. Is this even possible? I thought Maven would take care of this kind of dependency version problems.

有没有一种简单的方法可以找出我所有依赖项的次要/主要版本? (如果在执行 mvn依赖:树时可以显示它会很棒。)

Is there an easy way to find out the minor/major version of all my dependencies? (Would be awesome if it could be shown when doing mvn dependency:tree for instance.)


推荐答案

问题是每个依赖项(维护者)都可以决定使用哪个java版本来编译(1.5,1.6,1.7,1.8等)所以这不能通过Maven解决。但是你可以确保你不使用使用不同Java版本的依赖项。

The problem is that every dependency (maintainer) can decide on it's own which java version is used to compile (1.5, 1.6, 1.7, 1.8 etc) so this is not solvable via Maven. But you can make sure that you don't use dependencies which are using a different Java version than you like to have.

这可以通过使用 extra-enforcer-rules / enforceBytecodeVersion.htmlrel =noreferrer> extra-enforcer-rules :

This can be enfored by using Maven Enforcer Plugin by using extra-enforcer-rules:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.4.1</version> <!-- find the latest version at http://maven.apache.org/plugins/maven-enforcer-plugin/ -->
        <executions>
          <execution>
            <id>enforce-bytecode-version</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <enforceBytecodeVersion>
                  <maxJdkVersion>1.6</maxJdkVersion>
                  <excludes>
                    <exclude>org.mindrot:jbcrypt</exclude>
                  </excludes>
                </enforceBytecodeVersion>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>extra-enforcer-rules</artifactId>
            <version>1.0-beta-5</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

如果您的依赖项使用不同版本的JDK编译,这将破坏您的构建想拥有。

This will break your build if you have a dependency which is compiled with a different version of JDK than you want to have.

这篇关于Maven依赖的必需Java版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 17:47