【问题标题】:How to force Maven to target Java 1.7 with a javax.persistence dependency?如何强制 Maven 使用 javax.persistence 依赖项以 Java 1.7 为目标?
【发布时间】:2017-02-20 14:44:47
【问题描述】:

我在 pom.xml 文件中设置这个:

<properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>


<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
        </plugin>
    </plugins>
</build>

当我打开 .jar 文件并使用 javap -v 检查 .class 文件时,它会输出对应于 1.8 的“主要版本:52”,所以我看到它不尊重我的配置。

我对 javax.persistence 有依赖:

<dependencies>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>
</dependencies>

如果我删除这个依赖,javap -v 会输出“major version: 51”,这是我想要的目标。

但我需要依赖。

是否可以使用 javax.persistence 依赖项来定位 1.7?怎么样?

如果它不能像我配置的那样以 Java 1.7 为目标,我预计会出现编译错误。

我正在使用 IntelliJ IDEA。

谢谢。

【问题讨论】:

  • 我用简单的项目对其进行了检查,并且此依赖项的存在不会影响输出类版本。所以你的项目中必须有其他东西。

标签: java maven maven-3 maven-compiler-plugin


【解决方案1】:

你试过了吗?

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

【讨论】:

  • 这正是原始海报所使用的......没有区别。
  • 我试过了,但似乎是等价的。
  • 我有一个项目,它使用这个确切的依赖插件和 javax.persistence 作为依赖,它工作正常,因此我认为它可能会有所不同,我也怀疑你的项目设置可能有所不同,也许检查 ide 设置
猜你喜欢
  • 2012-01-04
  • 1970-01-01
  • 2014-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-06
  • 2022-10-04
  • 2016-01-11
相关资源
最近更新 更多