关于java:老大说新项目的结构和-xxx-项目一样就可以了我-……使用-Maven-Archetype-快速创建项目

8次阅读

共计 4035 个字符,预计需要花费 11 分钟才能阅读完成。

前言

又要开发新我的项目了,还是创立新我的项目,怎么办?老大说依照 xxx 我的项目的构造创立一个新我的项目就能够了。

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

在工作中常常有新我的项目须要创立,此时就会有三种罕用的形式

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

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

当然,这里必定不是应用这两种方法,上面咱们介绍一种更简洁的形式,应用 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/arche…

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

筹备模版我的项目

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

这是一个多 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

执行图示如下:

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

构造如图所示:

  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

关上 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<String> 蕴含文件
excludes/exclude* List<String> 排除文件

因为生成我的项目不须要 .idea *.iml 文件,所以间接删除:

  • module 就是要生成的我的项目一共几个 module
属性 类型 形容
id String The module’s artifactId.
dir String The module’s directory.
name String The module’s name.
元素 类型 形容
fileSets/fileSet* List<FileSet> 文件
modules/module* List<ModuleDescriptor> 模块

能够看出外面就是本人的我的项目模版。

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

应用

  1. clean install

  1. IDEA Add Archetype

  1. 抉择应用 Archetype 生成新我的项目

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

  1. 生成新我的项目

扩大

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. 批改模版的文件名

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

  1. 批改外部文件的包门路。包含 .java .xml .properties 等。

  1. 从新 clean install

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

在生成时留神指定 middlePackage 属性。

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

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

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

当然也能够起一个通用的名字。

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 就行。

相干材料

代码:https://github.com/liuzhihang…

正文完
 0