使用原因
在建立项目或者模块的时候常常会copy已经搭建好的项目或者模块。此时我们可以利用maven 中的archetype来进行自定义项目脚手架搭建。只需要将你需要的项目模板准备好,下次需要用到该模板为框架的项目就无需一点一点的进行copy直接生成项目,例如idea中利用maven 创建一个web项目。
搭建脚手架方法
- 建立一个项目模板。
- 写上模板代码。
@RestController
@RequestMapping("")
@Slf4j
public class Controller {
@Autowired
private BaseService service;
}
- 使用maven 命令
mvn archetype:create-from-project
从本项目中去进行产生脚手架,运行该命令后会产生如下效果
- 运行结果
- 在项目中会产生如下的结构
- 进入到archetype 目录下面
cd /target/generated-sources/archetype
- 将jar 安装到本地仓库
mvn install - 在repostory中会生成一个
archetype-catalog.xml
文件,里面记录着本地的脚手架信息
<?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>******</groupId>
<artifactId>*****</artifactId>
<version>1.0-SNAPSHOT</version>
<description>Parent pom providing dependency and plugin management for applications
built with Maven</description>
</archetype>
<archetype>
<groupId>*****</groupId>
<artifactId>*******</artifactId>
<version>1.0-SNAPSHOT</version>
<description>Parent pom providing dependency and plugin management for applications
built with Maven</description>
</archetype>
</archetypes>
</archetype-catalog>
使用脚手架方法
- 进入到你需要创建工程的目录下面使用cmd 打开命令窗口
- 输入maven命令
mvn archetype:generate -DarchetypeCatalog=local
从本地archetype中进行查找 脚手架信息
到此项目就通过脚手架搭建完成。。