1. 程式人生 > >《Maven官方文件》建立Archetype

《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="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。這三個引數在後面從命令列呼叫archetype:generate是會用到。

2. 建立archetype描述符

archetype描述符是一個名為archetype.xml的檔案,這個檔案必須放在src/main/resources/META-INF/maven/ 目錄下。在quickstart archetype中你可以找到一個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>

<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="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. 安裝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外掛)。