maven自定義骨架
1 maven自定義archtype骨架
Maven的 archetype技術,為新建標準化的工程框架提供了方便。為自定義一套工程框架標準,可參考以下步驟操作:
1.1 建立一個專案的原型
專案根目錄:project-name
1.2 建立專案骨架
在專案根目錄project-name執行命令:mvn archetype:create-from-project,新生成的archetype在project-name /target/generated-sources/archetype目錄
project-name/target/generated-sources/archetype/src/main/resources/archetype-resources目錄下模版工程的資源元檔案,這些元檔案是生成工程的時候需要用到,該目錄下必須要有一個頂級pom檔案,子資料夾代表了模組定義。archetype
1.3 手工調整相關archetype原始碼
1.3.1 檔案的變數替換
進入project-name/target/generated-sources/archetype/src/main/resources 目錄,手工調整相關archetype原始碼.
需進行變數替換的檔案,需在project-name/target/generated-sources/archetype/src/main/resources/META-INF/maven/archetype-metadata.xml中開啟filtered="true";
檔案內容替換
將資源中需要訂製的地方替換成相應的${groupid},${artifactid},${package},這樣maven會在建立專案的過程中自動將這些值傳入的相應要替換的地方
比如:
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
資料夾替換
那建立專案的時候回自動替換裡面的變數,如果建立的檔名裡面有變數,那使用__artifactId__這個格式。
1.3.2 archetype-metadata.xml詳解
archetype-metadata.xml檔案中重要的幾個屬性如下:
1.3.2.1 屬性變數定義
<requiredProperties>
<requiredPropertykey="appName">
<defaultValue>helloworld</defaultValue>
</requiredProperty>
<requiredPropertykey="groupId">
<defaultValue>com.helloworld</defaultValue>
</requiredProperty>
<requiredPropertykey="artifactId">
<defaultValue>helloworld</defaultValue>
</requiredProperty>
</requiredProperties>
這個不是必填。
1.3.2.2 專案子模組定義
<module id="${artifactId}-biz"dir="__artifactId__-biz" name="${artifactId}-biz"></module>
module有三個屬性,解釋如下:
id:相當於工程的artifactId.
dir:相當於工程原始檔在archetype-resources裡對應的directory.
name:模組的名字.
1.3.2.3 專案檔案集定義
<fileSets>
<fileSet encoding="UTF-8">
<directory>assets/css</directory>
<includes>
<include>**/*.css</include>
</includes>
</fileSet>
</fileSets>
1.4 編輯專案骨架釋出位置
在project-name/target/generated-sources/archetype下有個pom.xml檔案,編輯裡面的
<groupId>com.***.***.archetype</groupId>
<artifactId>***-archetype</artifactId>
<version>*.*</version>
這樣可以釋出到自己想要的位置,如果不修改那就放入預設的位置。
1.5 安裝專案骨架
在archetype根目錄(project-name/target/generated-sources/archetype)執行:
mvn clean install
將該archetype傳到本地的maven倉庫
1.6 建立骨架專案
建立骨架專案如下:
mvn archetype:generate-DarchetypeGroupId=***.archetype-DarchetypeArtifactId=***-archetype-DarchetypeVersion=**
1.7 檢視專案骨架
mvn archetype:generate-DarchetypeCatalog=local
1.8 刪除專案骨架
把localRepository目錄({M2_HOME}/repository)下面的archetype-catalog.xml檔案中對應的<archetype>位元組段刪掉,
把本地倉庫中相應groupId和artifactId下面的檔案刪掉就可以了。
1.9 釋出專案骨架
mvn clean deploy