api重復引用導致的詭異問題排查
最近一個項目上線前開發環境、測試環境都能正常打包並運行。然而到了准生產環境和生產環境則報一些詭異的錯誤信息:
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project xxx: Compilation failure: Compilation failure:
[ERROR] /data/release/xxx/xxx/src/main/java/com/xxx/testmodel/util/ZipRsaCiph.java:[19,15] BASE64Decoder is internal proprietary API and may be removed in a future release
[ERROR] /data/release/xxx/xxx/src/main/java/com/xxx/testmodel/util/ZipRsaCiph.java:[19,15] BASE64Decoder is internal proprietary API and may be removed in a future release
[ERROR] /data/release/xxx/xxx/src/main/java/com/xxx/testmodel/util/ZipRsaCiph.java:[19,15] BASE64Decoder is internal proprietary API and may be removed in a future release
[ERROR] /data/release/xxx/xxx/src/main/java/com/xxx/testmodel/util/ZipRsaCiph.java:[19,15] BASE64Decoder is internal proprietary API and may be removed in a future release
[ERROR] /data/release/xxx/xxx/src/main/java/com/xxx/testmodel/util/ZipRsaCiph.java:[64,9] BASE64Decoder is internal proprietary API and may be removed in a future release
[ERROR] /data/release/xxx/xxx/src/main/java/com/xxx/testmodel/aop/Author.java:[95,90] error: cannot find symbol
[ERROR] -> [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/MojoFailureException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
咋一看莫名其妙,代碼怎么找也找不到問題所在。查看代碼修改部分,發現是重新引入了一個新的api。
通過mvn dependency:tree
發現,同時引用了 test-api:jar:2.0.0 和 test-api:jar:3.12.0 兩個版本的api。
而通過mvn dependency:analyze
查看的結果為:
[WARNING] Used undeclared dependencies found:
[WARNING] com.xxx.auth:xxx-api:jar:2.0.0:compile
[WARNING] Unused declared dependencies found:
[WARNING] com.xxx.auth:xxx-api:jar:3.12.0:compile
發現並沒有引用新的api。
Used undeclared dependencies
:指那些在項目中直接使用到的,但沒有在POM中配置的依賴。
Unused declared dependencies
:供我們參考,這表示那些我們配置了,但並未直接使用的依賴。
后來分析發現是有個第三方 api 引用了相同的api,從而導致這一詭異問題的出現。因而在引用api前可通過:
mvn dependency:tree
和 mvn dependency:analyze
看是否存在重復引用的api,如果存在重復引用的情況可將不需要的版本api排除掉即可。
<dependency>
<groupId>xxx</groupId>
<artifactId>xxx-api</artifactId>
<version>1.0</version>
<exclusions>
<exclusion>
<groupId>test</groupId>
<artifactId>test-api</artifactId>
</exclusion>
</exclusions>
</dependency>