当前位置: 首页 > article >正文

自定义一个maven骨架 | 最佳实践

自定义一个maven骨架 | 最佳实践
目的:将一个多模块项目的基础结构制作成maven骨架,实现项目的快速构建,风格统一

公司内部会制定自己的规范及包结构。当创建新项目的时候就需要选择骨架,即可生成包结构。
Archetype都为不同类型的项目提供了快速创建和初始化的方式,从而提高了开发效率。

文章目录

      • 1.准备工作
      • 2.生成骨架
      • 3.将骨架安装到本地仓库
      • 4.通过idea使用骨架
      • 5.注意事项:
      • 6.ignore文件打不进去的处理方案
      • 7.效果图
      • 7.遗留问题

1.准备工作

在父pom加入maven插件(和同级)

<build>
        <pluginManagement>
            <plugins>

                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>


                <!-- 将项目打成maven骨架需要这三个 开始 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-archetype-plugin</artifactId>
                    <version>3.0.1</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <!-- 将项目打成maven骨架需要这三个 结束 -->



            </plugins>
        </pluginManagement>
    </build>

个别插件如果下载不了就多设置几个仓库来源(和同级)

<repositories>
        <repository>
            <id>aliyun-central</id>
            <url>https://maven.aliyun.com/repository/central</url>
        </repository>
        <repository>
            <id>aliyun-jcenter</id>
            <url>https://maven.aliyun.com/repository/jcenter</url>
        </repository>
        <repository>
            <id>aliyun-apache-snapshots</id>
            <url>https://maven.aliyun.com/repository/apache-snapshots</url>
        </repository>
        <repository>
        <id>central</id> <!-- 仓库ID,唯一标识 -->
        <url>https://repo.maven.apache.org/maven2</url>
        </repository>
    </repositories>

2.生成骨架

执行mvn archetype:create-from-project命令生成骨架(要在父pom所在的目录下执行)

执行示意:
D:\java_work_space\cloud\demo>mvn archetype:create-from-project
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] springcloud-eureka-demo                                            [pom]
[INFO] common-server                                                      [jar]
[INFO] eureka-server                                                      [jar]
[INFO] eureka-client                                                      [jar]
[INFO] eureka-client-b                                                    [jar]
[INFO] loadbalancer-client                                                [jar]
[INFO]
[INFO] ----------------< com.example:springcloud-eureka-demo >-----------------
[INFO] Building springcloud-eureka-demo 1.0.0                             [1/6]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:create-from-project (default-cli) > generate-sources @ springcloud-eureka-demo >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:create-from-project (default-cli) < generate-sources @ springcloud-eureka-demo <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:create-from-project (default-cli) @ springcloud-eureka-demo ---
[INFO] Setting default groupId: com.example
[INFO] Setting default artifactId: springcloud-eureka-demo
[INFO] Setting default version: 1.0.0
[INFO] Setting default package: com.example
[INFO] Scanning for projects...
[INFO]
[INFO] -----------< com.example:springcloud-eureka-demo-archetype >------------
[INFO] Building springcloud-eureka-demo-archetype 1.0.0
[INFO] --------------------------[ maven-archetype ]---------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.3.1:resources (default-resources) @ springcloud-eureka-demo-archetype ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 64 resources from src\main\resources to target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.3.1:testResources (default-testResources) @ springcloud-eureka-demo-archetype ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources from src\test\resources to target\test-classes
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:jar (default-jar) @ springcloud-eureka-demo-archetype ---
[INFO] Building archetype jar: D:\java_work_space\cloud\demo\target\generated-sources\archetype\target\springcloud-eureka-demo-archetype-1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.808 s
[INFO] Finished at: 2024-12-19T14:19:58+08:00
[INFO] ------------------------------------------------------------------------
[INFO] Archetype project created in D:\java_work_space\cloud\demo\target\generated-sources\archetype
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] springcloud-eureka-demo 1.0.0 ...................... SUCCESS [  1.956 s]
[INFO] common-server 1.0-SNAPSHOT ......................... SKIPPED
[INFO] eureka-server 1.0.0 ................................ SKIPPED
[INFO] eureka-client 1.0.0 ................................ SKIPPED
[INFO] eureka-client-b 1.0.0 .............................. SKIPPED
[INFO] loadbalancer-client 1.0.0 .......................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.213 s
[INFO] Finished at: 2024-12-19T14:19:58+08:00
[INFO] ------------------------------------------------------------------------

