十四、archetype

本章是《maven實戰(zhàn)》最后一章啦,十四章走來,每天都在堅持,同時也簡單的記錄了自己每一天的點點滴滴,感謝大家支持,我會繼續(xù)走下去,希望能在今后給大家?guī)砀泳实墓适?br> 接下來我準(zhǔn)備向《高性能MySQL》進(jìn)軍啦,可能過程中筆記更新會不定期,但是我還是會按照章節(jié)的方式記錄每一章重點并呈現(xiàn)給大家,所以前方路途雖遠(yuǎn),但堅持就是勝利,一起加油吧?。。?/p>

forgive.jpg

可以把archetype當(dāng)做一個模板,當(dāng)我們要創(chuàng)建一些項目的時候,可以通過簡單的maven命令就可以快速生成項目的骨架

使用archetype的一般步驟

交互式

mvn archetype:generate,mvn會列出一個列表供用戶選擇

archetype項目列表.png

這個列表來自archetype-catalog.xml文件,在后續(xù)操作中,用戶需要提供一些通用的基本參數(shù),主要有groupId,artifactId,version,package,這樣archetype插件就可以生成項目的骨架了

批處理方式

批處理方式不同在于使用上述命令時直接把參數(shù)給出來,同時使用-B參數(shù)要求archetype插件以批處理的方式運行,不過需要用戶顯示給出archetype的坐標(biāo)信息

mvn archetype:generate -B \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.0 \
-DgroupId=com.zheng.mavenstudy\
-DartifactId=archetype-test \
-Dversion=1.0-SNAPSHOT \
-Dpackage=com.zheng.mavenstudy

常用的archetype介紹

maven-archetype-quickstart默認(rèn)值 常用于一般javase項目結(jié)構(gòu)
maven-archetype-webapp 用于web項目架構(gòu)
appfuse archetype
appfuse是一個集成了很多開源工具的項目,在于幫助程序員快速高效的創(chuàng)建項目,它提供了大量archetype,方便用戶使用各種類型的項目

編寫自己的archetype項目

一個archetype-maven項目需要包含以下幾個重要部分
pom.xml archetype自身的pom
src/main/resources/archetype-resources/pom.xml 基于該archetype生成的項目pom原型
src/main/resources/META-INF/maven/archetype-metadata.xml archetype的描述符文件
src/main/resources/archetype-resources/ 其他需要包含在archetype項目中的內(nèi)容
基本結(jié)構(gòu)如下

自定義archetype項目結(jié)構(gòu).png

archetypepom.xml文件,用于定義archetype項目的坐標(biāo)
archetype-test/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>com.zheng.archetypestudy</groupId>
  <artifactId>archetype-test</artifactId>
  <version>1.0-SNAPSHOT</version>
</project>

生成的項目pom.xml
用于描述生成項目中包含的一些配置,包括參數(shù)、依賴等,其中groupId,artifactId,version使用了屬性替換
文件位于archetype-test/src/main/resources/archetype-resources/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>${artifactId}</name>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

archetype-metadata.xml文件,用于描述archetype項目哪些文件夾及目錄需要被引入到項目中
位于
archetype-test/src/main/resources/META-INF/maven/archetype-metadata.xml
其中通過fileSets設(shè)置了需要被用于項目中的文件,通過filtered,packaged分別設(shè)置指定目錄下包含的文件是否需要屬性值替換,同時對應(yīng)的目錄是否需要生成包目錄

<?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"
                   xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
    <fileSets>
        <fileSet filtered="true" packaged="true">
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.java</include>
            </includes>
        </fileSet>
        <fileSet filtered="true" packaged="true">
            <directory>src/test/java</directory>
            <includes>
                <include>**/*.java</include>
            </includes>
        </fileSet>
        <fileSet filtered="true" packaged="false">
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </fileSet>
    </fileSets>
    <requiredProperties>
        <requiredProperty key="groupId">
            <defaultValue>com.zheng.archetypestudy</defaultValue>
        </requiredProperty>
    </requiredProperties>
</archetype-catalog>

上面配置的src/main/java,src/test/java,src/main/resources,分別指向目標(biāo)項目的src/main/java,src/test/java,src/main/resources
對應(yīng)于archetype-test項目的
archetype-test/src/main/resources/archetype-resources/src/main/java
archetype-test/src/main/resources/archetype-resources/src/test/java
archetype-test/src/main/resources/archetype-resources/src/main/resources
其中配置的src/main/java目錄,采用通配符方式包含所有當(dāng)前包及其子包下的java文件,最終出現(xiàn)在目標(biāo)項目project-name/src/main/java/[package-name]/**/*.java

自定義的一些項目文件
archetype-test/src/main/resources/archetyperesources/src/main/java/dao/BaseDao.java

package ${package}.dao;

