maven3.8.5 建立archetype
1. 建立maven项目
<groupId>cn.webrx</groupId>
<artifactId>springboot-simple</artifactId>
<version>1.0</version>
建立完成的项目结构
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本地库目录
4. Idea 建立Add Archetype
5. 使用archetype
idea 建立maven项目,并使用 cn.webrx:springboot-simple-archetype
第二步:
第三步:
第四步:点击Finish