maven自定义Archetype

1、创建模板项目

    如下

maven自定义Archetype

2、模板项目的pom.xml中添加archetype插件

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-archetype-plugin</artifactId>
	<version>3.0.1</version>
</plugin>

3、eclipse建立archetype项目

    如果使用的是eclipse工具,右键->Debug As->Maven build,执行mvn archetype:create-from-project

maven自定义Archetype

4、Archetype安装到本地仓库

会生成target\generated-sources\archetype目录,进入该目录,执行

mvn install

build success,表示Archetype已经安装到本地仓库。

5、archetype-catalog.xml

    查看Maven安装目录下conf文件夹中的settings.xml文件,如果你在此设置了<localRepository>本地仓库路径,则Archetype就安装在这个路径下。同时在<localRepository>默认路径:个人文件夹/.m2目录下会有一个archetype-catalog.xml文件,如果没有这个文件,就执行 mvn archetype:crawl 命令生成该文件。打开文件,可以看到Archetype的groupId、artifactId等。

<?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>test.pro</groupId>
      <artifactId>test.pro-archetype</artifactId>
      <version>1.0.0</version>
      <description>test.pro-archetype</description>
    </archetype>
  </archetypes>
</archetype-catalog>

6、使用自定义的Archetype

    eclipse新建maven project,进入到

maven自定义Archetype

默认是没有我们自定义的Archetype,点击上面的按钮

maven自定义Archetype

添加本地catalog

maven自定义Archetype

选中生成的archetype-catalog.xml,点击ok,就可以得到

maven自定义Archetype

7、mvn命令创建项目

mvn archetype:generate  

选择对应的数字,生成本地项目。

8、删除安装的自定义Archetype

(1)archetype-catalog.xml删除:打开个人文件夹/.m2目录下的archetype-catalog.xml文件,将对应的<archetype></archetype>删除,比如删除文中刚刚安装的自定义archetype,那就把第5步中的<archetype></archetype>删除;

(2)eclipse删除:如图

maven自定义Archetype

9、修改安装的自定义Archetype

    某些时候,我们可能对生成的archetype不太满意,比如要改动配置文件,虽然也可以新建项目后直接修改,但这样每次新建后都要修改,所以就在源项目中先修改了,然后再重新生成一次archetype吧。

(1).执行上面的删除步骤;

(2).进入项目根目录,把target\generated-sources\archetype目录下的文件都删除掉或者直接把target下的generated-sources文件夹删掉,然后重新生成Archetype就行了。