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>org.example</groupId>
<artifactId>maventest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
创建dependencies节点,写出依赖坐标
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
镜像配置
idea安装根目录/plugins/maven/lib/maven3/conf
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
将默认的远程仓库地址(国外),配置为国内的阿里云仓库地址
scope
属性
compile :为默认的依赖有效范围。如果在定义依赖关系的时候,没有明确指定依赖有效范围的话,则默认采用该依赖有效范围。此种依赖,在编译、运行、测试时均有效。
provided :在编译、测试时有效,但是在运行时无效,也就是说,项目在运行时,不需要此依赖,比如我们上面的Lombok,我们只需要在编译阶段使用它,编译完成后,实际上已经转换为对应的代码了,因此Lombok不需要在项目运行时也存在。
runtime :在运行、测试时有效,但是在编译代码时无效。比如我们如果需要自己写一个JDBC实现,那么肯定要用到JDK为我们指定的接口,但是实际上在运行时是不用自带JDK的依赖,因此只保留我们自己写的内容即可。
test :只在测试时有效,例如:JUnit,我们一般只会在测试阶段使用JUnit,而实际项目运行时,我们就用不到测试了
resource
文件夹
将一些静态资源例如配置文件放入,项目自动将其打包
导入本地jar包
<dependency>
<groupId>javax.jntm</groupId>
<artifactId>lbwnb</artifactId>
<version>2.0</version>
<scope>system</scope>
<systemPath>C://学习资料/4K高清无码/test.jar</systemPath>
</dependency>
可选依赖,默认导入依赖时过滤
排除依赖
通过排除依赖防止添加不必要的依赖
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
继承关系
Maven项目可以继承,实现快速配置
子项目继承父项目的依赖
把dependencies全放入dependencyManagement节点,完全作为依赖统一管理
这样父项目完全作为依赖统一管理,父项目做好依赖配置,子项目只管拿来用
Maven常用命令
- clean命令,执行后会清理整个target文件夹,在之后编写Springboot项目时可以解决一些缓存没更新的问题。
- validate命令可以验证项目的可用性。
- compile命令可以将项目编译为.class文件。
- install命令可以将当前项目安装到本地仓库,以供其他项目导入作为依赖使用
- verify命令可以按顺序执行每个默认生命周期阶段(validate,compile,package等)
测试项目
test命令一键测试test目录下的@Test
打包项目
package命令打包(不含依赖)
含依赖
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.test.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
deploy 发布项目到本地仓库和远程仓库
site 生成当前项目发布站点