maven自定义archtype骨架

Maven 的 archetype 技术,为新建标准化的工程框架提供了方便。为自定义一套工程框架标准,可参考以下步骤操作:

1,创建一个项目的原型

2,在项目根目录执行命令:mvn archetype:create-from-project,新生成的archetype在target/generated-sources/archetype目录

  archetype-resources目录下模版工程的资源元文件,这些元文件是生成工程的时候需要用到,该目录下必须要有一个顶级pom文件,子文件夹代表了模块定义。archetype目录下的pom文件是用来定义骨架groupId,artifactid信息的,用于创建应用的时候。

3,进入target/generated-sources/archetype/src/main/resources 目录,手工调整相关archetype源码需进行变量替换的文件,需在archetype-metadata.xml中开启filtered="true";将资源中需要订制的地方替换成相应的${groupid},${artifactid},${package},这样maven会在创建项目的过程中自动将这些值传入的相应要替换的地方

比如:

<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>

那创建项目的时候回自动替换里面的变量,如果创建的文件名里面有变量,那使用__artifactId__这个格式。

archetype-metadata.xml文件中重要的几个属性如下:

  a.属性变量定义

       

<requiredProperties>  
    <requiredProperty key="appName">  
        <defaultValue>helloworld</defaultValue>  
    </requiredProperty>  
    <requiredProperty key="groupId">  
        <defaultValue>com.helloworld</defaultValue>  
    </requiredProperty>  
    <requiredProperty key="artifactId">  
        <defaultValue>helloworld</defaultValue>  
    </requiredProperty>   
</requiredProperties>  

  这个不是必填。

   b.项目子模块定义

 <module id="${rootArtifactId}-biz" dir="__rootArtifactId__-biz" name="${rootArtifactId}-biz">
</module>

  module有三个属性,解释如下:
    id     :相当于工程的artifactId.
    dir    :相当于工程源文件在archetype-resources里对应的directory.
    name   :模块的名字.

 c.项目文件集定义

 

 <fileSets>
    <fileSet encoding="UTF-8">
      <directory>assets/css</directory>
      <includes>
        <include>**/*.css</include>
      </includes>
    </fileSet>
</fileSets>

4,在..\target\generated-sources\archetype下有个pom.xml文件,编辑里面的

<groupId>com.***.***.archetype</groupId>
<artifactId>***-archetype</artifactId>
<version>*.*</version>

 这样可以发布到自己想要的位置,如果不修改那就放入默认的位置。

5,在archetype根(..\target\generated-sources\archetype)目录执行:mvn clean install,将该archetype传到本地的maven仓库

6,通过mvn archetype:generate -DarchetypeGroupId=***.archetype -DarchetypeArtifactId=***-archetype -DarchetypeVersion=**就可以创建项目了。

原文地址:https://www.cnblogs.com/iusmile/p/2765318.html