文档更新地址:https://gitee.com/zhengqingya/java-developer-document
Maven安装与配置见 https://zhengqing.blog.csdn.net/article/details/83956373
https://maven.apache.org
项目构建管理工具



约定大于配置
D:.
└─demo
│ .gitignore -- git提交代码时忽略指定文件
│ demo.iml -- idea生成的该项目模块配置信息
│ HELP.md
│ pom.xml -- maven配置(包含项目基本信息,如何构建,声明项目依赖等等)
├─.idea -- idea生成的该项目的配置信息,包括历史记录,版本控制信息等。
├─.mvn -- 为工程指定maven版本,统一该项目的开发环境 => 避免开发因版本差异引起的诡异错误 -- tips: 即有此配置可无需单独下载maven
│ └─wrapper
│ maven-wrapper.jar
│ maven-wrapper.properties
│
│ mvnw -- maven liunx脚本 (与上面的`maven-wrapper`关联) ex:`mvnw clean` 类似于单独安装maven后的`mvn clean`
│ mvnw.cmd -- maven windows脚本 ex:`mvnw.cmd clean`
├─src -- 项目源代码
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─zhengqing
│ │ │ └─demo
│ │ │ DemoApplication.java
│ │ │
│ │ └─resources
│ │ └─application.properties
└─target -- 编译文件目录

可删除.mvn&mvnw&mvnw.cmd使用自己安装的maven版本,即我们常见的项目结构
基础命令
# 查看版本
mvn -v
# 清理项目编译后产生的临时目录`target`
mvn clean
# 编译源代码 生成target目录
mvn compile
# 打包
mvn package
# 将指定jar包打包到本地仓库中
mvn install
mvn install:install-file -DgroupId=com.zhengqing -DartifactId=app-demo -Dversion=0.0.1.release -Dfile=/home/soft/app-jar/app-demo-2.0.0.jar -Dpackaging=jar
# 发布到远程仓库,提供给别人下载依赖使用
mvn deploy
项目实战命令
# 跳过单元测试 打包
mvn clean package -Dmaven.test.skip=true
# -f 指定项目路径 ex: 对项目demo进行打包
mvn -f ./demo clean package -Dmaven.test.skip=true
pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.7.2version>
<relativePath/>
parent>
<groupId>com.zhengqinggroupId>
<artifactId>maven-demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>maven-demoname>
<description>maven-demodescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
pom.xml
${MAVEN_HOME}\lib\maven-model-builder-3.8.6.jar\org\apache\maven\model\pom-4.0.0.xml
定义maven的目录结构
<project>
<modelVersion>4.0.0modelVersion>
<repositories>
<repository>
<id>centralid>
<name>Central Repositoryname>
<url>https://repo.maven.apache.org/maven2url>
<layout>defaultlayout>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<id>centralid>
<name>Central Repositoryname>
<url>https://repo.maven.apache.org/maven2url>
<layout>defaultlayout>
<snapshots>
<enabled>falseenabled>
snapshots>
<releases>
<updatePolicy>neverupdatePolicy>
releases>
pluginRepository>
pluginRepositories>
<build>
<directory>${project.basedir}/targetdirectory>
<outputDirectory>${project.build.directory}/classesoutputDirectory>
<finalName>${project.artifactId}-${project.version}finalName>
<testOutputDirectory>${project.build.directory}/test-classestestOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/javasourceDirectory>
<scriptSourceDirectory>${project.basedir}/src/main/scriptsscriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/javatestSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resourcesdirectory>
resource>
resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resourcesdirectory>
testResource>
testResources>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-pluginartifactId>
<version>1.3version>
plugin>
<plugin>
<artifactId>maven-assembly-pluginartifactId>
<version>2.2-beta-5version>
plugin>
<plugin>
<artifactId>maven-dependency-pluginartifactId>
<version>2.8version>
plugin>
<plugin>
<artifactId>maven-release-pluginartifactId>
<version>2.5.3version>
plugin>
plugins>
pluginManagement>
build>
<reporting>
<outputDirectory>${project.build.directory}/siteoutputDirectory>
reporting>
<profiles>
<profile>
<id>release-profileid>
<activation>
<property>
<name>performReleasename>
<value>truevalue>
property>
activation>
<build>
<plugins>
<plugin>
<inherited>trueinherited>
<artifactId>maven-source-pluginartifactId>
<executions>
<execution>
<id>attach-sourcesid>
<goals>
<goal>jar-no-forkgoal>
goals>
execution>
executions>
plugin>
<plugin>
<inherited>trueinherited>
<artifactId>maven-javadoc-pluginartifactId>
<executions>
<execution>
<id>attach-javadocsid>
<goals>
<goal>jargoal>
goals>
execution>
executions>
plugin>
<plugin>
<inherited>trueinherited>
<artifactId>maven-deploy-pluginartifactId>
<configuration>
<updateReleaseInfo>trueupdateReleaseInfo>
configuration>
plugin>
plugins>
build>
profile>
profiles>
project>
pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.7.0version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.zhengqing.commongroupId>
<artifactId>commonartifactId>
<version>1.0.0version>
<scope>systemscope>
<systemPath>${pom.basedir}/src/main/resources/lib/common-1.0.0.jarsystemPath>
dependency>
<dependency>
<groupId>com.zhengqinggroupId>
<artifactId>baseartifactId>
<scope>providedscope>
dependency>
dependencies>
compile默认值,适用于所有阶段,依赖会参与项目的编译、测试和运行阶段,属于强依赖。打包时,会打到包里去,随项目发布。
runtime只在运行时使用。
一般这种类库都是接口与实现相分离的类库,ex: JDBC类库,在编译之时仅依赖相关的接口,在具体的运行之时,才需要具体的mysql、oracle等等数据的驱动程序。 此类的驱动都是为runtime的类库。
test只在测试时使用,用于编译和运行测试代码。不会随项目发布。
provided该依赖可以参与编译、测试和运行等周期,与compile等同。
区别: 在打包时,不需要打进去
system使用上与provided相同,不同之处在于该依赖不从maven仓库中提取,而是从本地文件系统中提取,其会参照systemPath的属性进行提取依赖。
import只能在dependencyManagement中使用,能解决maven单继承问题,import的依赖实际上并不参与依赖传递。
ex: 通过实现多继承,导入SpringBoot和SpringCloud两个父模块的jar包管理
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.7.2version>
parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.7.0version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>2021.0.2version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>

