1 maven自定义archtype骨架
Maven的 archetype技术,为新建标准化的工程框架提供了方便。为自定义一套工程框架标准,可参考以下步骤操作:
1.1 创建一个项目的原型
项目根目录:project-name
1.2 创建项目骨架
在项目根目录project-name执行命令:mvn archetype:create-from-project,新生成的archetype在project-name /target/generated-sources/archetype目录
project-name/target/generated-sources/archetype/src/main/resources/archetype-resources目录下模版工程的资源元文件,这些元文件是生成工程的时候需要用到,该目录下必须要有一个顶级pom文件,子文件夹代表了模块定义。archetype目录下的pom文件是用来定义骨架groupId,artifactid信息的,用于创建应用的时候。
1.3 手工调整相关archetype源码
1.3.1 文件的变量替换
进入project-name/target/generated-sources/archetype/src/main/resources 目录,手工调整相关archetype源码.
需进行变量替换的文件,需在project-name/target/generated-sources/archetype/src/main/resources/META-INF/maven/archetype-metadata.xml中开启filtered="true";
文件内容替换
将资源中需要订制的地方替换成相应的${groupid},${artifactid},${package},这样maven会在创建项目的过程中自动将这些值传入的相应要替换的地方
比如:
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
文件夹替换
那创建项目的时候回自动替换里面的变量,如果创建的文件名里面有变量,那使用__artifactId__这个格式。
1.3.2 archetype-metadata.xml详解
archetype-metadata.xml文件中重要的几个属性如下:
1.3.2.1 属性变量定义
<requiredProperties>
<requiredPropertykey="appName">
<defaultValue>helloworld</defaultValue>
</requiredProperty>
<requiredPropertykey="groupId">
<defaultValue>com.helloworld</defaultValue>
</requiredProperty>
<requiredPropertykey="artifactId">
<defaultValue>helloworld</defaultValue>
</requiredProperty>
</requiredProperties>
这个不是必填。
1.3.2.2 项目子模块定义
<module id="${artifactId}-biz"dir="__artifactId__-biz" name="${artifactId}-biz"></module>
module有三个属性,解释如下:
id:相当于工程的artifactId.
dir:相当于工程源文件在archetype-resources里对应的directory.
name:模块的名字.
1.3.2.3 项目文件集定义
<fileSets>
<fileSet encoding="UTF-8">
<directory>assets/css</directory>
<includes>
<include>**/*.css</include>
</includes>
</fileSet>
</fileSets>
1.4 编辑项目骨架发布位置
在project-name/target/generated-sources/archetype下有个pom.xml文件,编辑里面的
<groupId>com.***.***.archetype</groupId>
<artifactId>***-archetype</artifactId>
<version>*.*</version>
这样可以发布到自己想要的位置,如果不修改那就放入默认的位置。
1.5 安装项目骨架
在archetype根目录(project-name/target/generated-sources/archetype)执行:
mvn clean install
将该archetype传到本地的maven仓库
1.6 创建骨架项目
创建骨架项目如下:
mvn archetype:generate-DarchetypeGroupId=***.archetype-DarchetypeArtifactId=***-archetype-DarchetypeVersion=**
1.7 查看项目骨架
mvn archetype:generate-DarchetypeCatalog=local
1.8 删除项目骨架
把localRepository目录({M2_HOME}/repository)下面的archetype-catalog.xml文件中对应的<archetype>字节段删掉,
把本地仓库中相应groupId和artifactId下面的文件删掉就可以了。
1.9 发布项目骨架
mvn clean deploy