IDEA 默认自带了 Maven,所以一般不需要额外下载
(而且 IDEA 修改 Maven 配置容易自动变来变去,之前的老版本在项目里面默认只能改当前项目的 Maven 配置,每一次重新创建项目都会使用 IDEA 捆绑的,不如直接改IDEA默认获取读的配置文件即可)
直接 Copy IDEA 安装目录下的 plugins\maven\lib\maven3\conf\settings.xml 文件到 C:\Users\用户名\.m2 文件夹
然后将下面的粘贴到:settings.xml:
为什么改这个文件,因为IDEA默认就会获取这个配置:试所有直接改这个文件就等于修改了IDEA的配置
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd">
<!-- ...... -->
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>nexus-tencentyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus tencentyun</name>
<!-- 如果有问题可以改为 https -->
<url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
</mirror>
</mirrors>
<!-- ...... -->
</settings>
本地仓库有什么用:Maven 解析依赖时会先检查本地仓库,如果没有,则去repositories找(Maven 默认有一个中央仓库,外网比较忙,所有才需要上面的软件源),还没找到或者网络问题(尤其是学校的校园网)则会失败,重试就好了(最好还是换源)
修改 settings.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://maven.apache.org/xsd/settings-1.2.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:\</localRepository>
</settings>
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>icu.twtool</groupId> <!-- 一般为域名的倒序 -->
<artifactId>pro-web</artifactId> <!-- 这个 Maven 工程 的唯一 ID 标识 -->
<version>1.0-SNAPSHOT</version> <!-- 这个 Maven 工程 的版本 -->
<properties>
<!-- 配置源文件版本,和 Maven 工程编译后允许的最低 JDK 版本(如1.8, 17) -->
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<!-- 要引入的 Jar 包写这里 -->
</dependencies>
<build>
<!-- 构建的相关配置(如插件)写这里 -->
</build>
</project>
IDEA 直接右键 pom.xml 文件点击 Add as Maven Porject ,就可以添加为 IDEA 的 Maven 模块
Maven Repository: 挺好用的,但是有时候国内访问不了
Maven Central Repository Search: Maven 官方的
仓库服务 (aliyun.com):阿里巴巴的,可以搜索的仓库比较全,但是查找的版本号不是最新最前,文件结构也有点混乱
在上面找到的 Jar 就可以在 Maven 工程引用了
<?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>
<!-- ...... -->
<dependencies>
<!-- 引入 MySQL 的 JDBC 驱动(MySQL) MySQL从 8.0.31 开始从 mysql-connector-java 改为 mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.31</version>
</dependency>
</dependencies>
<!-- ...... -->
</project>
IDEA 直接双击 Ctrl 键即可执行
mvn package -Dmaven.test.skip=true:不执行 test,也不编译测试模块的类
mvn package -DskipTests: 打包不执行测试,但编译测试用例类生成相应的class文件至target/test-classes下