什么是maven archetype?详细定义参考maven官网。
下面开始我们自定义archetype实现(基于现有项目构建)
1、根据需要构建maven项目
本示例根据 springboot + mybatis-plus + shardingsphere + nacos 实现,仅供参考
代码上传gitee
gitee地址:https://gitee.com/wpml/lc-maven-rw-java.git
自行搭建自己所需项目,也可以参考本人gitee上的代码,有啥疑问欢迎留言探讨
项目结构如下
2、构建archetype步骤
1、修改pom文件,添加archetype 插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>3.0.0</version>
</plugin>
2、cd 到项目根目录执行下面命令 生成archetype模板
mvn archetype:create-from-project
执行完该命令会在target目录下生成
3、进入archetype目录
cd target/generated-sources/archetype/
mvn install #安装到本地
4、执行下面命令生成archetype-catalog.xml文件 默认在settings.xml中配置的 E:\repository 目录中
mvn archetype:crawl
archetype-catalog.xml 文件内容如下
<archetypes>
<archetype>
<groupId>com.lianchuang</groupId>
<artifactId>lc-maven-rw-java-archetype</artifactId>
<version>1.0.0</version>
<description>pom依赖管理</description>
</archetype>
</archetypes>
5、发布模板到私服
- 修改maven配置,settings.xml中添加配置
- 修改pom文件
打开项目目录下的target\generated-sources\archetype\pom.xml文件,添加distributionManagement配置,此处nexus-releases 要与上面中配置的ID保持一致
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://nexus.***.com:8089/nexus/content/groups/public/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://nexus.***.com:8089/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
- 执行命令上传 mvn deploy
3、idea 中使用自定义maven-archetype
1、添加自定义模板信息,需要注意的是archetype-catalog.xml文件中可能存在多个组件,此处选择自己需要的(自己生成的模板)
添加之后,创建项目时选择新添加的maven archetype
下图添加了一个package参数,作用是帮助创建包名(也可以创建项目后自行手动创建包)
下面是settings.xml的配置模板,仅供参考,根据实际修改;
<?xml version="1.0" encoding="UTF-8"?>
<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">
<localRepository>E:\repository</localRepository>
<servers>
<server>
<id>rdc-releases</id>
<username>xxxx</username>
<password>xxxx</password>
</server>
<server>
<id>rdc-snapshots</id>
<username>xxxx</username>
<password>xxxx</password>
</server>
<server>
<id>archetype</id>
<username>xxxx</username>
<password>xxxx</password>
</server>
</servers>
<mirrors>
<mirror>
<id>local</id>
<name>local maven</name>
<url>http://xxxx:8081/repository/maven-public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>rdc</id>
<repositories>
<repository>
<!-- the repository id has to be named: archetype 这repository Id必须是archetype -->
<id>archetype</id>
<name>my archetypes</name>
<url>http://xxxx:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件
<pluginRepository>
<id>public</id>
<name>Public Repositories</name>
<url>http://xxxx:8081/repository/maven-public/</url>
</pluginRepository>
-->
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>rdc</activeProfile>
</activeProfiles>
</settings>
配置完成后就可以实现上传和下载私服中的依赖包了