背景:本項目使用JDK1.8
編譯maven工程的時候出現如下錯誤:
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1
pom中如下配置maven插件,配置中聲明使用JDK1.8:
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.1</version>
- <configuration>
- <verbose>true</verbose>
- <fork>true</fork>
- <executable>${JAVA8_HOME}/bin/javac</executable>
- </configuration>
- </plugin>
這里的${JAVA8_HOME}這個變量是在settings.xml中配置的,如下:
- <profile>
- <id>custom-compiler</id>
- <properties>
- <JAVA8_HOME>C:\Program Files (x86)\Java\jdk1.8.0_73</JAVA8_HOME>
- </properties>
- </profile>
當然這里應該需要激活,所以settings.xml文件還應該有如下配置:
- <activeProfiles>
- <activeProfile>custom-compiler</activeProfile>
- </activeProfiles>
從pom文件中CTRL點擊變量JAVA8_HOME能跳到settings.xml中找到它的定義處,按理來說應該是能找到這個變量,出現上述問題並不是因為找不到這個變量。我將pom文件中的JAVA8_HOME這個變量直接用實際的路徑替換,即替換為
- C:\Program Files (x86)\Java\jdk1.8.0_73\bin\javac
發現編譯通過,這就奇怪了。
揭曉原因:
maven其實是有一個默認的倉庫.m2倉庫和默認的settings.xml配置文件,我們在這個默認的settings.xml文件中也添加了一個JAVA8_HOME的變量后,編譯就通過了,這就說明,maven編譯的時候找的不是我在idea中配置的我自定義的settings.xml,而是先找的它默認的那個。因為里面沒有,所以之前找不到JAVA8_HOME,導致編譯失敗、
總結:maven編譯的時候應該是先找的默認的settings.xml,如果找不到,才會去找我在idea的settings選項下配置的“User settings file”中配置的settings.xml文件。
解決辦法:刪掉maven默認的去找的那個settings.xml文件,這樣自定義的文件就會生效了