Skip to content

使用 Maven Archetype 创建 Spring Boot 项目

Archetype 是 Maven 的项目模板工具包,用于快速创建项目的初始结构。

通过现有的 Maven 项目可以生成骨架,然后通过骨架快速地创建新的项目。

从项目创建骨架

首先在现有的 Maven 项目根目录下执行如下命令:

bash
mvn archetype:create-from-project

这个命令会自动在当前项目根目录的 \target\generated-sources\archetype\ 路径下生成骨架的代码。

打开其中的 pom.xml 文件可以看到项目的 GroupId、ArtifactId 等。默认的 ArtifactId 为当前项目的 ArtifactId 加 -archetype

注意

本地执行时发现项目运行时生成的日志文件也会被复制过去,最好在执行下一步操作前将这些文件删除掉。

安装骨架

cd 到该目录下执行如下命令:

bash
mvn clean install

这个命令本身的功能就是将 Maven 项目安装到本地仓库,对于 archetype 来说也是一样的。

从骨架创建项目

在 JetBrain IDEA 的新建项目页面,通过 Archetype 右边的 添加 按钮将刚刚安装的骨架加进列表,参数同骨架代码的 pom.xml 里一致即可。

注意

本地使用正在做的一个项目测试时发现,新创建的项目的 version 全部都变成了 1.0-SNAPSHOT。创建时的默认版本在折叠起来的高级设置里,创建的时候不太容易注意到。另外一个虽然有层级关系的子包,但是原项目中这个子包没有设置 <parent>,但是通过骨架创建的新项目中却自动把 <parent> 标签加上了。

Archetype 添加后下拉框中看不到

不知道为什么,我这边在添加骨架后直接使用时没有问题,但是再次打开新建项目页面时,看不到刚刚添加的骨架。

看到有文章说是在 设置 → 构建、运行、部署 → 构建工具 → Maven → 仓库 页面更新仓库后就可以看到了,但是我这边更新后仍然看不到。

另外,本地仓库的根目录下面确实有一个 archetype-catalog.xml 文件,而且里面包含上面安装的 archetype 的,但是将目录切换到 默认本地 后 Archetype 下拉框仍然是空的。

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.