D:\java_work_space\cloud\demo>

观察执行结果,骨架生成于父pom的target文件夹下:
D:\java_work_space\cloud\demo\target\generated-sources
这个目录下可以看到新生成了名为archetype的文件夹,就是骨架

3.将骨架安装到本地仓库

执行mvn install命令(打开骨架的pom所在的目录执行)

执行过程:

D:\java_work_space\cloud\demo\target\generated-sources>cd D:\java_work_space\cloud\demo\target\generated-sources\archetype

D:\java_work_space\cloud\demo\target\generated-sources\archetype>mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] -----------< com.example:springcloud-eureka-demo-archetype >------------
[INFO] Building springcloud-eureka-demo-archetype 1.0.0
[INFO] --------------------------[ maven-archetype ]---------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.3.1:resources (default-resources) @ springcloud-eureka-demo-archetype ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 64 resources from src\main\resources to target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.3.1:testResources (default-testResources) @ springcloud-eureka-demo-archetype ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources from src\test\resources to target\test-classes
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:jar (default-jar) @ springcloud-eureka-demo-archetype ---
[INFO] Building archetype jar: D:\java_work_space\cloud\demo\target\generated-sources\archetype\target\springcloud-eureka-demo-archetype-1.0.0
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:integration-test (default-integration-test) @ springcloud-eureka-demo-archetype ---
[INFO] Processing Archetype IT project: basic
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: springcloud-eureka-demo-archetype:1.0.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: archetype.it
[INFO] Parameter: artifactId, Value: basic
[INFO] Parameter: version, Value: 0.1-SNAPSHOT
[INFO] Parameter: package, Value: it.pkg
[INFO] Parameter: packageInPathFormat, Value: it/pkg
[INFO] Parameter: version, Value: 0.1-SNAPSHOT
[INFO] Parameter: package, Value: it.pkg
[INFO] Parameter: groupId, Value: archetype.it
[INFO] Parameter: artifactId, Value: basic
[WARNING] Don't override file D:\java_work_space\cloud\demo\target\generated-sources\archetype\target\test-classes\projects\basic\project\basic\.idea\artifacts\demo_war.xml
[WARNING] Don't override file D:\java_work_space\cloud\demo\target\generated-sources\archetype\target\test-classes\projects\basic\project\basic\.idea\artifacts\demo_war_exploded.xml
[INFO] Parent element not overwritten in D:\java_work_space\cloud\demo\target\generated-sources\archetype\target\test-classes\projects\basic\project\basic\eureka-server\pom.xml
[INFO] Parent element not overwritten in D:\java_work_space\cloud\demo\target\generated-sources\archetype\target\test-classes\projects\basic\project\basic\eureka-client\pom.xml
[INFO] Parent element not overwritten in D:\java_work_space\cloud\demo\target\generated-sources\archetype\target\test-classes\projects\basic\project\basic\eureka-client-b\pom.xml
[INFO] Parent element not overwritten in D:\java_work_space\cloud\demo\target\generated-sources\archetype\target\test-classes\projects\basic\project\basic\loadbalancer-client\pom.xml
[INFO] Parent element not overwritten in D:\java_work_space\cloud\demo\target\generated-sources\archetype\target\test-classes\projects\basic\project\basic\common-server\pom.xml
[INFO] Project created from Archetype in dir: D:\java_work_space\cloud\demo\target\generated-sources\archetype\target\test-classes\projects\basic\project\basic
[INFO]
[INFO] --- maven-install-plugin:3.1.3:install (default-install) @ springcloud-eureka-demo-archetype ---
[INFO] Installing D:\java_work_space\cloud\demo\target\generated-sources\archetype\pom.xml to D:\maven\mvn_repository\com\example\springcloud-eureka-demo-archetype\1.0.0\springcloud-eureka-demo-archetype-1.0.0.pom
[INFO] Installing D:\java_work_space\cloud\demo\target\generated-sources\archetype\target\springcloud-eureka-demo-archetype-1.0.0.jar to D:\maven\mvn_repository\com\example\springcloud-eureka-demo-archetype\1.0.0\springcloud-eureka-
demo-archetype-1.0.0.jar
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:update-local-catalog (default-update-local-catalog) @ springcloud-eureka-demo-archetype ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.812 s
[INFO] Finished at: 2024-12-19T14:26:00+08:00
[INFO] ------------------------------------------------------------------------

