解決使用eclipse創建maven web項目時報Could not resolve archetype的問題
前兩天重裝了系統,今天想寫一個項目的時候出現了點問題。
在使用eclipse創建maven web項目時,點Finish後報了Could not resolve archetype的問題。
Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:1.0 from any of the configured repositories.
Could not resolve artifact org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0
Failure to transfer org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0 from/to central (https://repo.maven.apache.org/maven2): repo.maven.apache.org
Failure to transfer org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0 from/to central (https://repo.maven.apache.org/maven2): repo.maven.apache.org
網上比較流行的解決方法有兩種:
1.在Eclipse Maven配置中添加新的Catalog配置
2.在本地庫中裝載maven-archetype-quickstart
我先用了方法1,配置後沒有任何效果。再用方法2,配置完後試著創建maven-archetype-quickstart類型的項目創建成功,但maven web項目依然報錯。
然後我就想到了分析下報錯內容再去解決問題。
看看報錯的第一句話,大致意思是:無法從任何已經配置的庫裏面解析org.apache.maven.archetypes:maven-archetype-webapp:1.0。
所以第二種辦法行不通的原因是因為缺少webapp這個包,而我們從第二步中安裝的只是maven-archetype-quickstart這個項目包。
再看第二段第一句話:從https://repo.maven.apache.org/maven2下載org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0失敗。
從這裏就能知道了目前的問題是org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0這個包在創建maven web項目時maven找不到,或者是找到了但沒有辦法解析。當找不到或者無法解析時,maven又不能從遠程倉庫下載下來。
因此我的解決方案就出來了:
- 在本地庫中裝載maven-archetype-webapp
- 刪除掉目前本地倉庫中的maven-archetype-webapp這個包然後創建項目,讓maven自動從遠程倉庫獲取該包。
註:該包的路徑是(%你的默認本地倉庫%\repository\org\apache\maven\archetypes\maven-archetype-webapp\1.0)
方法2的思路來自maven下載jar包到本地倉庫時會產生.lastupdate文件而導致該包無法使用。
補充一個不錯的方法:
先在CMD中執行mvn help:system命令行,執行後會下載很多東西,等待下載結束。
命令行執行:mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false?
此時會下載所需要的項目jar包,等待下載完畢就可以創建項目了。
解決使用eclipse創建maven web項目時報Could not resolve archetype的問題