应用框架的搭建需要一定工作量,我们每次开发新应用都手动搭建一遍,或者耐心的慢慢copy现有应用,这个工作确实不是一件愉快的事情。
我们需要一个应用脚手架,每次新应用只需要一个引导界面,填写项目基本名称信息,就可以完成框架的搭建工作。maven正好提供了这种解决方案,我们在创建maven工程的时候,就有很多archetype模板可以选择,并IDE有图形化的引导工具。
那我们就来看看如何创建maven的archetype。
archetype的原理
这里可以看出来,archetype的方案需要两步,
- 创建archetype工程并生成archetype.jar;
- 使用archetype.jar生成maven工程。
然后生成archetype工程,又分为两部分工作,
- 准备脚手架工程文件,就是最终生成应用的内容;
- 声明archetype如何根据工程文件来创建应用。
开始创建archetype工程
创建一个普通maven工程【quickstart】
在src/main/resources下
创建/archetype-resources,存放脚手架工程
创建/META-INF/maven/archetype-metadata.xml,archetype配置文件
这里说明一下META-INF,archetype目前有两种版本1.0和2.0
1.0使用archetype.xml,不支持正则表达式
2.0使用archetype-metadata.xml,支持正则,并且支持多模块工程,还能文件夹上使用占位符
所以这里采用2.0的模式
配置archetype-metadata.xml,生成工程时,如何拷贝文件路径,还有模板引擎对占位符信息进行替换。
这样archetype工程咱们就创建好了,这里为了思路清晰,将工程代码进行简化,只放了一个app.java文件,其他文件处理方式一样的。文章最后给出源码信息。
工程使用mvn install
将archetype工程加入到本地maven仓库
为了后续eclipse整合使用
eclipse整合archetype
生成archetype-catalog.xml文件,把咱们自定义archetype声明,后续eclipse需要使用;
eclipse配置maven插件,引用刚刚创建的archetype-catalog.xml文件
然后就可以通过eclipse的创建maven工程,就能使用咱们自定义的archetype了。
eclipse插件有坑,在更改了archetype-metadata.xml文件后,重新创建应用汇报错,解决办法重启eclipse就可以了。
org.apache.maven.archetype.exception.ArchetypeGenerationFailure: Error merging velocity templates: Unable to find resource 'archetype-resources/pom.xml'
回顾总结
创建maven的archetype脚手架工程,让新建应用不用重复搭建框架,eclipse可以整合,使用图形化引导功能。但是eclipse插件有bug,需要注意。
参考官方资料
官网archetype工程创建教程
archetype-metadata.xml配置文件语法
默认参数: groupId、artifactId、version;可以自定义参数;
参数使用方法: 文件内容,${groupId};文件名称,property
archetype-metadata.xml内容分三部分: requiredProperties、fileSets、modules
源码
archetype-metadata.xml
<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor name="beast-archetype"
xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0
http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd">
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="false" encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/test/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet encoding="UTF-8">
<directory>src/test/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</fileSet>
</fileSets>
</archetype-descriptor>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<packaging>jar</packaging>
<name>${artifactId}</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>