D:\java_work_space\cloud\demo\target\generated-sources\archetype>

此时可以按骨架的坐标到本地maven仓库搜一下有没有生成对应骨架的jar:
D:\maven\mvn_repository\com\example\springcloud-eureka-demo-archetype

至此骨架安装到本地仓库了

4.通过idea使用骨架

我的是2020版的idea:file-》new project-》maven -》勾选create from archetype - 》 add archetype -》 填入对应的坐标信息
即可完成基础项目搭建

5.注意事项:

1.执行命令要找准目录
2.一些教程说在项目的根目录下执行xxx命令。这个根目录,在maven项目中,指的就是pom文件所在的目录

脚手架能力:
重复的工作通过标准的工具解决。

1.mybatis逆向工程支持
2.基本的maven结构
3.按代码风格,形成风格架构比如ddd。按单体或微服务提供常用依赖支持
4.能够适配构建发布流程
5.有固定的分层
6.通用能力(redis,MongoDB工具类)、有基本的工具类支持
7.单元测试实例
8.集成成熟的接口文档,比如yapi

//若依脚手架

https://www.cnblogs.com/kisshappyboy/p/18096562

6.ignore文件打不进去的处理方案

【.ignore文件打不进去】
新增ignore文件(生成骨架后,手动赋值ignore到archetype-resources 目录下的根目录中,再执行install)

确保 .gitignore 位于 archetype-resources 目录下的根目录中。

D:\java_work_space\cloud\demo\target\generated-sources\archetype\src\main\resources\archetype-resources

或者模版工程内的.gitignore文件重命名成__gitignore__这样就会直接打包到骨架中了

从git移除项目无关文件:

git rm --cached *.log
git rm --cached *.iml

7.效果图

骨架安装到本地之后,在本地maven仓库:
在这里插入图片描述

在这里插入图片描述

执行完生成骨架命令之后,骨架于target目录生成:
在这里插入图片描述
在这里插入图片描述
如果想把gitignore文件打包到骨架中,要手动复制,再install:
在这里插入图片描述

在idea使用自定义骨架:
在这里插入图片描述

7.遗留问题

如果要打成骨架的模板工程也带上了.iml .idea文件,即使加了archetype.properties过滤文件也不管用,暂时没找到解决方案


http://www.kler.cn/a/448977.html

相关文章:

  • 车载网关性能 --- GW ECU报文(message)处理机制的技术解析
  • 语音助手关键模块整理
  • 深入理解 Linux wc 命令
  • C++ 杨辉三角 - 力扣(LeetCode)
  • 本机如何连接虚拟机MYSQL
  • 数据挖掘之认识数据
  • JavaScript中函数调用时的参数传递
  • 华为、华三交换机纯Web下如何创关键VLANIF、操作STP参数
  • etcd+京东hotkey探测使用
  • C++ 中的 Lambda 表达式:函数式编程的利器
  • 深入浅出:多功能 Copilot 智能助手如何借助 LLM 实现精准意图识别
  • 关于稀疏数据的模型选择
  • GESP2级2403 小杨的日字矩阵
  • ISP图像处理简介
  • SpringCloud 入门(3)—— Nacos配置中心
  • 短视频矩阵系统种类繁多,应该如何对比选择?
  • 使用Python实现智能家居控制系统:开启智慧生活的钥匙
  • 【Linux】资源隔离机制 — 命名空间(Namespace)详解
  • Python实战:基于表单的暴力破解——以Pikachu靶机系统为例
  • vue中使用echarts做一个基础可滚动的折线图及dataZoom滚动配置项
  • SQL,生成指定时间间隔内的事件次序号
  • Hadoop完全分布式环境部署
  • malloc 分配大堆块(128KB)的一次探索
  • **Adversarial Demonstration Attacks on Large Language Models**
  • 【Leetcode】855. 考场就座
  • 小程序 - 模拟时钟