maven3.8.5 建立archetype

本文档详细介绍了如何使用Maven 3.8.5创建一个Spring Boot项目并将其转化为archetype模板。首先,展示了如何配置pom.xml和编写App.java主类以及application.yml配置文件。接着,通过在命令行运行`mvn archetype:create-from-project`生成archetype,然后执行`mvn install`安装到本地仓库。最后,演示了在Idea中如何添加和使用这个archetype来快速创建新的Spring Boot项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

maven3.8.5 建立archetype

1. 建立maven项目

<groupId>cn.webrx</groupId>
<artifactId>springboot-simple</artifactId>
<version>1.0</version>

建立完成的项目结构

image-20220416085710057

1.1 pom.xml

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.webrx</groupId>
    <artifactId>springboot-simple</artifactId>
    <version>1.0</version>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

1.2 cn.webrx.App.java

src/main/java/cn/webrx/App.java

/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * <p>Project: springboot-simple - App
 * <p>Powered by webrx On 2022-04-16 08:18:37
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

1.3 application.yml

src/main/resources/application.yml

users:
  name: 李四
  age: 22

1.4 AppTest.java

src/test/cn/webrx/AppTest.java

package cn.webrx;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootVersion;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;

/**
 * <p>Project: springboot-simple - AppTest
 * <p>Powered by webrx On 2022-04-16 08:19:31
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@SpringBootTest
class AppTest {
    @Autowired
    ApplicationContext ctx;

    @Value("${users.name}")
    String name;

    @Test
    void t1() {
        System.out.println(name);
    }

    @Test
    void context() {
        for (String n : ctx.getBeanDefinitionNames()) {
            System.out.println(n);
        }
    }

    @Test
    void m() {
        System.out.printf("hello springboot %s", SpringBootVersion.getVersion());
    }
}

2. 生成archetype

命令控制台进入项目springboot-simple目录,mvn archetype:create-from-project

springboot-simple>mvn archetype:create-from-project

3. 安装archetype

cd target/generated-sources/archetype/ //切换到archetype项目跟目录
mvn install //对archetype项目打包安装

mvn install

安装后可以查看maven本地库目录

image-20220416085238331

4. Idea 建立Add Archetype

image-20220416085038068

5. 使用archetype

idea 建立maven项目,并使用 cn.webrx:springboot-simple-archetype

image-20220416085440524

第二步:

image-20220416085503225

第三步:

image-20220416085527666

第四步:点击Finish

image-20220416085623405

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值