本文通過 Maven archetype 來創(chuàng)建自定義的項(xiàng)目結(jié)構(gòu),形成Maven 項(xiàng)目模板,避免重復(fù)的項(xiàng)目創(chuàng)建工作。
Archetypes 創(chuàng)建指南
創(chuàng)建一個(gè) archetype 是一個(gè)非常簡單的過程。 archetype 是一個(gè)簡化版的 artifact,其中包含了你想要?jiǎng)?chuàng)建的項(xiàng)目的原型。 archetype 由以下部分組成:
- archetype 描述器 (archetype.xml 路徑 src/main/resources/META-INF/maven/)。它分類列舉了要包含在 archetype 中的全部文件,確保archetype 生成機(jī)制正常運(yùn)行。
- 通過 archetype 插件要復(fù)制的原型文件(src/main/resources/archetype-resources/)
- 原型 pom (pom.xml 路徑 src/main/resources/archetype-resources)
- Archetype 的 pom 文件(pom.xml 在 archetype 的根目錄)。
創(chuàng)建 archetype 要遵循下面的這些步驟:
1. 為 archetype artifact 創(chuàng)建一個(gè)新項(xiàng)目和 pom.xml
下面是一個(gè) archetype artifact 的 pom.xml 示例:
<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>my.groupId</groupId>
<artifactId>my-archetype-id</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
你需要寫的信息是 groupId, artifactId 和 version。這三個(gè)參數(shù)將在稍后使用(在命令行通過 archetype:generate 調(diào)用 archetype 時(shí)使用)。
2. 創(chuàng)建 archetype 描述器
archetype 描述器 是一個(gè)叫 archetype.xml 的文件,必須在 src/main/resources/META-INF/maven/ 目錄下。 下面是 archetype 描述器的示例:
<archetype xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0 http://maven.apache.org/xsd/archetype-1.0.0.xsd">
<id>quickstart</id>
<sources>
<source>src/main/java/App.java</source>
</sources>
<testSources>
<source>src/test/java/AppTest.java</source>
</testSources>
</archetype>
標(biāo)簽 <id> 必須和 archetype 的 pom.xml 中的 artifactId 一樣。
<allowPartial>true</allowPartial> 表示:可以在已存在的項(xiàng)目上運(yùn)行 archetype:generate 命令。
<sources>, <resources>, <testSources>, <testResources> 和<siteResources> 標(biāo)簽代表項(xiàng)目的不同部分:
- <sources> = src/main/java
- <resources> = src/main/resources
- <testSources> = src/test/java
- <testResources> = src/test/resources
- <siteResources> = src/site
<sources> 和<testSources> 可以包含 <source> 元素來指定一個(gè)源文件。
<testResources> 和<siteResources> 包含 <resource>元素來指定一個(gè)資源文件。
其他資源與 src/main/webapp 目錄配置在 <resources> 標(biāo)簽中一樣處理 。
這里只能指定單個(gè)要?jiǎng)?chuàng)建的文件,不能是空目錄。
下面的目錄結(jié)構(gòu)展示了上面的定義:
archetype
|-- pom.xml
`-- src
`-- main
`-- resources
|-- META-INF
| `-- maven
| `--archetype.xml
`-- archetype-resources
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- App.java
`-- test
`-- java
`-- AppTest.java
3. 創(chuàng)建原型文件和原型 pom.xml
另一個(gè) archetype 的組件是原型 pom.xml。隨便一個(gè) pom.xml 都行,只要?jiǎng)e忘了設(shè)置 artifactId 和 groupId 做變量 (${artifactId} 和 ${groupId})就行。兩個(gè)變量在命令行調(diào)用 archetype:generate 時(shí)將被初始化。
原型 pom.xml 的例子:
<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>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<packaging>jar</packaging>
<name>A custom project</name>
<url>http://www.myorganization.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
4. 安裝并運(yùn)行 archetype 插件
現(xiàn)在你可以安裝 archetype:
mvn install
現(xiàn)在,你已經(jīng)創(chuàng)建好了 archetype,你可以在你本地環(huán)境用下面的命令試試。這個(gè)命令中,你需要指定你要使用的 archetype 的全部信息(包括 groupId,artifactId,version)和你要新建項(xiàng)目的信息(artifactId,groupId)。別忘了包含你的 archetype 的版本(如果你沒有包含版本,你的 archetype 可能會(huì)創(chuàng)建失敗并顯示一條消息:version:RELEASE 未發(fā)現(xiàn))。
mvn archetype:generate \
-DarchetypeGroupId=<archetype-groupId> \
-DarchetypeArtifactId=<archetype-artifactId> \
-DarchetypeVersion=<archetype-version> \
-DgroupId=<my.groupid> \
-DartifactId=<my-artifactId>
如若好運(yùn)執(zhí)行到此,你就可以發(fā)布你的 archetype 到 Maven 上,以便更多的人可以使用這個(gè) archetype。
其他創(chuàng)建 Archetype 的方法
用以下方式可以替代手工創(chuàng)建 archetype 的目錄結(jié)構(gòu):
mvn archetype:generate
-DgroupId=[your project's group id]
-DartifactId=[your project's artifact id]
-DarchetypeArtifactId=maven-archetype-archetype
之后,你可以自定義 archetype-resources 目錄和 archetype.xml 的內(nèi)容,然后執(zhí)行第四步(安裝 archetype 并運(yùn)行 archetype 插件)
官網(wǎng)原文地址:https://maven.apache.org/guides/mini/guide-creating-archetypes.html