maven基础
# maven的安装与配置
# maven安装
maven下载地址 (opens new window) maven中央仓库 (opens new window)
系统变量:
M2_HOME = d:\maven\apache-maven-3.8.1
系统变量:
path = %M2_HOME%\bin
# setting.xml文件
1、指定存放目录
2、修改maven远程下载地址
3、指定jdk默认版本
<?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">
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
</servers>
<!-- 保存到本地的目录 -->
<localRepository>d:/maven/newrepo</localRepository>
<mirrors>
<!-- 阿里云仓库 -->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>*,!jeecg,!jeecg-snapshots</mirrorOf>
</mirror>
</mirrors>
<profiles>
<!-- 指定全局jdk版本及编码 -->
<profile>
<id>jdk-1.8</id>
<activation>
<jdk>1.8</jdk>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
</settings>
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
# 1、仓库的概念
本地仓库: 本地存放jar包的地方,默认存放路径在路径名为 .m2/respository/ ,可以再settings.xml中配置。 中央仓库: Maven 中央仓库是由 Maven 社区提供的仓库,其中包含了大量常用的库。中央仓库包含了绝大多数流行的开源Java构件,以及源码、作者信息、SCM、信息、许可证信息等。一般来说,简单的Java项目依赖的构件都可以在这里下载到。 nexus私服: 开发人员自己定制仓库,包含了所需要的代码库或者其他工程中用到的 jar 文件。一般公司都会搭建一个自己的私服,加快jar包下载速度,也可以deploy公司内部的jar包。
# maven repository 与镜像地址
http://mvnrepository.com (opens new window) maven 仓库用于查找所需要pom项目
http://repo1.maven.org/maven2/ 全球总仓库1
http://repo2.maven.org/maven2/ 全球总仓库2
http://maven.aliyun.com/nexus/content/groups/public/ 阿里云镜像仓库
http://maven.oschina.net/content/groups/public/ oschina 镜像仓库
镜像配置
修改settings.xml 文件
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!-- 中央仓库1 -->
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo1.maven.org/maven2/</url>
</mirror>
<!-- 中央仓库2 -->
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
</mirrors>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 2、pom详解
gav: 引入依赖的坐标
scope: 作用域
- test: test范围依赖在一般的编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用。
- compile: 编译时,默认;
- provider: 编译时生效,但不会被打包进去。
- runtime: 依赖在运行和测试系统的时候需要,但在编译的时候不需要。比如,你可能在编译的时候只需要JDBC API JAR,而只有在运行的时候才需要JDBC驱动实现。
- system: system范围依赖与provided类似,但是你必须显式的提供一个对于本地系统中JAR文件的路径。这么做是为了允许基于本地对象编译,而这些对象是系统类库的一部分。这样的构建应该是一直可用的,Maven 也不会在仓库中去寻找它。如果你将一个依赖范围设置成系统范围,你必须同时提供一个systemPath元素。注意该范围是不推荐使用的(建议尽量去从公共或定制的 Maven 仓库中引用依赖)
- import: 仅支持在
<dependencyManagement>
中的类型依赖项上。它表示要在指定的POM<dependencyManagement>
部分中用有效的依赖关系列表替换的依赖关系。该scope类型的依赖项实际上不会参与限制依赖项的可传递性。
<!--spring cloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
2
3
4
5
6
7
8
# scope的依赖传递:
scope是test或者provided,不会往下传递
参考 : maven:pom文件详解_夜雨落花的博客-CSDN博客_pom文件 (opens new window)
# maven常用命令
备注 | |
---|---|
mvn -v | --version 显示版本信息; |
mvn -s | --settings arg 用户配置文件的备用路径; |
mvn -f | --file file 强制使用备用的POM文件; |
mvn -U | 强制更新snapshot类型的插件或依赖库(否则maven一天只会更新一次snapshot依赖); |
mvn -Dxxx=yyy | 指定Java全局属性; 如跳过测试: -Dmaven.test.skip=false 指定test: -Dtest=com.jeesite.test.InitData |
mvn install | 打包并上传到本地仓库 |
mvn clean | 清理 |
mvn compile | 编译 |
mvn test | 测试 |
mvn package | 打包 |
mvn install | 打包并上传到本地仓库 |
mvn depeloy | 上传到远程仓库 |
mvn -Dmaven.test.skip=true | 跳过测试 |
# maven的生命周期
阶段 | 处理 | 描述 |
---|---|---|
验证 validate | 验证项目 | 验证项目是否正确且所有必须信息是可用的 |
编译 compile | 执行编译 | 源代码编译在此阶段完成 |
测试 Test | 测试 | 使用适当的单元测试框架(例如JUnit)运行测试。 |
包装 package | 打包 | 创建JAR/WAR包如在 pom.xml 中定义提及的包 |
检查 verify | 检查 | 对集成测试的结果进行检查,以保证质量达标 |
安装 install | 安装 | 安装打包的项目到本地仓库,以供其他项目使用 |
部署 deploy | 部署 | 拷贝最终的工程包到远程仓库中,以共享给其他开发人员和工程 |
当maven运行到生命周期的某个阶段,其之前的阶段都会自动按序执行
参考: Maven完整生命周期 – Introduction to the Build Lifecycle (apache.org) (opens new window)
# 7、SNAPSHOT
在框架持续开发期间,如果该框架被多个项目依赖,每次升级一个版本都要通知被调用的人,版本升级给调用者会带来极大的不便,采用SNAPSHOT可以避免此问题。 正式版本和快照版本的主要区别在于,本地获取这些依赖的机制有所不同。假设你依赖一个库的正式版本,构建的时候构建工具会先在本次仓库中查找是否已经有了这个依赖库,如果没有的话才会去远程仓库中去拉取。 快照版本会每隔一定时间去远程仓库拉去看有没有更新的变化。频率共有四种,分别是always、daily、interval(分钟为单位)、never(跟正式版本一样)。
# maven指定setting运行
mvn命令会默认调用
M2_HOME
里面setting.xml
。 但是有一些项目中,需要单独地设置setting.xml
默认C:\Users\Administrator\.m2\setting.xml
mvn clean compile -Dmaven.test.skip=true -s "E:\ideaWorkspace\zxpnet\jeesite4\web\bin\settings.xml"
# 安装jar包到本地仓库
有些jar包,需要手动安装到本地,如oracle的驱动包
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dfile=ojdbc6-11.2.0.1.0.jar -Dpackaging=jar
然后在maven里面添加依赖,注意GAV
的名称
# maven插件
官方Maven插件库 – Available Plugins (apache.org) (opens new window)
# 插件结构
# phase
每个生命周期中都包含着一系列的阶段(phase)。这些 phase 就相当于 Maven 提供的统一的接口,然后这些 phase 的实现由 Maven 的插件来完成。
我们在输入 mvn 命令的时候 比如 mvn clean
,clean 对应的就是 Clean 生命周期中的 clean 阶段。但是 clean 的具体操作是由 maven-clean-plugin 来实现的。
所以说 Maven 生命周期的每一个阶段的具体实现都是由 Maven 插件实现的。
# goal
目标,不同的插件文档里面有说明
mvn [plugin-name]:[goal-name]
如: mvn package spring-boot:repackage
2
插件 | 描述 |
---|---|
clean | 构建之后清理目标文件。删除目标目录。 |
compiler | 编译 Java 源文件。 |
surefile | 运行 JUnit 单元测试。创建测试报告。 |
jar | 从当前工程中构建 JAR 文件。 |
war | 从当前工程中构建 WAR 文件。 |
javadoc | maven-javadoc-plugin 为工程生成 Javadoc。 |
antrun | maven-antrun-plugin 从构建过程的任意一个阶段中运行一个 ant 任务的集合。 |
assembly | 支持定制化打包方式,例如 apache 项目的打包方式 |
maven-shade-plugin | 用来打可执行包,executable(fat) jar |
maven--插件篇(assembly插件) - SegmentFault 思否 (opens new window)
# 打包源码的插件
Apache Maven Source Plugin – Usage (opens new window)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase> <!-- 何时执行,对应生命周期 -->
<goals>
<goal>jar-no-fork</goal> <!-- 做什么,插件自己定义的目标 -->
</goals>
</execution>
</executions>
</plugin>
2
3
4
5
6
7
8
9
10
11
12
13
14
# 测试Surefire插件
默认会对包含Test的类进行测试,
Maven Surefire Plugin – surefire:test (apache.org) (opens new window)
# maven-resources-plugin
结合 resources使用; pom里面定义的值,可以在application.yml里面引用了
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
2
3
4
5
6
7
8
9
10
11
在多项目构建中,将source-plugin置于顶层或parent的pom中并不会发挥作用,必须置于具体项目的pom中
参考文章: 利用 maven-resources-plugin 插件,用表达式替换 resources 文件中的内容 - 简书 (jianshu.com) (opens new window)
Maven学习(四) - 插件maven-resources-plugin_u012501054的博客-CSDN博客_maven-resources-plugin (opens new window)
# springboot-maven-plugin
Spring Boot Maven plugin
的 5个 Goals:
- repackage,默认 goal。在 mvn package 之后,再次打包可执行的 jar/war,同时保留 mvn package 生成的 jar/war 为 .origin
- run,运行 SpringBoot 应用
- start,在 mvn integration-test 阶段,进行 SpringBoot 应用生命周期的管理
- stop,在 mvn integration-test 阶段,进行 SpringBoot 应用生命周期的管理
- build-info,生成 Actuator 使用的构建信息文件 build-info.properties
一般在父pom中不指定goal,具体要打包的项目中,写goal
// 父pom中
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 如果没有该项配置,则devtools不会起作用,即应用不会restart -->
<fork>true</fork>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 需要将lib打包到jar中,则这样写:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 将依赖的lib打进去 -->
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
2
3
4
5
6
7
8
9
10
11
12
13
如common-core
子项目,就可以不引入springboot-maven-plugin
插件,会自动继承父级pom,父级打包会自动将它聚合的子项目都打包,在聚合helloworld项目时,其maven插件的goal为repackage,,则将它依赖的jar打开其 BOOT-INF/lib/
下
# 打包时,如何将jar和lib分离
从上面的分析,每次repackage,都会将依赖的jar打进jar包,如果部署的话,一般lib很少变动,这样就很不方便。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--<configuration>
<mainClass>com.zxp.springboot.Hello01Application</mainClass>
<layout>ZIP</layout>
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
</includes>
</configuration>-->
<!-- 将依赖的lib打进去 -->
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 打jar包的插件,设定manifest的参数,比如指定运行的Main class,还有依赖的jar包,加入classpath中 -->
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
<mainClass>com.zxp.springboot.Hello01Application</mainClass>
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>-->
<!--设置将 lib 拷贝到应用 Jar 外面-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
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
nohup java -jar -Dloader.path=lib -Dspring.profiles.active=prod -Dserver.port=18080 gmzx-web-audit.jar &
tail -f nohup.out #查看启动日志
jps #查看java进程
netstat -anp |grep 18080 #通过端口查看进程号
2
3
4
5
参考文章:
springboot 打 jar 包分离依赖 lib 和 配置文件 (opens new window)
J2eeFAST 如何打包部署Linux运行? · 语雀 (yuque.com) (opens new window)
maven-assembly-plugin插件 (opens new window)
# 聚合与继承
应用场景:聚合主要为了快速构建项目,表示项目与子项目之间的关系,继承主要为了消除重复
当对父工程进行install的时候,会将聚合的工程进行构建。 聚合项目知道哪些项目是它的子项目,但是那些被聚合的项目并不知道其被哪个模块聚合了。
对于继承关系的父 POM 来说,它不知道哪些子模块继承于它,但那些子模块都必须知道自己的父 POM 是什么。
实际项目中,大家会发现一个 pom 即是聚合 pom,又是父 pom,这么做主要是为了方便
像 dependencies 这样可以被子类继承的元素还有下面几个元素:
- groupId
- version
- description
- organization
- inceptionYear
- url
- developers
- contributors
- distributionManagement
- issueManagement
- ciManagement
- scm
- mailingLists
- properties
- dependencies
- dependencyManagement
- repositories
- build
- reporting