背景:本项目使用JDK1.7
编译maven工程的时候出现如下错误:
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile)
pom中如下配置maven插件,配置中声明使用JDK1.7:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable>${JAVA_HOME}/bin/javac</executable>
</configuration>
</plugin>
</plugins>
</build>
上面JAV A_HOME在%maven安装目录%/conf/settings.xml中profiles节点下新增如下;
<profile>
<id>custom-compiler</id>
<properties>
<JAVA_HOME>C:\Program Files (x86)\Java\jdk1.7.0_25</JAVA_HOME>
</properties>
</profile>
配置后还要激活上面的配置,在settings节点下配置。
<activeProfiles>
<activeProfile>custom-compiler</activeProfile>
</activeProfiles>
maven其实是有一个默认的仓库.m2仓库和默认的settings.xml配置文件,我们在这个默认的settings.xml文件中也添加了一个JAVA_HOME的变量后,编译就通过了,这就说明,maven编译的时候找的不是我在idea中配置的我自定义的settings.xml,而是先找的它默认的那个。因为里面没有,所以之前找不到JAVA_HOME,导致编译失败、
总结:maven编译的时候应该是先找的默认的settings.xml,如果找不到,才会去找我在idea的settings选项下配置的“User settings file”中配置的settings.xml文件。
解决办法:为maven默认的去找的那个settings.xml文件配置JAVA_HOME,这样自定义的文件就会生效了
参考:
Maven构建失败
针对Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1的解决方案