《Maven官方文檔》創建Archetype 原文鏈接
創建archetype是一個非常簡單的過程。archetype就是一個非常簡單的構件,它包含了你想創建的工程的模型。archetype由這些東西組成:
- 一個archetype描述符(archetype descriptor)(src/main/resources/META-INF/maven目錄下的archetype.xml)。這個文件列出了包含在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的根目錄下)
創建原型需要以下幾個步驟:
1. 為archetype構件創建一個新工程和pom.xml
一個archetype構件的pom.xml示例看起來像這樣:
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://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。這三個參數在後麵從命令行調用archetype:generate是會用到。
2. 創建archetype描述符
archetype描述符是一個名為archetype.xml的文件,這個文件必須放在src/main/resources/META-INF/maven/ 目錄下。在quickstart archetype中你可以找到一個archetype描述符的例子:
<archetype xmlns="https://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0 https://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> <id>標簽應該和archetype的pom.xml中的artifactId一樣。
<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 `-- src `-- main `-- resources |-- META-INF | `-- maven | `--archetype.xml `-- archetype-resources |-- pom.xml `-- src |-- main | `-- java | `-- App.java `-- test `-- java `-- AppTest.java
3. 創建原型文件和原型pom.xml
下一個要創建的archetype組件是原型pom.xml。每一個pom.xml都要做的,就是不要忘記設置artifactId和groupId作為變量 ( ${artifactId}/ ${groupId} )。這兩個變量都將在archetype:generate從命令行運行時被初始化。
一個原型pom.xml的例子如下:
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://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>https://www.myorganization.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
4. 安裝archetype並運行archetype插件
現在你可以準備安裝這個archetype:
mvn install
現在你已經創建了一個archetype,你可以試著在你的本地係統上使用下麵的命令。使用這個命令,你需要製定你想要使用的archetype的全部信息(groupId, artifactId, version)和你想要創建的新工程的信息(artifactId和groupId)。別放了你的archetype的版本(如果你沒有包含版本信息,你的archetype創建操作可能會得到一個關於版本的失敗信息:RELEASE was not found)
mvn archetype:generate \ -DarchetypeGroupId=<archetype-groupId> \ -DarchetypeArtifactId=<archetype-artifactId> \ -DarchetypeVersion=<archetype-version> \ -DgroupId=<my.groupid> \ -DartifactId=<my-artifactId>
如果你覺得你的archetype很好用,你可以將它和其他構件一起部署(提交到ibiblio),這樣這個archetype就能被其他Maven用戶使用了。
創建Archetype的其他方式
作為手工創建archetype目錄結構的替代方案,你可以簡單的這樣做
mvn archetype:generate -DgroupId=[your project's group id] -DartifactId=[your project's artifact id] -DarchetypeArtifactId=maven-archetype-archetype
這樣的話,你現在可以自定義archetype.xml和archetype-resources目錄的內容,接著,繼續第四步驟(安裝archetype並運行archetype插件)。
最後更新:2017-05-19 15:32:25