1. 程式人生 > >Maven:Could not resolve archetype

Maven:Could not resolve archetype

在建立maven-archetype-webapp專案時,總是丟擲以下異常:

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 http://maven.oschina.net/content/groups/public/ was cached in the local repository, resolution will not be reattempted until the update interval of maven-net-cn has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0 from/to maven-net-cn (http://maven.oschina.net/content/groups/public/): Connection refused: connect
Failure to transfer org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0 from http://maven.oschina.net/content/groups/public/ was cached in the local repository, resolution will not be reattempted until the update interval of maven-net-cn has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven.archetypes:maven-archetype-webapp:pom:1.0 from/to maven-net-cn (http://maven.oschina.net/content/groups/public/): Connection refused: connect

此問題在網上有很多文章說已經解決,但試試,一開始都不行,經常不但的嘗試,分析,終於解決。現將解決過程描述如下:

首先從錯誤資訊說起:

錯誤資訊是說,不能從遠端下載maven-archetype-webapp:pom:1.0。

可為什麼要下載這個呢。根據Maven官方對archetype的定義如下:

In short, Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made

.

可以知道,archetype是Maven各專案的配置模板。在建立專案時,會根據建立的專案型別,下載相應的模板配置資訊。

archetype檔案為各種專案型別,也就是下圖所看到的型別。以及各種型別的專案模板的遠端地址。建立專案時,會從配置的地址下載配置資料。

如果沒有相應的專案型別,到網上下載,再配置到Maven中。我下載的檔案是地址為:

http://maven.oschina.net/content/groups/public/archetype-catalog.xml

此檔案中有相當多的專案型別,我平時也用兩種,所以我其它的刪除了變成了以下的內容:

<?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"
    xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <archetypes>
    
	<archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-webapp</artifactId>
      <version>1.0-alpha-4</version>
      <repository>http://maven.oschina.net/content/groups/public</repository>
    </archetype>
	
	<archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-quickstart</artifactId>
      <version>1.0-alpha-4</version>
      <repository>http://maven.oschina.net/content/groups/public</repository>
    </archetype>
	
  </archetypes>
</archetype-catalog>

在Eclipse中 Window -> Preferences -> Maven -> ArcheTypes : Add Local Catalog  在彈出的對話方塊中選擇下載的檔案即可。建議重啟一下Eclipse。

在建立專案時,選擇相應的專案,會看到下載配置資訊的過程。

我用網上其它方法不能解決的原因:

1. 有的提供的archetype檔案下載地址,下載的檔案中沒有專案的遠端地址資訊,當然也就不可能下載專案配置模板資料。

2. 就是我所使用的網路較差,連線不到遠端伺服器,導致失敗(因為我在下載archetype檔案時都用了好長時間,有時還下載不了)