A -> B -> C
compile: A可以使用C的依赖
test/provided: A不可以使用C的依赖
dependency中的type声明引入依赖的类型jar、war、pom等
默认值是jar
pom.xml引入本地jar包
<dependencies>
<dependency>
<groupId>com.zhengqing.commongroupId>
<artifactId>commonartifactId>
<version>1.0.0version>
<scope>systemscope>
<systemPath>${pom.basedir}/src/main/resources/lib/common-1.0.0.jarsystemPath>
dependency>
dependencies>
通过上面方式引入本地jar包后,打包时并不会将本地jar包一起打包,还需要如下配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<includeSystemScope>trueincludeSystemScope>
configuration>
plugin>
plugins>
build>
exclusions
com.alibaba.cloud
spring-cloud-starter-alibaba-seata
io.seata
seata-spring-boot-starter
父工程pom.xml
<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">
<parent>
<artifactId>small-tools-apiartifactId>
<groupId>com.zhengqinggroupId>
<version>1.0.1version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>commonartifactId>
<packaging>pompackaging>
<description>公共模块description>
<modules>
<module>basemodule>
modules>
project>
子工程pom.xml
<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">
<parent>
<artifactId>commonartifactId>
<groupId>com.zhengqinggroupId>
<version>1.0.1version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>baseartifactId>
<name>${project.artifactId}name>
<version>${small-tools-api.project.version}version>
<packaging>jarpackaging>
project>
pom.xml
<properties>
<small-tools-api.project.version>0.0.1small-tools-api.project.version>
properties>
<dependencies>
<dependency>
<groupId>com.zhengqinggroupId>
<artifactId>baseartifactId>
<version>${small-tools-api.project.version}version>
dependency>
dependencies>
如果properties在父工程中定义,那么在子工程可直接引用
<dependencies>
<dependency>
<groupId>com.zhengqinggroupId>
<artifactId>baseartifactId>
<version>${small-tools-api.project.version}version>
dependency>
dependencies>
微服务中父工程pom.xml声明某一依赖并指定版本
dependencyManagement中配置依赖并不会实际引入,只是为了版本管理。
实际引入需要直接在dependencies中添加。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.seatagroupId>
<artifactId>seata-spring-boot-starterartifactId>
<version>1.5.2version>
dependency>
dependencies>
dependencyManagement>
子工程pom.xml引用依赖,无需再指定版本号,即实现全局统一版本管理
<dependencies>
<dependency>
<groupId>io.seatagroupId>
<artifactId>seata-spring-boot-starterartifactId>
dependency>
dependencies>
当统一管理版本时,依赖过多时,可以引入别人封装好的依赖管理
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.7.0version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>2021.0.2version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
这里用maven打包插件举例说明
父工程pom.xml
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>${spring-boot.version}version>
<configuration>
<finalName>${project.build.finalName}finalName>
<includeSystemScope>trueincludeSystemScope>
configuration>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>

