Maven 的 archetype 技術,為新建標准化的工程框架提供了方便。為自定義一套工程框架標准,可參考以下步驟操作:
1,創建一個項目的原型
2,在項目根目錄執行命令:mvn archetype:create-from-project,新生成的archetype在target/generated-sources/archetype目錄
archetype-resources目錄下模版工程的資源元文件,這些元文件是生成工程的時候需要用到,該目錄下必須要有一個頂級pom文件,子文件夾代表了模塊定義。archetype目錄下的pom文件是用來定義骨架groupId,artifactid信息的,用於創建應用的時候。
3,進入target/generated-sources/archetype/src/main/resources 目錄,手工調整相關archetype源碼. 需進行變量替換的文件,需在archetype-metadata.xml中開啟filtered="true";將資源中需要訂制的地方替換成相應的${groupid},${artifactid},${package},這樣maven會在創建項目的過程中自動將這些值傳入的相應要替換的地方
比如:
<groupId>${groupId}</groupId> <artifactId>${artifactId}</artifactId> <version>${version}</version>
那創建項目的時候回自動替換里面的變量,如果創建的文件名里面有變量,那使用__artifactId__這個格式。
archetype-metadata.xml文件中重要的幾個屬性如下:
a.屬性變量定義
<requiredProperties> <requiredProperty key="appName"> <defaultValue>helloworld</defaultValue> </requiredProperty> <requiredProperty key="groupId"> <defaultValue>com.helloworld</defaultValue> </requiredProperty> <requiredProperty key="artifactId"> <defaultValue>helloworld</defaultValue> </requiredProperty> </requiredProperties>
這個不是必填。
b.項目子模塊定義
<module id="${rootArtifactId}-biz" dir="__rootArtifactId__-biz" name="${rootArtifactId}-biz"> </module>
module有三個屬性,解釋如下:
id :相當於工程的artifactId.
dir :相當於工程源文件在archetype-resources里對應的directory.
name :模塊的名字.
c.項目文件集定義
<fileSets> <fileSet encoding="UTF-8"> <directory>assets/css</directory> <includes> <include>**/*.css</include> </includes> </fileSet> </fileSets>
4,在..\target\generated-sources\archetype下有個pom.xml文件,編輯里面的
<groupId>com.***.***.archetype</groupId>
<artifactId>***-archetype</artifactId> <version>*.*</version>
這樣可以發布到自己想要的位置,如果不修改那就放入默認的位置。
5,在archetype根(..\target\generated-sources\archetype)目錄執行:mvn clean install,將該archetype傳到本地的maven倉庫
6,通過mvn archetype:generate -DarchetypeGroupId=***.archetype -DarchetypeArtifactId=***-archetype -DarchetypeVersion=**就可以創建項目了。