18.3 Archetype Catalog
18.2节中我们自定义了一个Archetype,然后可以通过指定该Archetype的坐标在命令行用它创建项目原型。但是,18.1.2节告诉我们,通常使用Archetype不需要精确地指定Archetype的坐标,maven-archetype-plugin会提供一个Archetype列表供我们选择。那么,能否把自己创建的Archetype加入到这个列表中呢?答案是肯定的。下面就介绍相关的做法及相关原理。
18.3.1 什么是Archetype Catalog
当用户以不指定Archetype坐标的方式使用maven-archetype-plugin的时候,会得到一个Archetype列表供选择,这个列表的信息来源于一个名为archetype-catalog.xml的文件。例如,代码清单18-6是一个包含了两个Archetype信息的archetype-catalog.xml文件。
代码清单18-6 archetype-catalog.xml
<?xml version="1.0"encoding="UTF-8"?>
<archetype-catalog
xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plu-
gin/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/arche-
type-catalog/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<archetypes>
<archetype>
<groupId>com.juvenxu.mvnbook.archetypes</groupId>
<artifactId>mvnbook-archetype-sample</artifactId>
<version>1.0-SNAPSHOT</version>
<description>sample</description>
</archetype>
<archetype>
<groupId>org.apache.maven.archetypes</groupId>
<artifactId>maven-archetype-quickstart</artifactId>
<version>1.0</version>
<description>quickstart</description>
</archetype>
</archetypes>
</archetype-catalog>
上述archetype-catalog.xml包含的两个Archetype读者应该已经熟悉了,第一个Archetype的坐标是com.juvenxu.mvnbook.archetypes:mvnbook-archetype-sample:1.0-SNAPSHOT,也就是上一节自定义的Archetype;第二个则是maven-archetype-plugin默认使用的Quick-start Archetype。这个XML非常简单,它主要包含了各个Archetype的坐标。这样,当用户选择使用某个Archetype的时候,Maven就能够立刻定位到Archetype构件。