定制Maven原型生成项目

1自定义原型


1.1创建原型项目

要定制自己的原型,首先就要创建原型项目来进行定制:

mvnarchetype:create -DgroupId=com.cdai.arche -DartifactId=test-archetype-DarchetypeArtifactId=maven-archetype-archetype

生成项目结构如下:



1.2安装原型到本地

在pom.xml中添加:

    <build>

       <plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-archetype-plugin</artifactId>

                <version>2.2</version>

            </plugin>

        </plugins>

    </build>

在pom.xml所在目录下执行:

         mvn archetype:create-from-project

就会在target目录下面生成generated-sources目录,这个就是生成的archetype

 

切换目录后执行:

         cd targetgenerated-sourcesarchetype

         mvn install

这样就把自定义的archetype安装到本地仓库了。archetype安装的地址是在maven安装目录下面的conf/settings.xml文件中指定的<localRepository>。默认会在  ~/.m2  目录下面生成一个archetype-catalog.xml文件。

 

archetype-catalog.xml内容如下:

===============================================================================

<?xmlversion="1.0" encoding="UTF-8"?>

<archetype-catalogxsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0http://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.cdai.arche</groupId>

     <artifactId>test-archetype-archetype</artifactId>

     <version>1.0-SNAPSHOT</version>

     <description>test-archetype-archetype</description>

    </archetype>

  </archetypes>

</archetype-catalog>


2从已有项目生成原型

只需执行上面列举的两条命令,即可将一个已有项目安装到本地原型库:

         mvn archetype:create-from-project

         cd targetgenerated-sourcesarchetype

         mvn install


3生成项目


3.1用原型生成项目

mvn archetype:generate -B-DarchetypeCatalog=local -DarchetypeRepository=local -DarchetypeGroupId=com.cdai.arche-DarchetypeArtifactId=test-archetype -DarchetypeVersion=0.0.1-SNAPSHOT-DgroupId=com.cdai -DartifactId=test

参考资料

使用maven3 创建自定义的archetype

http://blog.csdn.net/uncleja/article/details/7252780

原文地址:https://www.cnblogs.com/xiaomaohai/p/6157734.html