Maven构建Spring Boot后端项目的步骤
使用Maven构建Spring Boot后端项目的步骤通常包括以下几个部分:
-
环境准备:
- 确保已经安装了Java Development Kit (JDK),Spring Boot 项目通常需要 Java 8 或更高版本(推荐使用Java8, 高版本jdk可能存在 收费问题)。
- 安装 Apache Maven。你可以从 Apache Maven 官网 下载并安装最新版本的 Maven。
-
创建项目结构:
- 使用 Spring Initializr 或者 IntelliJ IDEA, Eclipse 等 IDE 创建一个新的 Maven 项目。
- 如果使用命令行,可以使用
spring init
命令初始化一个基本的 Spring Boot 项目。
-
配置pom.xml文件:
- 在项目的根目录下找到
pom.xml
文件,并配置项目的依赖项。 - 添加 Spring Boot Starter Parent 作为父 POM,以便继承默认配置。
- 添加所需的 Starter 依赖,例如
spring-boot-starter-web
用于 Web 开发。 - 根据项目需求添加其他依赖,比如数据库驱动、测试框架等。
- 在项目的根目录下找到
<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>com.example</groupId>
<artifactId>my-spring-boot-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- 使用Spring Boot Starter Parent以获得一些默认配置 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
<relativePath/> <!-- 查找父POM的位置 -->
</parent>
<properties>
<java.version>17</java.version>
<spring-boot.version>3.1.2</spring-boot.version>
</properties>
<dependencies>
<!-- Spring Boot Web Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Data JPA 和 MySQL 数据库驱动 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Spring Boot Cache Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Spring Boot Test Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Lombok for boilerplate code reduction -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- Springfox for Swagger 2 API Documentation -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!-- Other utilities or libraries you might need -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven Plugin to package the app as an executable jar -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 编写代码:
- 创建必要的 Java 类和接口。
- 实现业务逻辑。
- 编写单元测试或集成测试(可选)。
- 基本的代码目录结构如下:
src/
|-- main/
| |-- java/
| | |-- com.example.myapp/
| | | |-- application/ # 应用程序启动类
| | | |-- config/ # 配置类
| | | | |-- AppConfig.java # Spring Boot 配置类
| | | | |-- DatabaseConfig.java # 数据库配置类
| | | | |-- CacheConfig.java # 缓存配置类
| | | |-- controllers/ # 控制器层
| | | | |-- MyController.java # RESTful 控制器
| | | |-- dtos/ # 数据传输对象 (DTOs)
| | | | |-- UserDto.java # 用户 DTO
| | | |-- entities/ # 实体类
| | | | |-- User.java # 用户实体类
| | | |-- repositories/ # 数据访问层 (DAOs)
| | | | |-- UserRepository.java # 用户 Repository
| | | |-- services/ # 业务逻辑层
| | | | |-- UserService.java # 用户服务接口和服务实现
| | | |-- utils/ # 工具类
| | | | |-- StringUtils.java # 字符串工具类
| | | | |-- DateUtils.java # 日期工具类
| | | |-- security/ # 安全性相关类
| | | | |-- SecurityConfig.java # 安全配置类
| | | | |-- JwtTokenProvider.java # JWT Token 处理类
| | | |-- exceptions/ # 自定义异常处理
| | | | |-- CustomException.java # 自定义异常类
| | | | |-- ExceptionHandler.java # 异常处理器
| | | |-- enums/ # 枚举类型
| | | | |-- Status.java # 状态枚举
| | | |-- filters/ # 过滤器
| | | | |-- CustomFilter.java # 自定义过滤器
| | | |-- interceptors/ # 拦截器
| | | | |-- LoggingInterceptor.java # 日志记录拦截器
| | | |-- listeners/ # 监听器
| | | | |-- ApplicationListener.java # 应用监听器
| | | |-- tasks/ # 定时任务
| | | | |-- ScheduledTasks.java # 定时任务类
| | |-- resources/
| | | |-- application.properties # 应用配置文件
| | | |-- application.yml # YAML 格式的配置文件
| | | |-- messages.properties # 国际化消息资源文件
| | | |-- static/ # 静态资源文件
| | | | |-- css/
| | | | |-- js/
| | | | |-- images/
| | | |-- templates/ # Thymeleaf 或其他模板引擎模板
| | | | |-- index.html # 主页模板
| | | |-- logs/ # 日志文件
| | |-- sql/ # SQL 脚本
| | | |-- schema.sql # 数据库表结构脚本
| | | |-- data.sql # 初始数据脚本
|-- test/
| |-- java/
| | |-- com.example.myapp/
| | | |-- controllers/ # 控制器测试
| | | | |-- MyControllerTest.java
| | | |-- services/ # 服务层测试
| | | | |-- UserServiceTest.java
| | | |-- repositories/ # 存储层测试
| | | | |-- UserRepositoryTest.java
| | | |-- utils/ # 工具类测试
| | | | |-- StringUtilsTest.java
| | | |-- config/ # 配置类测试
| | | | |-- AppConfigTest.java
| | | |-- security/ # 安全相关测试
| | | | |-- SecurityConfigTest.java
| | | |-- exceptions/ # 异常处理测试
| | | | |-- ExceptionHandlerTest.java
-
打包项目:
- 使用命令行进入到项目根目录,运行以下命令来打包项目:
mvn clean package
- 这个命令会清理旧的构建结果,编译源代码,运行测试(如果有的话),然后将项目打包成 JAR 或 WAR 文件。
- 使用命令行进入到项目根目录,运行以下命令来打包项目:
-
运行项目:
- 打包完成后,可以在项目的目标目录 (
target
) 中找到生成的 JAR 文件。 - 运行 JAR 文件:
java -jar target/your-project-name.jar
- 打包完成后,可以在项目的目标目录 (
-
验证应用:
- 访问应用的 URL 来确认是否成功启动。
- 如果应用中包含 REST API,可以通过工具如 Postman 或浏览器访问 API 端点进行测试。
-
常见组件
-
数据库驱动
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc10</artifactId> <scope>runtime</scope> </dependency>
-
缓存组件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> <version>3.1.5</version> </dependency> <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.9.6</version> </dependency>
-
接口文档组件
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
-
.
9. 相关文献参考
pom.xml文件格式说明
springboot官网
maven中心仓库
spring initialize 官网