天天看點

《Maven官方文檔》建立Archetype 原文連結

建立archetype是一個非常簡單的過程。archetype就是一個非常簡單的構件,它包含了你想建立的工程的模型。archetype由這些東西組成:

原型檔案(prototype files),archetype插件會複制這些檔案(目錄: src/main/resources/archetype-resources/)

原型pom(prototype pom)(pom.xml in: src/main/resources/archetype-resources)

archetype的pom(pom.xml 在archetype的根目錄下)

建立原型需要以下幾個步驟:

一個archetype構件的pom.xml示例看起來像這樣:

你需要修改的僅僅是groupid, artifactid 和 version。這三個參數在後面從指令行調用archetype:generate是會用到。

archetype描述符是一個名為archetype.xml的檔案,這個檔案必須放在src/main/resources/meta-inf/maven/ 目錄下。在quickstart archetype中你可以找到一個archetype描述符的例子:

<allowpartial>true</allowpartial>标簽是可選的,它使得archetype:generate可以在一個已存在的工程中運作。

<sources>, <resources>, <testsources>, <testresources> 和 <siteresources>标簽則代表工程中不同的部分:

<sources> = src/main/java

<resources> = src/main/resources

<testsources> = src/test/java

<testresources> = src/test/resources

<siteresources> = src/site

<sources> 和<testsources>都能包含<source>元素來指定源檔案。

<testresources>和<siteresources>能包含<resource>元素來指定資源檔案。

将其他的資源,比如src/main/webapp目錄放在<resource>标簽中。

在這一點上,你可以指定要建立的單獨的檔案,但不能是空目錄。

是以,上面展示的quickstart archetype 定義的目錄結構為:

下一個要建立的archetype元件是原型pom.xml。每一個pom.xml都要做的,就是不要忘記設定artifactid和groupid作為變量 ( ${artifactid}/ ${groupid} )。這兩個變量都将在archetype:generate從指令行運作時被初始化。

一個原型pom.xml的例子如下:

現在你可以準備安裝這個archetype:

現在你已經建立了一個archetype,你可以試着在你的本地系統上使用下面的指令。使用這個指令,你需要制定你想要使用的archetype的全部資訊(groupid, artifactid, version)和你想要建立的新工程的資訊(artifactid和groupid)。别放了你的archetype的版本(如果你沒有包含版本資訊,你的archetype建立操作可能會得到一個關于版本的失敗資訊:release was not found)

如果你覺得你的archetype很好用,你可以将它和其他構件一起部署(送出到ibiblio),這樣這個archetype就能被其他maven使用者使用了。

作為手工建立archetype目錄結構的替代方案,你可以簡單的這樣做

這樣的話,你現在可以自定義archetype.xml和archetype-resources目錄的内容,接着,繼續第四步驟(安裝archetype并運作archetype插件)。