import java.io.Serializable;
import java.util.List;

/**
 * 基礎(chǔ)數(shù)據(jù)接口
 * Created by zhenglian on 2017/8/21.
 */
public interface BaseDao<T> {
    void save(T t);
    void update(T t);
    int delete(Serializable id);
    T findById(Serializable id);
    List<T> findAll();
}

注意上面package引入通過${package}變量名方式,這樣可以通過在命令行時動態(tài)指定,結(jié)合上面所提供的archetype-metadata.xml配置,對指定文件進(jìn)行變量替換
archetype-test/src/main/resources/archetype-resources/src/main/java/service/BaseService.java

package ${package}.service;

import java.io.Serializable;
import java.util.List;

/**
 * 基礎(chǔ)服務(wù)接口
 * Created by zhenglian on 2017/8/21.
 */
public interface BaseService<T> {
    void save(T t);
    void update(T t);
    int delete(Serializable id);
    T findById(Serializable id);
    List<T> findAll();
}

配置完成后,項目總體結(jié)構(gòu)如上面給出的圖所示,通過mvn clean installarchetype項目打包到本地倉庫中
如此就可以通過mvn archetype:generate命令使用本地創(chuàng)建的archetype項目架構(gòu)了
mvn archetype:generate -DarchetypeGroupId=com.zheng.archetypestudy -DarchetypeArtifactId=archetype-test -DarchetypeVersion=1.0-SNAPSHOT

注意在運行該命令的時候,如果是在archetype-test項目根目錄下,那么會報錯,提示Unable to add module to the current project as it is not of packaging type 'pom',說明當(dāng)前創(chuàng)建的項目為模塊項目,而父級目錄不是一個pom類型的項目,所以報錯
在上一級目錄下運行即可

自定義archetype運行結(jié)果.png

archetypeCatalog

在使用maven-archetype-plugin插件時,會得到一個列表供選擇,這個列表的信息來源于一個名為archetype-catalog.xml的文件
用戶可以自定義這個archetype-catalog.xml中的內(nèi)容,當(dāng)然也可以通過掃描本地倉庫自動生成基于該倉庫的archetype-catalog.xml文件
那么archetype-catalog.xml配置文件是從哪里來的呢,maven中有以下幾種選擇:
internal maven內(nèi)置的archetypeCatalog
local 指向用戶本地的archetype catalog,其位置為C:\Users\Administrator\.m2\archetype-catalog.xml,但是該文件默認(rèn)是不存在的
remote 指向了maven中央倉庫的archetype catalog,具體地址為http://repo1.maven.org/maven2/archetype-catalog.xml
file://... 用戶可以指定本機任何位置的archetype-catalog.xml文件
http://... 用戶可以使用http協(xié)議指定遠(yuǎn)程的archetype-catalog.xml文件
上面幾種方式可以通過mvn archetype:generate命令的時候,使用archetypeCatalog指定插件使用的catalog,例如:
mvn archetype:generate -DarchetypeCatalog=local

使用本機archetypecatalog.png

上面的local指定使用C:\Users\Administrator\.m2\archetype-catalog.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"
    xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <archetypes>
    <archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-mojo</artifactId>
      <version>1.0</version>
      <description>plugin</description>
    </archetype>
    <archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-quickstart</artifactId>
      <version>1.0</version>
      <description>quickstart</description>
    </archetype>
    <archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-quickstart</artifactId>
      <version>1.1</version>
      <description>quickstart</description>
    </archetype>
    <archetype>
      <groupId>org.apache.maven.archetypes</groupId>
      <artifactId>maven-archetype-webapp</artifactId>
      <version>1.0</version>
      <description>webapp</description>
    </archetype>
    <archetype>
      <groupId>org.appfuse.archetypes</groupId>
      <artifactId>appfuse-modular-spring</artifactId>
      <version>2.0</version>
      <description>appfuse-modular-spring</description>
    </archetype>
  </archetypes>
</archetype-catalog>

上面的archetype-catalog.xml是通過掃描本地倉庫自動生成的,可以通過mvn archetype:crawl來瀏覽當(dāng)前本地倉庫的項目結(jié)構(gòu),默認(rèn)會生成到本地倉庫(localRepository)配置的根目錄下
當(dāng)然也可以通過-Drepository指定本地倉庫位置,-Dcatalog指定生成的archetype-catalog.xml生成的方式
mvn archetype:crawl -Drepository=G:/workspace/repository -Dcatalog=C:/Users/Administrator/.m2/archetype-catalog.xml
如果沒有指定-Drepository參數(shù)時,maven會通過settings.xml中配置的本地倉庫進(jìn)行解析

通過學(xué)習(xí)上面,我相信大家應(yīng)該可以通過自定義自己的archetype來生成屬于自己的項目骨架啦,趕緊動起來吧!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容