目 录CONTENT

文章目录

Maven:Archetype构建

Jinty
2024-01-15 / 0 评论 / 0 点赞 / 88 阅读 / 6332 字

Maven Archetype Plugin

Maven Archetype Plugin 3.2.1

Maven Archetype is a set of tools to deal with archetypes, i.e. an abstract representation of a kind of project that can be instantiated into a concrete customized Maven project. An archetype knows which files will be part of the instantiated project and which properties to fill to properly customize the project.

  • 是一组用于处理原型的工具

  • 可以实例化为具体定制的Maven项目的抽象表示

Archetype - 原型知道哪些文件将成为实例化项目的一部分,以及填充哪些属性以正确定制项目。

通过Maven Archetype Plugin我们可以通过已有项目构建一个原型,而后再通过该原型快速构建新项目。

环境说明

  • IDEA版本:IntelliJ IDEA 2023.1.1 (Community Edition)

  • Maven版本:Apache Maven 3.3.9

  • 一个仅引入了Junit依赖的简单示例maven项目

使用步骤说明

1. 引入:添加 maven-archetype-plugin

在已有maven项目的pom.xml中引入maven-archetype-plugin 插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-archetype-plugin</artifactId>
    <version>3.2.1</version>
</plugin>

完整pom.xml(一个非常简单的maven项目示例,具体项目结构不展开说明):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.com.jcoo</groupId>
    <artifactId>maven-archetype-jclab2</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-archetype-plugin</artifactId>
                <version>3.2.1</version>
            </plugin>
        </plugins>
    </build>
</project>

引入成功后可在插件处看到可执行插件工具:

2. 创建原型:以当前项目构建生成原型

点击archetype:create-from-project或者在项目路径下执行命令mvn clean archetype:create-from-project ,成功后将会在target生成如下内容:

3. 打开原型:打开生成的原型项目

复制绝对路径:

IDEA打开:

4. 安装原型:将生成的原型项目安装在本地仓库

执行maven生命周期工具 install

5. 扫描并添加到本地archetype-catalog

执行archetype插件工具archetype:crawl ,该步骤将扫描本地仓库,把打包类型为maven-archetype(<packaging>maven-archetype</packaging>)的依赖添加到archetype-catalog.xml文件

6. 检查本地archetype-catalog

maven所配置的本地仓库的根路径下,检查archetype-catalog.xml文件是否更新:

7. 验证:从已有的原型构建新项目

可以先配置archetype本地目录后再选择

配置-DarchetypeCatalog=internal 可直接从本地构建加快构建过程

0

评论区