引入archetype插件

在项目添加 依赖

1
2
3
4
5
 <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>3.2.1</version>
</plugin>

新建archetype.properties文件

在项目根目录下新建archetype.properties模板规则文件

1
2
3
4
5
# 添加不需要放到模板的正则匹配
excludePatterns=.idea/**,README.md,.mvn/wrapper/*,logs/**
# 需要替换处理的文件后缀,默认没有yml、yaml
archetype.filteredExtensions=java,yml,yaml,xml,txt,groovy,cs,mdo,aj,jsp,gsp,vm,html,xhtml,properties,classpath,project,doc,docx,bat,sh,sql,vm

生成archetype

1
mvn clean archetype:create-from-project

idea 执行 clean 再执行插件的create-from-project

image-20240613121711472

成功后target 目录下会生成archetype 目录,如下

image-20240613121859263

target目录下会有generated-sources目录,generated-sources/archetype/src/main.resource/META_INF.maven下会有一个archetype-metadata.xml文件,这里是可以配置那些资源会被包含在骨架中,那些不会包含在骨架中。

安装骨架到本地仓库

到generated-sources/archetype 目录下安装骨架到本地

1
mvn clean install -Dmaven.test.skip=true

在本地仓库生成坐标信息

1
mvn archetype:crawl

本地的maven仓库根目录中就会生成一个archetype-catalog.xml文件,里面有固件的坐标信息

发布到私服

1
mvn deploy

新建项目

本地

image-20240613143158484

远程仓库

使用远程nexus仓库的archetype创建项目

使用远程nexus仓库的archetype创建项目的时候,必须在自己的maven confsettings.xml加入以下配置:

因为Maven 3改变了原型存储库的集成方式-DarchetypeRepository参数不再存在。相反,需要将archteype存储库添加到settings.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://192.168.12.62:10000/repository/maven-releases/</url>
</repository>
</repositories>
</profile>
</profiles>

<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>

思考

可以做成一个服务或页面直接访问生成