简体   繁体   English

使用不带maven-compiler-plugin的Java 1.7

[英]Using Java 1.7 without maven-compiler-plugin

Is there any way I can force maven to compile using java 1.7 standard without using the maven-compiler-plugin? 有什么方法可以强制不使用maven-compiler-plugin而使用Java 1.7标准进行Maven编译? I have read about the JAVA_HOME being not set properly, but I don't think this is the case. 我读过有关JAVA_HOME设置不正确的信息,但我认为情况并非如此。 I have added the following line in the mvn script: 我在mvn脚本中添加了以下行:

echo $JAVA_HOME

Below is the output of mvn --version 以下是mvn --version的输出

Apache Maven 3.0.4 Apache Maven 3.0.4

Maven home: /usr/share/maven Maven主页:/ usr / share / maven

Java version: 1.7.0_45, vendor: Oracle Corporation Java版本:1.7.0_45,供应商:Oracle Corporation

Java home: /usr/lib/jvm/java-7-oracle/jre Java主页:/ usr / lib / jvm / java-7-oracle / jre

Default locale: en_US, platform encoding: UTF-8 默认语言环境:en_US,平台编码:UTF-8

OS name: "linux", version: "3.11.0-12-generic", arch: "i386", family: "unix" 操作系统名称:“ linux”,版本:“ 3.11.0-12-generic”,拱门:“ i386”,家族:“ unix”

One problem would be that $JAVA_HOME is set to /usr/lib/jvn/java-7-oracle while manven reports it to be $JAVA_HOME/jre. 一个问题是$ JAVA_HOME设置为/ usr / lib / jvn / java-7-oracle,而manven报告将其设置为$ JAVA_HOME / jre。

The question is is there a way to configure maven tu use java 1.7 (similar to javac -target 1.7) ? 问题是是否有一种方法可以使用Java 1.7配置Maven Tu(类似于javac -target 1.7)?

The question is is there a way to configure maven tu use java 1.7 (similar to javac -target 1.7) ? 问题是是否有一种方法可以使用Java 1.7配置Maven Tu(类似于javac -target 1.7)?

There is a way, and that is by configuring maven-compiler-plugin. 有一种方法,那就是配置maven-compiler-plugin。 That is what Maven uses for compiling, so that's what you need to configure. 那就是Maven用来编译的东西,所以这就是您需要配置的东西。

If you insist on using javac, see what maven compiler plugin manual says : 如果您坚持使用Javac,请参阅《 Maven编译器插件手册》中的内容

Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. 从3.0开始,默认的编译器是javax.tools.JavaCompiler(如果使用的是Java 1.6),并且用于编译Java源代码。 If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse. 如果要使用javac强制使用插件,则必须配置插件选项forceJavacCompilerUse。

but even in that case it would be also about configuring the maven-compiler-plugin. 但是即使在那种情况下,它也将与配置maven-compiler-plugin有关。

You may configure the compiler plugin by passing appropriate -D... arguments when invoking maven. 您可以在调用maven时通过传递适当的-D...参数来配置编译器插件。 In your case this might be 在您的情况下,这可能是

mvn -Dmaven.compiler.target=1.7 compile

But i'd consider this an antipattern, as this makes your build dependent on additional parameters which must be documented. 但是我认为这是一种反模式,因为这会使您的构建依赖于必须记录的其他参数。 It is way better to define a proper configuration in some parent pom. 最好在某些父pom中定义适当的配置。

See compiler:compile for complete description of the maven-compiler-plugin 有关maven-compiler-plugin的完整说明,请参见compile:compile

Based on http://maven.apache.org/plugins/maven-compiler-plugin/testCompile-mojo.html , it looks like you can set these properties can be set without "poluting" your pom.xml file by setting in the command line the properties maven.compiler.target and maven.compiler.source. 基于http://maven.apache.org/plugins/maven-compiler-plugin/testCompile-mojo.html ,您似乎可以设置这些属性,而无需通过设置“命令行中的属性maven.compiler.target和maven.compiler.source。

So, a command like below should solve your problem. 因此,如下所示的命令应该可以解决您的问题。 mvn install -Dmaven.compiler.target=1.7 -Dmaven.compiler.source=1.7 mvn install -Dmaven.compiler.target = 1.7 -Dmaven.compiler.source = 1.7

The default version for these atributes is 1.5. 这些属性的默认版本为1.5。 You can also modify the mvn script and add in the exec line these two atributes. 您还可以修改mvn脚本并在exec行中添加这两个属性。

暂无
暂无

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

相关问题 java maven-compiler-plugin:3.1,javacTask:源版本1.7需要目标版本1.7 - java maven-compiler-plugin:3.1, javacTask: source release 1.7 requires target release 1.7 Maven Java 构建编译时出错:maven-compiler-plugin - Maven Java error compiling on build: maven-compiler-plugin 找不到 maven 编译器插件 - maven-compiler-plugin not found 使用带有java 1.8和netbeans的maven-compiler-plugin 2.5.1在Java中进行注释的编译错误 - Compilation error for annotations in Java using maven-compiler-plugin 2.5.1 with java 1.8 and netbeans 使用 maven-compiler-plugin 从编译中排除 src/main/java - exclude src/main/java from compiling with maven-compiler-plugin java,为什么会出现这些错误? --maven-编译器插件:3.7.0 - java, why these error? --maven-compiler-plugin:3.7.0 无法执行maven-compiler-plugin:3.6.1:使用java 9时的testCompile - Failed to execute maven-compiler-plugin:3.6.1:testCompile when using java 9 无法使用Java高于1.8的maven-compiler-plugin进行编译 - Impossible to compile using maven-compiler-plugin with Java higher than 1.8 Java_home在mvn版本和pom.xml中为1.7,但出现错误“无法执行目标org.apache.maven.plugins:maven-compiler-plugin” - Java_home is 1.7 in mvn version, and in pom.xml, but getting error “Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin” Maven-无法使用Java 10执行目标org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile - Maven - Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile Using java 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM