maven的pom标签说明:

https://developer.aliyun.com/article/983957

maven的setting.xml标签说明:

https://developer.aliyun.com/article/983958

maven使用说明:

https://www.cnblogs.com/melojun/p/15478774.html

https://mvnrepository.com/ –仓库官网地址

maven的生命周期

1
2
3
4
5
6
7
8
9
clean : 清理.删除上次构建的结果,为下一次构建做准备
validate : 校验. 验证正确性
complie : 编译. 将java文件编译成字节码文件
test : 测试. 执行可执行的单元测试方法
package : 打包. 将程序包打包成jar包或war包
verify : 验证.
install : 安装. 将jar包安装到本地的maven仓库
site : 站点. 将对应程序包生成网页站点
deploy : 部署. 将jar包部署到远程私服仓库

maven指令

mvn(主命令) archetype(插件):generate(目标)

如图:

maven坐标,三个向量加起来就能定位到仓库中唯一的jar包

groupId(公司或组织的id)、artifactId(一个项目或项目中的一个模块id)、version(版本号)

maven核心概念: pom.xml

maven约定的工程目录结构(约定大于配置,配置大于编码),如图:

maven创建web工程,执行执行下面指令即可

1
2
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes 
-DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.4

依赖范围对应设置的枚举值

scope值 main-compile test-compile package transfer dependencyManagement outer
compile(default) y y y y n n
provided y y n n n n
runtime n n y n n n
test n y n n n n
system n n n n n y
import n n n n y n

import应用的例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<dependencyManagement>
<dependencies>

<!-- SpringCloud 依赖导入 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR9</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<!-- SpringCloud Alibaba 依赖导入 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<!-- SpringBoot 依赖导入 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

system应用的例子

1
2
3
4
5
6
7
<dependency>
<groupId>com.atguigu.maven</groupId>
<artifactId>atguigu-maven-test-aaa</artifactId>
<version>1.0-SNAPSHOT</version>
<systemPath>D:\temp\atguigu-maven-test-aaa-1.0-SNAPSHOT.jar</systemPath>
<scope>system</scope>
</dependency>

父子工程的继承关系(作用,父工程统一定义所有jar包的依赖管理)

父package的标签值必须为pom. package的枚举值:jar、war、ear、pom

工程聚合,优势如下:

  • 一键执行 Maven 命令:很多构建命令都可以在“总工程”中一键执行。

  • 以 mvn install 命令为例:Maven 要求有父工程时先安装父工程;有依赖的工程时,先安装被依赖的工程。我们自己考虑这些规则会很麻烦。但是工程聚合之后,在总工程执行 mvn install 可以一键完成安装,而且会自动按照正确的顺序执行。

  • 配置聚合之后,各个模块工程会在总工程中展示一个列表,让项目中的各个模块一目了然。

pom之间的层级

超级pom->父pom->当前pom->有效pom

超级pom的默认设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<project>
<modelVersion>4.0.0</modelVersion>

<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>

<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
<pluginManagement>
<!-- NOTE: These plugins will be removed from future versions of the super POM -->
<!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<reporting>
<outputDirectory>${project.build.directory}/site</outputDirectory>
</reporting>

<profiles>
<!-- NOTE: The release profile will be removed from future versions of the super POM -->
<profile>
<id>release-profile</id>

<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>

<build>
<plugins>
<plugin>
<inherited>true</inherited>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<updateReleaseInfo>true</updateReleaseInfo>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>

build标签详解

各个子标签的含义:

目录名 作用
sourceDirectory 主体源程序存放目录
scriptSourceDirectory 脚本源程序存放目录
testSourceDirectory 测试源程序存放目录
outputDirectory 主体源程序编译结果输出目录
testOutputDirectory 测试源程序编译结果输出目录
resources 主体资源文件存放目录
testResources 测试资源文件存放目录
directory 构建结果输出目录

自定义插件,这个使用场景很少就不列举了,具体查看

http://heavy_code_industry.gitee.io/code_heavy_industry/pro002-maven/chapter09/verse06.html

具体参见博客:

http://heavy_code_industry.gitee.io/code_heavy_industry/pro002-maven/

  1. mac设置idea自带maven插件的环境变量

mac的idea自带的maven路径:

/Applications/IntelliJ\ IDEA.app/Contents/plugins/maven/lib/maven3

1.在配置环境变量时,空格识别不了;所以把空格改成下划线

2.在~/.zprofile文件下配置环境变量

1
2
3
4
5
#Setting PATH for MAVEN
MAVEN_HOME=/Applications/IntelliJ_IDEA.app/Contents/plugins/maven/lib/maven3
PATH=$MAVEN_HOME/bin:$PATH
export MAVEN_HOME
export PATH

3.执行生效命令 resource .zprofile

4.执行mvn -v,发现

1
zsh: permission denied: mvn

没有权限

5.给idea maven路径授予执行权限

1
chmod a+x /Applications/IntelliJ_IDEA.app/Contents/plugins/maven/lib/maven3/bin/mvn

6.再次执行mvn -v,发现环境变量配置成功

  1. mvn reimport 操作提示:

Cannot access nexus-aliyun (https://maven.aliyun.com/repository/public) in offline mode原因

问题是因为idea开启了offine mode,关闭该模式再次刷新即可,如图:

2.本地仓库使用maven指令打包

1
2
3
4
5
6
7
8
9
mvn install:install-file -DgroupId=com.oracle.jdbc -DartifactId=ojdbc8 -Dversion=12.2.0.1 -Dpackaging=jar -Dfile=/Users/xiaotian/Documents/剑阁/桌面内容/通用需求/ojdbc8-12.2.0.1.jar

mvn install:install-file -DgroupId=com.saicgmac.muse -DartifactId=muse-security -Dversion=2.4.0 -Dpackaging=jar -Dfile=/Users/xiaotian/Documents/剑阁/桌面内容/通用需求/muse-security-2.4.0.jar

mvn install:install-file -DgroupId=com.saicgmac.muse -DartifactId=muse-test -Dversion=2.4.0 -Dpackaging=jar -Dfile=/Users/xiaotian/Documents/剑阁/桌面内容/通用需求/muse-test-2.4.0.jar

mvn install:install-file -DgroupId=com.saicgmac.muse -DartifactId=muse-core -Dversion=2.4.0 -Dpackaging=jar -Dfile=/Users/xiaotian/Documents/剑阁/桌面内容/通用需求/muse-core-2.4.0.jar

mvn install:install-file -DgroupId=org.pbccrc.collectclient -DartifactId=client-collect-api -Dversion=0.5.3.0 -Dpackaging=jar -Dfile=/Users/xiaotian/Documents/剑阁/桌面内容/通用需求/client-collect-api-0.5.3.0.jar

mvn install:install-file -DgroupId=com.oracle.jdbc -DartifactId=ojdbc8 -Dversion=12.2.0.1 -Dpackaging=jar -Dfile=/Users/xiaotian/Documents/剑阁/桌面内容/通用需求/ojdbc8-12.2.0.1.jar

maven其他笔记补充:

1.设置中央仓库

<!– 中央仓库1 –>
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>https://repo1.maven.org/maven2/</url>
</mirror>
<!– 中央仓库2 –>
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>https://repo2.maven.org/maven2/</url>
</mirror>
<!– 阿里云地址 –>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

2.maven常用命令

1.mvn tomcat:run 打包运行编译文件

2.mvn clean 清理,编译后的目录

3.mvn compole 编译,只编译main目录,不编译test中的代码

4.mvn test-compile 编译test目录中的代码

5.mvn test 编译test里面的代码

6.mvn package 打包

根据 artifactId+version+package生成

7.mvn install 发布项目到本地仓库

4.maven命令的生命周期

1.clean: 生命周期clean

2.default:生命周期 compile、test-compile、test、package、install

3.site:生命周期 site

不同的生命周期可以一起执行,例如先clean再compile

5.命令的执行顺序

compile<test-compile<test<package<install

6.maven手动添加插件

生成maven索引

maven编译jdk目录

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.1</version>

<configuration>

    <source>1.8</source>

    <target>1.8</target>

    <encoding>UTF-8</encoding>

    <showWarnings>true</showWarnings>

</configuration>

tomcat插件官网查询;

7.scope属性:

compile:编译 测试 运行 打包 都有效

provide:编译 测试 有效 运行 打包无效

test: 测试 有效 编译 运行 打包 无效

runtime: 编译 无效 测试运行打包有效

8 maven 3.5.4+tomcat 8.5.32 热部署

修改tomcat-user.xml配置文件

9.resources资源放行

src/main/java

**/*.properties

**/*.xml

10.maven 子项目依赖父项目

子项目

<artifactId>paychannel</artifactId>

<groupId>com.internet.paychannel</groupId>

<version>1.1.5-SNAPSHOT</version>

paychannel-biz

父项目

com.internet.paychannel

paychannel

1.1.5-SNAPSHOT

<module>paychannel-api</module>

<module>paychannel-biz</module>

<module>paychannel-client</module>

11.maven的依赖传递,与依赖版本管理

1.依赖传递

2.依赖冲突

解决依赖冲突办法:

1.直接申明明确的版本号

2.排除jar包

<exclusion>

    <artifactId>guava</artifactId>

    <groupId>com.google.guava</groupId>

</exclusion>

12 dependencyManagement 生命依赖不是真正引入依赖,进行依赖版本锁定

13:nexus远程仓库建立

1.下载

2.安装 install

3.启动 start

14.maven连接远程仓库

<repository>

    <id>nexus</id>

    <name>Releases</name>

    <url>http://10.0.2.102:8081/nexus/content/repositories/releases/</url>

</repository>

<snapshotRepository>

    <id>nexus</id>

    <name>Snapshots</name>

    <url>http://10.0.2.102:8081/nexus/content/repositories/snapshots/</url>

</snapshotRepository>

配置权限

nexus

admin

admin123

上传jar包

mvn deploy

maven-解决打包jar到本地仓库

maven编译jar包到本地

mvn install:install-file -DgroupId=com.saicgmac.muse -DartifactId=muse-core -Dversion=2.4.0 -Dpackaging=jar -Dfile=C:\Users\jiange\Desktop\通用需求\muse-core-2.4.0.jar

mvn install:install-file -DgroupId=org.pbccrc.collectclient -DartifactId=client-collect-api -Dversion=0.5.1.0-SNAPSHOT -Dpackaging=jar -Dfile=C:\Users\jiange\Desktop\client-collect-api-0.5.1.0.jar

mvn install:install-file -DgroupId=com.thoughtworks.xstream -DartifactId=xstream -Dversion=1.4.15 -Dpackaging=jar -Dfile=C:\Users\jiange\Desktop\xstream-1.4.15.jar

#server 153 maven的执行命令

/java/maven/apache-maven-3.5.4/bin/mvn install:install-file -DgroupId=com.jslsolucoes -DartifactId=ojdbc6 -Dversion=11.2.0.1.0 -Dpackaging=jar -Dfile=/root/ojdbc6-11.2.0.1.0.jar

/java/maven/apache-maven-3.5.4/bin/mvn install:install-file -DgroupId=com.jslsolucoes -DartifactId=ojdbc6 -Dversion=11.2.0.1.0 -Dpackaging=jar -Dfile=/home/java/ojdbc6-11.2.0.1.0.jar

mvn install:install-file -DgroupId=cn.smallbun.screw -DartifactId=screw-core -Dversion=1.0.5 -Dpackaging=jar -Dfile=/Users/xiaotian/workspace/gitee/screw-v1.0.5/screw-core/target/screw-core-1.0.5.jar

mvn install:install-file -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc42 -Dversion=6.0.8112 -Dpackaging=jar -Dfile=/Users/xiaotian/Downloads/sqljdbc42-6.0.8112.jar

/Users/xiaotian/workspace/jiange/svn/huamei/FrontInterface_V1.2.0/WebContent/WEB-INF/lib/sqljdbc42.jar

1
2
3
4
5
<dependency>
<groupId></groupId>
<artifactId>screw-core</artifactId>
<version>${lastVersion}</version>
</dependency>

mvn install:install-file -DgroupId=com.saicgmac.muse -DartifactId=muse-security -Dversion=2.4.0 -Dpackaging=jar -Dfile=C:\Users\jiange\Desktop\通用需求\muse-security-2.4.0.jar

mvn install:install-file -DgroupId=com.saicgmac.muse -DartifactId=muse-test -Dversion=2.4.0 -Dpackaging=jar -Dfile=C:\Users\jiange\Desktop\通用需求\muse-test-2.4.0.jar

mvn install:install-file -DgroupId=com.jslsolucoes -DartifactId=ojdbc8 -Dversion=12.2.0.1 -Dpackaging=jar -Dfile=C:\Users\jiange\Desktop\通用需求\ojdbc8-12.2.0.1.jar

mvn install:install-file -DgroupId=com.jslsolucoes -DartifactId=ojdbc6 -Dversion=11.2.0.1.0 -Dpackaging=jar -Dfile=C:\Users\Administrator\Desktop\ojdbc6.jar

mac 通用二代征信项目把jar包编译到本地仓库

mvn install:install-file -DgroupId=com.jslsolucoes -DartifactId=ojdbc8 -Dversion=12.2.0.1 -Dpackaging=jar -Dfile=/Users/xiaotian/Documents/剑阁/桌面内容/通用需求/ojdbc8-12.2.0.1.jar

mvn install:install-file -DgroupId=com.saicgmac.muse -DartifactId=muse-security -Dversion=2.4.0 -Dpackaging=jar -Dfile=/Users/xiaotian/Documents/剑阁/桌面内容/通用需求/muse-security-2.4.0.jar

mvn install:install-file -DgroupId=com.saicgmac.muse -DartifactId=muse-test -Dversion=2.4.0 -Dpackaging=jar -Dfile=/Users/xiaotian/Documents/剑阁/桌面内容/通用需求/muse-test-2.4.0.jar

mvn install:install-file -DgroupId=com.saicgmac.muse -DartifactId=muse-core -Dversion=2.4.0 -Dpackaging=jar -Dfile=F:\桌面内容\通用需求\muse-core-2.4.0.jar

mvn install:install-file -DgroupId=org.pbccrc.collectclient -DartifactId=client-collect-api -Dversion=0.5.3.0 -Dpackaging=jar -Dfile=F:\桌面内容\通用需求\client-collect-api-0.5.3.0.jar

项目引入lombok maven编译出错

循环 1:

输入文件: {com.geping.etl.DFEastBus.repository.dataManage.finance.NetWorksRepository

注释: [org.springframework.stereotype.Repository

最后一个循环: false

解决 加大maven的vm内存

mac maven打包地址

mvn install:install-file -DgroupId=it.sauronsoftware -DartifactId=javabase64 -Dversion=1.3.1 -Dpackaging=jar -Dfile=/Users/xiaotian/Downloads/javabase64-1.3.1/javabase64-1.3.1.jar

mvn site指令含义:

Apache Maven Site Plugin是Maven项目提供的标准插件之一,用于为Maven项目生成站点(用以生成HTML页面的模块等文档),如果在pom.xml中配置了则其中也包括项目的报告

本地测试打包产生的相关文件信息,如图:

参考博客:

https://blog.csdn.net/taiyangdao/article/details/53909320

–待学习的博客

https://www.cnblogs.com/jingmoxukong/p/6050172.html

https://blog.csdn.net/weixin_43740223/article/details/114253461

https://juejin.cn/post/7035620309915058207