VS Code導入Java項目后,用maven構建編譯項目出錯的解決辦法
一個兩年前的老項目,近期客戶需要使用,時間跨度比較長,操作系統都重新安裝了,項目環境都需要重新配置搭建,當基礎的組件都安裝好后,需要用maven命令進行重新編譯一下,項目是采用maven管理多個子項目模塊的,子項目之間需要互相引用,結果跑打包命令時錯誤不斷,剛開始是出現如下錯誤:
[ERROR] Failed to execute goal on project nky-admin: Could not resolve dependencies for project com.yurongbj:nky-admin:jar:1.0.0: Failed to collect dependencies at com.yurongbj:yurongbj-framework:jar:4.6.1: Failed to read artifact descriptor for com.yurongbj:yurongbj-framework:jar:4.6.1: Could not transfer artifact com.yurongbj:yurongbj-framework:pom:4.6.1 from/to maven-default-http-blocker (http://0.0.0.0/): Blocked mirror for repositories: [public (http://maven.aliyun.com/nexus/content/groups/public/, default, releases+snapshots)] -> [Help 1]
這個錯誤是由于maven沒有配置阿里云的maven鏡像引起的,需要在maven安裝目錄下找到setting.xml配置文件,在文件中
mirrors節點下添加如下鏡像配置節點
<mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> </mirror> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>
重新再跑maven的打包命令,結果又出現如下錯誤:
[ERROR] Failed to execute goal on project nky-admin: Could not resolve dependencies for project com.yurongbj:nky-admin:jar:1.0.0: The following artifacts could not be resolved: com.yurongbj:yurongbj-framework:jar:4.6.1, com.yurongbj:yurongbj-quartz:jar:4.6.1, com.yurongbj:yurongbj-generator:jar:4.6.1: com.yurongbj:yurongbj-framework:jar:4.6.1 was not found in http://maven.aliyun.com/nexus/content/groups/public/ during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of public has elapsed or updates are forced -> [Help 1]
這個問題是由于maven的本地倉庫沒有配置好,繼續打開setting.xml配置文件,在settings節點下添加如下本地倉庫配置即可:
<localRepository>D:\mavenRepository</localRepository> <!--本地倉庫路徑視自己本地存放目錄而定-->
以上兩個配置項都做好后,重新跑maven的打包命令就能正常了。如果依然有錯誤,就按照提示將子項目模塊一個一個進行編譯,先運行maven的clean命令,然后執行maven的install命令,子項目沒報錯后,再執行總項目下的maven打包命令。