生成archetype
最簡單的方式是從已有的工程中逆向生成archetype,步驟如下:
① 切換到工程的根路徑:
cd pangu2
② 運行mvn命令
mvn archetype:create-from-project -Darchetype.filteredExtensions=java
-Darchetype.filteredExtensions參數指明哪些類型的文件要在模板中忽略掉
③ 去target目錄找到生成的archetype
archetype文件夾是一個maven 工程,pom.xml內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
? <modelVersion>4.0.0</modelVersion>
? <groupId>com.xxx</groupId>
? <artifactId>pangu-archetype</artifactId>
? <version>1.0.0-SNAPSHOT</version>
? <packaging>maven-archetype</packaging>
? <name>pangu-archetype</name>
? <build>
? ? <extensions>
? ? ? <extension>
? ? ? ? <groupId>org.apache.maven.archetype</groupId>
? ? ? ? <artifactId>archetype-packaging</artifactId>
? ? ? ? <version>3.0.1</version>
? ? ? </extension>
? ? </extensions>
? ? <pluginManagement>
? ? ? <plugins>
? ? ? ? <plugin>
? ? ? ? ? <artifactId>maven-archetype-plugin</artifactId>
? ? ? ? ? <version>3.0.1</version>
? ? ? ? </plugin>
? ? ? </plugins>
? ? </pluginManagement>
? </build>
</project>
artifactId取的是源工程的artifactId+archetype
④ 根據需要手動刪除一些文件,改變一些配置項
⑤ 安裝artchetype到本地maven 倉庫
$ mvn install
⑥ 使用archetype
$ cd /tmp/archetype/
$ mvn archetype:generate -DarchetypeCatalog=local
archetype工程說明
以pangu-archetype-standard為例
??pangu-archetype-standard?git:(master)?tree
.
├── pangu-archetype.iml
├── pom.xml//archetype工程本身的pom.xml
├── src
│?? └── main
│?? ? ? └── resources
│?? ? ? ? ? ├── META-INF
│?? ? ? ? ? │?? └── maven
│?? ? ? ? ? │?? ? ? └── archetype-metadata.xml?// archetype元數據
│?? ? ? ? ? └── archetype-resources//archetype模板文件夾
│?? ? ? ? ? ? ? ├── __rootArtifactId__-facade
│?? ? ? ? ? ? ? │?? ├── __rootArtifactId__-facade-sub
│?? ? ? ? ? ? ? │?? │?? └── pom.xml
│?? ? ? ? ? ? ? │?? └── pom.xml
│?? ? ? ? ? ? ? ├── __rootArtifactId__-service
│?? ? ? ? ? ? ? │?? ├── __rootArtifactId__-service-sub
│?? ? ? ? ? ? ? │?? │?? └── pom.xml
│?? ? ? ? ? ? ? │?? └── pom.xml
│?? ? ? ? ? ? ? ├── __rootArtifactId__-web
│?? ? ? ? ? ? ? │?? ├── __rootArtifactId__-web-sub
│?? ? ? ? ? ? ? │?? │?? └── pom.xml
│?? ? ? ? ? ? ? │?? └── pom.xml
│?? ? ? ? ? ? ? ├── bin
│?? ? ? ? ? ? ? │?? └── a.sh
│?? ? ? ? ? ? ? ├── filter
│?? ? ? ? ? ? ? │?? └── a.property
│?? ? ? ? ? ? ? └── pom.xml
注意目錄結構中包含 __rootArtifactId__ 的文件夾,在生成工程的時候,文件夾名 __rootArtifactId__ 部分會被重命名為傳入的artifactId.
archetype-metadata.xml說明
archetype-metadata.xml主要包含三類信息
生成工程需要的配置屬性
工程包含的文件列表
工程包含的模塊信息
一個簡單的archetype-metadata.xml如下所示:
src/main/java
**/*.java
使用以上archetype-metadata定義,用archetype:create生成工程時,會拷貝archetype-resources文件夾下src/mian/java所有的java類型的文件,使用Velocity模板引擎對java文件進行模板替換,可以被替換的文本類似以下形式:
? ? <groupId>${groupId}</groupId>
? ? <artifactId>${rootArtifactId}</artifactId>
? ? <version>${version}</version>
使用archetype:create生成工程時,會默認詢問輸入
groupId,
artifactId,
version,
package
四個屬性值
如果生成工程需要更多的屬性值,可以在
<requiredProperties>標簽中定義,如下所示
default-value
...
可以在archetype-metadata.xml嵌套定義模塊,類似以下
需要注意的是,嵌套的module所在的目錄dir是一個相對路徑,相對于父module路徑。