使用Maven设置Java编译器的-source和-target不起作用 [英] Setting the -source and -target of the Java Compiler with Maven - doesn't work

查看:794
本文介绍了使用Maven设置Java编译器的-source和-target不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了我的pom文件,要求Maven使用 source target config params。这是我的pom:

I have set my pom file to ask Maven to compile my source code to be version 1.5 compatible using the source and target config params. Here is my pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com</groupId>
  <artifactId>user</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>test</name>
   <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

我有一个像这样的简单主类:

And I have a simple main class like this:

package com.user;
public class Test
{
    public static void main(String[] argv)
    {
        System.out.println("".isEmpty());
    }
}

String#isEmpty() 是从Java 1.6开始引入的。但是,用 mvn compile 编译我的代码是有效的,而我原本预计它会失败,因为我已经设置Maven将我的代码编译为Java 1.5和 String#isEmpty 是在Java 1.6中引入的。任何人都可以建议我可能做错了什么?在编译时强制Maven使用特定Java版本的正确方法是什么?

String#isEmpty() was introduced since Java 1.6. However, compiling my code with mvn compile works, while I would have expected it to fail because I have set Maven to compile my code to Java 1.5 and String#isEmpty was introduced in Java 1.6. Could anyone please suggest what I might have done wrong? What is the correct way to force Maven to use a particular Java version when compiling?

有关信息,我使用的是Apache Maven 2.2.1和javac 1.6.0_19。

For information, I am using Apache Maven 2.2.1 and javac 1.6.0_19.

谢谢。

推荐答案

从<底部的注释< a href =http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html\"rel =noreferrer>编译器 - 插件页面 :


仅设置目标选项并不能保证您的代码实际上在具有指定版本的JRE上运行。缺点是无意中使用的API只存在于以后的JRE中,这会使您的代码在运行时因链接错误而失败。要避免此问题,您可以配置编译器的引导类路径以匹配目标JRE,或使用Animal Sniffer Maven插件验证您的代码不使用非预期的API

Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error. To avoid this issue, you can either configure the compiler's boot classpath to match the target JRE or use the Animal Sniffer Maven Plugin to verify your code doesn't use unintended APIs

这意味着虽然您正在生成1.5级字节码,但您仍然可以(无意中)调用1.6 API方法。要标记这些invalide API调用,请使用他们提到的插件。

This means that although you're generating 1.5-level bytecode, you can still (unintentionally) call a 1.6 API method. To flag these invalide API calls, use the plugin they mention.

这篇关于使用Maven设置Java编译器的-source和-target不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