老大说新项目的结构和 xxx 项目一样就可以了,我 ……(使用 Maven Archetype 快速创建项目)

前言


又要开发新项目了,还是创建新项目,怎么办?老大说按照 xxx 项目的结构创建一个新项目就可以了。


公众号:liuzhihangs,记录工作学习中的技术、开发及源码笔记;时不时分享一些生活中的见闻感悟。欢迎大佬来指导!

在工作中经常有新项目需要创建,此时就会有三种常用的方式

CC 大法 新建项目,然后找到之前的各种工具类,复制粘贴进来,此时还不一定能跑起来,然后再进行各种调试。

archetype-cc-45VBU1

CD 大法 复制老项目,然后改 module 名字,依赖名字,删除老代码,当然也不一定能跑起来,此时再进行各种调试。

archetype-cc1-SPx1ct

当然,这里肯定不是使用这两种办法,下面咱们介绍一种更简洁的方式,使用 maven archetype 生成项目模版,一键创建项目。

Action!!!

什么是 Archetype ?

简而言之,Archetype 是 Maven 项目模板工具箱。

An archetype is defined as an original pattern or model from which all other things of the same kind are made.

原型被定义为原始样式或模型,从中可以制成所有其他同类项目。

官方解释,简洁明了,就是使用已有的项目,生成一个模版。以后使用这个模版就可以快速生成结构相同的项目了。在团队开发中很有用。

其实就是官方解释,地址贴上来:http://maven.apache.org/archetype/maven-archetype-plugin/index.html

下面使用 IDEA 作为演示工具,一步一步开始介绍。

8e83e6d78b16698fb19762806acadd1c-ZXYM5I

准备模版项目

这里还是要有一个模版项目,比如这样:

archetype1-lZkrqX

这是一个多 module 项目,一个简单的 demo

  1. 使用了 nacos 作为注册中心;
  2. fegin 作为调用工具;
  3. 有通用校验 token 工具类;
  4. 假设里面也有一些公司的公共配置(MQ,链路监控,统一日志等)。

当然这个项目是可以跑起来的。新创建的项目也是这个模版。

进入主题

使用命令

**archetype:generate **

这样是基于当前项目生成,你也可以使用官网的用法分别指定参数

  1. mvn archetype:generate 然后一步一步按照提示输入;
  2. 一次性输入,其中 DarchetypeGroupId 、DarchetypeArtifactId 、DarchetypeVersion 为生成的 Archetype 项目的组织版本。
mvn archetype:generate                                  
  -DarchetypeGroupId=<archetype-groupId>                
  -DarchetypeArtifactId=<archetype-artifactId>          
  -DarchetypeVersion=<archetype-version>                
  -DgroupId=<my.groupid>                                
  -DartifactId=<my-artifactId>

高级用法 mvn clean archetype:create-from-project

执行图示如下:

archetype-iterm-GONbvt

执行后返回 IDEA 查看项目,在 target/generated-sources 目录下的 archetype 即生成的项目模版。

archetype-afer-term-vP3Tz3

结构如图所示:

  1. main/resources/archetype-resources:项目的模版,生成新项目,就是根据这块的代码进行生成的。
    1. .idea 无用,删除掉。
    2. __rootArtifactId__xxx 项目的各个 module
  2. main/resources/META-INF/maven/archetype-metadata.xml:模版工程的元数据配置。

可以把 archetype 拷出去,这是一个单独的工程模版,拷出去之后,使用 IDEA 打开。

下面开始介绍 archetype 里面都有什么。

archetype 模版项目介绍

使用 IDEA 打开之后发现,还是一个 Maven 项目。

archetype-resources

archetype-structure-xUdYE2

打开 pom 文件,可以看到里面 ${groupId}${artifactId}${version} 使用占位符指定的组织版本,这些就是新创建项目时指定的。

archetype-metadata.xml

archetype-metadata.xml 里面为元数据配置。

  • fileSet:用来生成一些项目中的文件。如果文件或目录名称包含 __property__ 模式,则将其替换为相应的属性值。
