简体   繁体   English

maven在这里使用哪个编译器版本?

[英]Which compiler version maven is using here?

I am using below maven compiler plugin to compile my java code: 我正在使用下面的Maven编译器插件来编译我的Java代码:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <inherited>true</inherited>
    <version>2.5.1</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
</plugin>

My java_home points to Java 7. 我的java_home指向Java 7。

My first question, will my classes be compiled with java 6 or java 7? 我的第一个问题是,我的类将使用Java 6还是Java 7进行编译?

If with java 6 (because of <source>1.6</source> ), how maven will know the path java 1.6 as java home points to 1.7? 如果使用Java 6(由于<source>1.6</source> ),那么maven如何知道java 1.6作为java home指向1.7的路径?

If I need to compile the source code with java 1.8, do I need to set source and target as 1.8? 如果需要使用Java 1.8编译源代码,是否需要将源和目标设置为1.8?

But then, how maven will know that jdk 1.8 is in the path? 但是,然后,maven如何知道jdk 1.8即将出现?

Do i need to change java_home to point to java 8? 我需要更改java_home指向Java 8吗?

Short answers: 简短的答案:

  1. Since JAVA_HOME points to Java 1.7, the javac program (the compiler) from Java 1.7 will be used. 由于JAVA_HOME指向Java 1.7,因此将使用Java 1.7中的javac程序(编译器)。 However, since the source and target are both 1.6 , the command mvn compile will generate classes that are runnable on JRE 1.6 . 但是,由于sourcetarget均为1.6 ,因此命令mvn compile将生成可在JRE 1.6上运行的类。 This would be evident if you saw the major and minor version number associated with the generated class file using the javap utility. 如果您使用javap实用工具看到了与生成的类文件相关联的主要和次要版本号,这将是显而易见的。 For Java 1.8, these values are 52 and 0 , for Java 1.7, they are 51 and 0 and for Java 1.6 they are 50 and 0 . 对于Java 1.8,这些值是52 and 0 ;对于Java 1.7,它们是51 and 0 ;对于Java 1.6,它们是50 and 0 You might wonder why you would ever want a Java 1.7 compiler to generate classes with target = 1.6 . 您可能想知道为什么要Java 1.7编译器生成target = 1.6类。 The reasons are based on the runtime (JRE) that you want to run the classes on. 原因是基于要在其上运行类的运行时(JRE)。 If your compiler and runtime always agree on the version, you may not use these, but these upgrades need to be coordinated in large teams and in the meanwhile, you should always try to remain as close to the latest versions of the software (one major reason: you want to get the bug fixes). 如果您的编译器和运行时始终在版本上达成共识,则可以不使用这些版本,但是需要在大型团队中协调这些升级,与此同时,您应始终尽量保持与软件的最新版本(一个主要版本)保持接近。原因:您想获取错误修复程序)。 The other thing to keep in mind is that source release 1.n requires target release 1.n . 要记住的另一件事是source release 1.n requires target release 1.n

  2. Yes, change those values <source> and <target> to 1.8 (there's perhaps a shortcut, but let's do that later ;)). 是的,将这些值<source><target>更改为1.8 (也许有一个快捷方式,但我们以后再做吧;)。 Make sure that JAVA_HOME (and PATH) points to JDK 1.8, since the maven-compiler-plugin ultimately delegates to the javac program in the PATH with -source and -target arguments that come from the plugin's <configuration> . 确保JAVA_HOME(和PATH)指向JDK 1.8,因为maven-compiler-plugin最终使用来自插件的<configuration> -source-target参数委托给PATH中的javac程序。 See the output of mvn -X compile , you will get something like: 查看mvn -X compile的输出,您将得到类似以下内容的信息:


[DEBUG]   (f) source = 1.8 
[DEBUG]   (f) staleMillis = 0 
[DEBUG]   (f) target = 1.8 
[DEBUG]   (f) useIncrementalCompilation = true 
[DEBUG]   (f) verbose = false 
[DEBUG] -- end configuration -- 
[DEBUG] Using compiler 'javac'.

Beware the following warning from Maven docs : 当心来自Maven docs的以下警告:

Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. 仅设置target选项不能保证您的代码实际在具有指定版本的JRE上运行。 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. 陷阱是仅在更高版本的JRE中存在的API的意外使用,这将使您的代码在运行时因链接错误而失败。 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. 为避免此问题,您可以将编译器的引导类路径配置为与目标JRE匹配,或使用Animal Sniffer Maven插件来验证您的代码未使用意外的API。

Maven uses the JDK which is been set in JAVA_HOME. Maven使用在JAVA_HOME中设置的JDK。 You can set in mvn.bat to look for a specific JDK location. 您可以在mvn.bat中设置以查找特定的JDK位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM