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

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

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

公司内部会制定自己的规范及包结构。当创建新项目的时候就需要选择骨架,即可生成包结构。

Archetype都为不同类型的项目提供了快速创建和初始化的方式,从而提高了开发效率。

文章目录

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

1.准备工作

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

bash 复制代码
<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>

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

bash 复制代码
<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所在的目录下执行)

bash 复制代码
执行示意:
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所在的目录执行)

bash 复制代码
执行过程:

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移除项目无关文件:

bash 复制代码
git rm --cached *.log
git rm --cached *.iml

7.效果图

骨架安装到本地之后,在本地maven仓库:

执行完生成骨架命令之后,骨架于target目录生成:

如果想把gitignore文件打包到骨架中,要手动复制,再install:

在idea使用自定义骨架:

7.遗留问题

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

相关推荐
hanbarger23 分钟前
mybatis框架——缓存,分页
java·spring·mybatis
cdut_suye30 分钟前
Linux工具使用指南:从apt管理、gcc编译到makefile构建与gdb调试
java·linux·运维·服务器·c++·人工智能·python
苹果醋342 分钟前
2020重新出发,MySql基础,MySql表数据操作
java·运维·spring boot·mysql·nginx
小蜗牛慢慢爬行44 分钟前
如何在 Spring Boot 微服务中设置和管理多个数据库
java·数据库·spring boot·后端·微服务·架构·hibernate
azhou的代码园1 小时前
基于JAVA+SpringBoot+Vue的制造装备物联及生产管理ERP系统
java·spring boot·制造
18号房客1 小时前
macOS开发环境配置与应用开发教程(一)
vscode·macos·visualstudio·eclipse·intellij-idea·phpstorm·visual studio
wm10431 小时前
java web springboot
java·spring boot·后端
smile-yan1 小时前
Provides transitive vulnerable dependency maven 提示依赖存在漏洞问题的解决方法
java·maven
老马啸西风1 小时前
NLP 中文拼写检测纠正论文-01-介绍了SIGHAN 2015 包括任务描述,数据准备, 绩效指标和评估结果
java
果冻的猿宇宙2 小时前
Maven 中央仓库访问过慢的解决方案--设置国内镜像
maven·镜像·仓库·aliyun·国内镜像·mirror