属性 类型 描述
filtered boolean 过滤文集,将指定文件直接复制不需要修改。默认值为:false。
packaged boolean 打包文件,指定文件将在package属性之前的目录结构中生成/复制。它们可以是非打包的,这意味着所选文件将在没有该前缀的情况下生成/复制。默认值为:false。
encoding String 过滤内容时使用的编码。

fileSet 包含以下元素:

元素 类型 描述
directory String 生成项目文件的目录
includes/include* List 包含文件
excludes/exclude* List 排除文件

因为生成项目不需要 .idea *.iml 文件,所以直接删除:

archetype-metadata1-iVIvLq

  • module 就是要生成的项目一共几个 module
属性 类型 描述
id String The module's artifactId.
dir String The module's directory.
name String The module's name.
元素 类型 描述
fileSets/fileSet* List 文件
modules/module* List 模块

archetype-metadata2-HCnJqd

可以看出里面就是自己的项目模版。

__rootArtifactId__-controller 在生成的时候,就会根据传入的 artifactId 生成指定的 module 名字。

使用

  1. clean install

archetype-clean-install-zbua0z

  1. IDEA Add Archetype

archetype-maven-b3sZ6a

  1. 选择使用 Archetype 生成新项目

archetype-maven2-HDnFc4

  1. 填写新生成项目的名字等

archetype-maven3-sbJlI1

  1. 生成新项目

archetype-generate-5GUrpu

扩展

Q: 如何自定义包路径?

A: 可以使用 requiredProperties 自定义参数。通过传入自定义的参数,来生成自定的包路径。

比如发现新生成项目的包路径都是 com.liuzhihang.archetype,这样肯定是不行的,每个项目有每个项目自己的包路径。只需要做以下修改:

  1. requiredProperties 添加到项目中,然后添加新变量 middlePackage
<requiredProperties>
    <!--使用archetype时候必须要求输入的参数-->
    <requiredProperty key="groupId">
        <!--可以设置默认值,使用archetype会使用默认值-->
        <defaultValue>com.liuzhihang</defaultValue>
    </requiredProperty>
    <requiredProperty key="package">
        <defaultValue>com.liuzhihang</defaultValue>
    </requiredProperty>
    <requiredProperty key="middlePackage">
        <defaultValue>${rootArtifactId}</defaultValue>
    </requiredProperty>
</requiredProperties>
  1. 修改模版的文件名

archetype-middle-mH0Gzf

如果文件或目录名称包含 __property__ 模式,则将其替换为相应的属性值。到这里还不行,因为生成的包名还没改。

  1. 修改内部文件的包路径。包括 .java 、** .xml** 、** .properties** 等。

archetype-change-middle-3C0Msb

  1. 重新 clean install

注:此时可能会报错,需要在 src/test/resources/projects/basic/archetype.properties 下添加 middlePackage=basic 再重新尝试下。

在生成时注意指定 middlePackage 属性。

archetype-middle-1-r8qOco

Q: 我想自定义 Application 的名字怎么弄?

A: 同样使用 requiredProperties 自定义参数。

<requiredProperty key="appName">
</requiredProperty>

archetype-app-name-Vt9Xj3

当然也可以起一个通用的名字。

Q: 别的小伙伴怎么用?

A: 当然是 deploy 到私服了, 在 pom 里面添加如下配置,指定自己公司的私服。deploy ,这样就可以和小伙伴一起愉快的使用啦。

<!-- 远程仓库 -->
<distributionManagement>
    <repository>
        <id>releases</id>
        <name>Nexus Release Repository</name>
        <url>http://liuzhihang.com:xxxx/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>snapshots</id>
        <name>Nexus Snapshot Repository</name>
        <url>http://liuzhihang.com:xxxx/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

Q: 我要怎么从 IDEA 删除 Archetype ​?

A: 这么好用怎么舍得删除​呢?只要找到以下路径

liuzhihang % > pwd
/Users/liuzhihang/Library/Caches/JetBrains/IntelliJIdea2020.1/Maven/Indices

里面有一个 UserArchetypes.xml​, 打开,删除掉里面的 archetype 就行。

archetype-delete-ZyQULu

相关资料

代码:https://github.com/liuzhihang/archetype-demo

原文地址:https://www.cnblogs.com/liuzhihang/p/archetype.html