让我们先回顾下本系列的前 3 篇博客:
Spring 入门(一): 创建 Spring 项目
Spring 入门(二): 自动化装配 bean
Spring 入门(三): 通过 JavaConfig 装配 bean
1. 为什么要使用 Maven?
之前我们新建的项目, 项目所要依赖的第三方 jar 包都在项目的类路径下(通常为 lib 目录), 如下所示:
使用这种方式, 我们无法知道第三方类库的依赖关系, 比如导入一个特定的 jar 包时, 可能此 jar 包还依赖于其他的 jar 包, 其他的 jar 包又依赖于更多的 jar 包, 这也是我们平常遇到的 ClassNotFound 异常的主要原因.
为了解决这个问题, 我们急需引入一个项目构建工具. 目前主流的项目构建工具有: Ant,Maven,Gradle 等. 这里我们选择 Maven 作为项目构建工具.
2.Maven 简介
Apache Maven 是一个软件项目管理工具. 基于项目对象模型 (Project Object Model,POM) 的概念, Maven 可用来管理项目的依赖, 编译, 文档等信息.
使用 Maven 管理项目时, 项目依赖的 jar 包将不再包含在项目内, 而是集中放置在用户目录下的. m2 文件夹下.
3.Maven 安装
3.1 下载 Maven
下载地址: http://maven.apache.org/
3.2 安装 Maven
将下载好的 apache-maven-3.6.0-bin.zip 解压到自己喜欢的目录, 我这边解压的目录为: E:\Program Files\apache-maven-3.6.0.
3.3 配置环境变量
环境变量弹窗打开方式: 计算机 -- 属性 -- 高级系统设置 --"高级" 选项卡 -- 环境变量.
1)新建系统环境变量 MAVEN_HOME, 值为: E:\Program Files\apache-maven-3.6.0(步骤 2 中解压的目录)
2)编辑系统变量 Path, 在最后增加:;%MAVEN_HOME%\bin
3.4 验证配置是否成功
打开 cmd 窗口, 执行命令: mvn -version, 如果能够正确的提示如下的版本信息, 说明 Maven 配置成功.
3.5 修改 Maven 配置文件
打开 maven 下 conf 文件夹下的 settings.xml 文件, 找到 localRepository 节点, 配置本地 Repository 路径, 我这里配置的是: E:\Java\MavenRepository.
<localRepository>E:/Java/MavenRepository</localRepository>
注意: 文件中的路径和 Windows 下的路径不一样, 拷贝过来后要将 \ 替换为 /, 而且不能包含中文.
3.6IDEA 配置 Maven
打开 IDE 后, 依次打开 File--Settings, 然后按照如下图所示配置 Maven:
注意: 该设置仅针对当前项目.
4.Maven 的 pom 文件
因为 Maven 是基于项目对象模型 (Project Object Model,POM) 的概念运作的, 所以 Maven 的项目都有一个 pom.xml 用来管理项目的依赖以及项目的编译等功能.
4.1dependencies 元素
- <dependencies>
- </dependencies>
此元素包含多个项目依赖需要使用的 < dependency></dependency>.
4.2dependency 元素
<dependency></dependency > 内部通过 groupId,artifactId,version 确定唯一的依赖, 可以称这 3 个为坐标:
groupId: 组织的唯一标识.
artifactId: 项目的唯一标识.
version: 项目的版本.
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- </dependency>
4.3properties 元素
- <properties></properties > 可定义变量在 dependency 中引用, 代码如下:
- <properties>
- <java.version>1.8</java.version>
- <spring-framework.version>4.3.18.RELEASE</spring-framework.version>
- </properties>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring-framework.version}</version>
- </dependency>
4.4 编译插件
Maven 提供了编译插件, 可在编译插件中涉及 Java 的编译级别, 代码如下:
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.8.0</version>
- <configuration>
- <source>${java.version}</source>
- <target>${java.version}</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
4.5Maven 运作方式
Maven 会自动根据 dependency 中的依赖配置, 直接通过互联网在 Maven 中心库 (https://mvnrepository.com/) 下载相关依赖包到本地 Maven 库, 本地 Maven 库默认是用户目录的. m2 目录, 但上面我们已经把本地 Maven 库修改为了: E:\Java\MavenRepository.
如果你不知道你所依赖的 jar 包的 dependency 怎么写的话, 推荐到 https://mvnrepository.com / 网站检索.
5. 使用 Maven 管理 Spring 项目
5.1 项目添加 Maven 依赖
在 IDEA 中, 选中项目根目录, 依次点击右键 -->Add Framework Support, 然后在弹出的界面中选中 Maven:
此时项目目录下, 会新生成一个 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>
- <groupId>groupId</groupId>
- <artifactId>spring-action</artifactId>
- <version>1.0-SNAPSHOT</version>
- </project>
5.2 配置文件迁移
将之前的 spring-config.xml 配置文件迁移至 src/main/resources 目录下.
5.3pom 文件添加 jar 包依赖
打开 File-->Project Structure-->Modules, 将之前方式引入的第三方 jar 包删除:
然后通过 pom 文件添加 jar 包依赖:
- <dependencies>
- <dependency>
- <groupId>org.hamcrest</groupId>
- <artifactId>hamcrest-core</artifactId>
- <version>2.1</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- </dependency>
- <dependency>
- <groupId>javax.inject</groupId>
- <artifactId>javax.inject</artifactId>
- <version>1</version>
- </dependency>
- <dependency>
- <groupId>com.GitHub.stefanbirkner</groupId>
- <artifactId>system-rules</artifactId>
- <version>1.16.0</version>
- </dependency>
- </dependencies>
修改完 pom 文件记得点击 "Import Changes":
导入完成后的项目结构图为:
可以看出和之前的区别, 现在的 jar 包都由 Maven 来管理, 也可以查看项目的依赖树:
5.4 最终的 pom 文件
- <?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>com.zwwhnly</groupId>
- <artifactId>spring-action</artifactId>
- <version>1.0-SNAPSHOT</version>
- <properties>
- <java.version>1.8</java.version>
- <spring-framework.version>4.3.18.RELEASE</spring-framework.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring-framework.version}</version>
- </dependency>
- <dependency>
- <groupId>org.hamcrest</groupId>
- <artifactId>hamcrest-core</artifactId>
- <version>2.1</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- </dependency>
- <dependency>
- <groupId>javax.inject</groupId>
- <artifactId>javax.inject</artifactId>
- <version>1</version>
- </dependency>
- <dependency>
- <groupId>com.GitHub.stefanbirkner</groupId>
- <artifactId>system-rules</artifactId>
- <version>1.16.0</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.8.0</version>
- <configuration>
- <source>${java.version}</source>
- <target>${java.version}</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
上面讲解的方式适用于新建项目时不是 Maven 项目, 后续转为 Maven 项目的操作步骤.
其实, 我们可以通过 IDEA, 直接新建 Maven 项目, 然后修改 pom 文件为以上内容引入 Spring 等依赖(推荐该方式):
6. 参考
使用 IntelliJ IDEA 配置 Maven(入门)
[Java] Maven 安装和配置
用 IDEA 将普通 web 项目转 Maven 项目
《Java EE 开发的颠覆者: Spring Boot 实战》
7. 源码
源码地址: https://github.com/zwwhnly/spring-action.git , 欢迎下载.
来源: https://www.cnblogs.com/zwwhnly/p/10894781.html