子工程若是不想继承父工程的插件可如下配置
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<skip>trueskip>
<finalName>${project.name}finalName>
configuration>
plugin>
plugins>
build>
<repositories>
<repository>
<id>aliyun-reposid>
<name>aliyun-reposname>
<url>https://maven.aliyun.com/nexus/content/groups/public/url>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun-pluginid>
<name>aliyun-pluginname>
<url>https://maven.aliyun.com/nexus/content/groups/public/url>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>falseenabled>
snapshots>
pluginRepository>
pluginRepositories>
maven默认不编译xml文件
此配置可解决mybatis中mapper与xml映射关系不对应问题
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
resource>
resources>
<testResources>
<testResource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
testResource>
testResources>
build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>${spring-boot.version}version>
<configuration>
<finalName>${project.build.finalName}finalName>
<includeSystemScope>trueincludeSystemScope>
configuration>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
指定jdk1.8版本
修改${MAVEN_HOME}\conf\settings.xml
<profiles>
<profile>
<id>jdk1.8id>
<activation>
<activeByDefault>trueactiveByDefault>
<jdk>1.8jdk>
activation>
<properties>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<maven.compiler.compilerVersion>1.8maven.compiler.compilerVersion>
properties>
profile>
profiles>
如果没有上面的全局配置,我们可以通过maven的编译插件来实现
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.1version>
<configuration>
<source>1.8source>
<target>1.8target>
<encoding>UTF-8encoding>
<compilerArgs>
<arg>-parametersarg>
compilerArgs>
configuration>
plugin>
plugins>
build>
即此依赖可有可无,不影响写代码
org.springframework.boot
spring-boot-devtools
runtime
true
依赖D将使用版本1.2
依赖D将使用版本1.1
新建项目maven-plugin-test - pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<artifactId>testartifactId>
<groupId>com.zhengqinggroupId>
<version>1.0.1version>
parent>
<artifactId>maven-plugin-testartifactId>
<packaging>maven-pluginpackaging>
<dependencies>
<dependency>
<groupId>org.apache.mavengroupId>
<artifactId>maven-plugin-apiartifactId>
<version>3.8.6version>
dependency>
<dependency>
<groupId>org.apache.maven.plugin-toolsgroupId>
<artifactId>maven-plugin-annotationsartifactId>
<version>3.6.4version>
<scope>providedscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-plugin-pluginartifactId>
<version>3.5.2version>
plugin>
plugins>
build>
project>
定义插件执行逻辑
@Slf4j
@Mojo(
// 标识
name = "myPlugin"
)
public class MyPlugin extends AbstractMojo {
/**
* 接收使用插件时传递的参数
*/
@Parameter
private String msg;
@Parameter
private List<String> options;
@Parameter(property = "args")
private String args;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
log.info("****** msg:[{}]", this.msg);
log.info("****** options:[{}]", this.options);
log.info("****** args:[{}]", this.args);
}
}
运行mvn clean install将插件安装到本地仓库
在要使用的项目pom.xml中引入
<build>
<plugins>
<plugin>
<groupId>com.zhengqinggroupId>
<artifactId>maven-plugin-testartifactId>
<version>${small-tools-api.project.version}version>
<configuration>
<msg>Hello Worldmsg>
<options>
<option>oneoption>
<option>twooption>
options>
<args>hiargs>
configuration>
<executions>
<execution>
<phase>cleanphase>
<goals>
<goal>myPlugingoal>
goals>
execution>
executions>
plugin>
plugins>
build>
测试

可见
十八、版本仲裁
由于路径相同时先声明者优先原则:依赖B将使用版本1.1;
由于2个版本不同,版本1.1中缺少了版本1.2的xx类;
导致A使用B时由于找不到类出现问题!
IDEA安装插件Maven Helper
Exclude冲突的jar包即可

今日分享语句:
生活简单就迷人,人心简单就幸福;学会简单,其实就是不简单。