操作背景:java version “1.7.0_51”;Spring CLI v1.3.0.RELEASE;Apache Maven 3.3.9。
使用https://start.spring.io/构建的springboot项目,想直接使用mvn spring-boot:run命令来启动测试一下。但是遇到了问题。
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project readinglist: Fatal error compiling: 无效的标记: -parameters -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
通过一番排查,发现是JDK问题。详细官方文档描述可以查看http://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html。
定位到我自己的问题是因为https://start.spring.io/构建项目时,可选择的JDK最低版本是JAVA8,而我自己电脑安装的是JAVA7。导致项目和计算机配置不匹配。
解决办法一(把项目指定为java7版本):
在项目的pom.xml文件中,找到
<java.version>1.8</java.version>
把其修改为
<java.version>1.7</java.version>
即可操作compile插件成功。如果pom.xml中没有配置,可以添加用来指定java7版本。
解决办法二(配置maven-compiler-plugin来指定使用java8的javac,需要你安装了java8):
在pom.xml文件中,添加如下配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<!--表示执行的时候使用配置的JAVA8_HOME路径的bin下的javac-->
<executable>${JAVA8_HOME}/bin/javac</executable>
</configuration>
</plugin>
</plugins>
</build>
因此需要在settings.xml中配置JAVA8_HOME:
<profile>
<id>custom1-compiler</id>
<properties>
<JAVA8_HOME>C:\Program Files\Java\jdk1.8.0_231</JAVA8_HOME>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>custom1-compiler</activeProfile>
</activeProfiles>
表示配置一个id为custom1-compiler的profile,该profile的配置为JAVA8_HOME,值为C:\Program Files\Java\jdk1.8.0_231;然后使用activeProfiles来激活该profile以供系统使用。
ps:如果需要操作java7/8共享的环境变量,可参考我的博文解决java7/8共存问题
特别需要注意的是:
虽然解决了compiler的问题,但是因为所使用的软件版本问题,compiler完成后立马遇到了别的问题,如果想了解,请阅读的我以下博文。
springboot学习:1.安装
springboot学习:3.第一个Spring Boot Application