基于现有项目构建自定义maven archetype

什么是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、发布模板到私服

  1. 修改maven配置,settings.xml中添加配置在这里插入图片描述
  2. 修改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>
  1. 执行命令上传 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>

配置完成后就可以实现上传和下载私服中的依赖包了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值