1. 什么是archeType
我們?cè)趧?chuàng)建maven項(xiàng)目的時(shí)候,你會(huì)發(fā)現(xiàn)有這么多的apache提供的模板产捞。
或者使用mvn archetype:generate
命令來快速創(chuàng)建maven項(xiàng)目,也會(huì)有很多個(gè)選項(xiàng)芦劣,讓你選擇模板序號(hào)院尔。那每個(gè)模板之間有什么區(qū)別呢?
每個(gè)模板里其實(shí)就是附帶不同的依賴和插件魁亦。一般在公司私服里都會(huì)有屬于本公司的一套archeType模板渔隶,里面有著調(diào)試好的項(xiàng)目用到的依賴包和版本號(hào)。
2. 創(chuàng)建archetype
假如自己已經(jīng)有了一個(gè)maven項(xiàng)目洁奈,想給該項(xiàng)目創(chuàng)建一個(gè)archeType模板间唉。
cd 到項(xiàng)目根目錄下執(zhí)行(pom.xml同級(jí)目錄)。
mvn archetype:create-from-project
此時(shí)會(huì)在項(xiàng)目target下生成這些文件:
3. 生成archetype模板
先 cd target/generated-sources/archetype/
然后執(zhí)行 mvn install
執(zhí)行成功后利术,執(zhí)行crawl
命令呈野,在本地倉(cāng)庫(kù)的根目錄生成archetype-catalog.xml
骨架配置文件:
mvn archetype:crawl
來看一看它里面的內(nèi)容:
[fantj@lalala repository]$ cat 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>com.fantj</groupId>
<artifactId>my-self-defind-archtype-archetype</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>my-self-defind-archtype</description>
</archetype>
</archetypes>
</archetype-catalog>
4. 使用archetype模板
執(zhí)行
mvn archetype:generate -DarchetypeCatalog=local
從本地archeType模板中創(chuàng)建項(xiàng)目。
mvn archetype:generate -DarchetypeCatalog=local
然后會(huì)讓你選擇模板序號(hào)和groupId``artifactId``version
和package
信息:
Choose archetype:
1: local -> com.fantj:my-self-defind-archtype-archetype (my-self-defind-archtype)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
Define value for property 'groupId': com.fantj
Define value for property 'artifactId': my-self-defind-archetype-test
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.fantj: :
Confirm properties configuration:
groupId: com.fantj
artifactId: my-self-defind-archetype-test
version: 1.0-SNAPSHOT
package: com.fantj
Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: my-self-defind-archtype-archetype:0.0.1-SNAPSHOT
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.fantj
[INFO] Parameter: artifactId, Value: my-self-defind-archetype-test
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.fantj
[INFO] Parameter: packageInPathFormat, Value: com/fantj
[INFO] Parameter: package, Value: com.fantj
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.fantj
[INFO] Parameter: artifactId, Value: my-self-defind-archetype-test
[INFO] Project created from Archetype in dir: /home/fantj/IdeaProjects/maven-tutorial/my-self-defind-archetype-test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
項(xiàng)目創(chuàng)建成功!
當(dāng)然印叁,也可以使用IDEA來幫我們用圖形界面使用archeType模板創(chuàng)建項(xiàng)目:
后面的就與創(chuàng)建普通項(xiàng)目相同了际跪,不做演示。