仓库机制:为了提高代码的复用性,我们往往将jar包拆成不同的包,供给其他项目去复用代码(即使是微服务也要抽离一个common包,将不同服务之间需要共同用到工具类、实体类复用),所以maven出现了,除了在本地开发中提供这样的包和包之间依赖关系的管理,同样maven还设立了远程仓库的概念,对于一些开发贡献值,可以将代码发布到远程仓库让各种开发者去使用这个依赖包,maven在这一块有着完善的生态体系;
编译代码:maven会手动调用java编译器完成java文件到classes的文件的转换,这中间需要处理很多协调性的东西都是maven帮忙完成的
依赖冲突:以前手动做jar包管理的时候,往往可能在同一个包下出现了类名相同的classes文件,maven会处理整个问题
项目的构建
从本地资源到打包资源
从本地java文件到class文件
从松散文件结构到规范文件结构
管理项目相关的其他内容,比如开发者信息,版本等等
统一管理jar包
在开发中经常需要依赖第三方的包,包与包之间存在依赖关系,版本间还有兼容性问题,有时还里要将旧的包升级或降级,当项目复杂到一定程度时包管理变得非常重要。
仅仅通过jar包的几个属性,就能确定唯一的jar包,在指定的文件pom.xml中,只要写入这些依赖属性,就会自动下载并管理jar包。
(jar最后会进入lib文件目录)
利用groupID+AfricatlID+version确定依赖所在
我们只需要知道顺序,然后以及怎么配置
依赖查找:本地仓库——中央仓库——远程仓库
Ps:
本地仓库:m2文件夹下的依赖
中央仓库:所有网民共享
远程仓库:自己建立的私服
在maven根目录找到conf文件夹,然后编辑setting.xml
<mirrors>
<mirror>
<id>alimavenid>
<name>aliyun mavenname>
<url>http://maven.aliyun.com/nexus/content/groups/public/url>
<mirrorOf>centralmirrorOf>
mirror>
mirrors>
引入中央仓库
<repositories>
<repository>
<id>alimavenid>
<name>aliyun mavenname>
<url>http://maven.aliyun.com/nexus/content/groups/public/url>
<releases>
<enabled>trueenabled>
releases>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
repositories>
在pom中直接配置
<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">
<repositories>
<repository>
<id>companyname.lib1id>
<url>http://download.companyname.org/maven2/lib1url>
repository>
<repository>
<id>companyname.lib2id>
<url>http://download.companyname.org/maven2/lib2url>
repository>
repositories>
project>
validate(校验) 校验项目是否正确并且所有必要的信息可以完成项目的构建过程。
initialize(初始化) 初始化构建状态,比如设置属性值。
sources
generate-sources(生成源代码) 生成包含在编译阶段中的任何源代码。
process-sources(处理源代码) 处理源代码,比如说,过滤任意值。
resources
generate-resources(生成资源文件) 生成将会包含在项目包中的资源文件。
process-resources (处理资源文件) 复制和处理资源到目标目录,为打包阶段最好准备。
compile(编译) 编译项目的源代码。
process-classes(处理类文件) 处理编译生成的文件,比如说对Java class文件做字节码改善优化。
test-sources(
generate-test-sources(生成测试源代码) 生成包含在编译阶段中的任何测试源代码。
process-test-sources(处理测试源代码) 处理测试源代码,比如说,过滤任意值。
test-resources
generate-test-resources(生成测试资源文件) 为测试创建资源文件。
process-test-resources(处理测试资源文件) 复制和处理测试资源到目标目录。
test-compile(编译测试源码) 编译测试源代码到测试目标目录.
process-test-classes(处理测试类文件) 处理测试源码编译生成的文件。
package
test(测试) 使用合适的单元测试框架运行测试(Juint是其中之一)。
prepare-package(准备打包) 在实际打包之前,执行任何的必要的操作为打包做准备。
package(打包) 将编译后的代码打包成可分发格式的文件,比如JAR、WAR或者EAR文件。
integration
pre-integration-test(集成测试前) 在执行集成测试前进行必要的动作。比如说,搭建需要的环境。
integration-test(集成测试) 处理和部署项目到可以运行集成测试环境中。
post-integration-test(集成测试后) 在执行集成测试完成后进行必要的动作。比如说,清理集成测试环境。
verify (验证) 运行任意的检查来验证项目包有效且达到质量标准。
install(安装) 安装项目包到本地仓库,这样项目包可以用作其他本地项目的依赖。
deploy(部署) 将最终的项目包复制到远程仓库中与其他开发者和项目共享。
通过dependencies标签,按照依赖的查找顺序进行查找;
注意,如果是本地package的话,一定要记得重新install子包的依赖,不然可能会报编译错误
<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">
<dependencies>
<dependency>
<groupId>com.jmgroupId>
<artifactId>jm-public-common_serviceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.jmgroupId>
<artifactId>jm-public-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
project>
父工程的一切依赖配置被子工程复制一份,也可以重写
<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>
<groupId>com.jmgroupId>
<artifactId>jm-rootartifactId>
<version>1.0-SNAPSHOTversion>
<relativePath>../pom.xmlrelativePath>
parent>
project>
<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>
<groupId>asia.banseongroupId>
<artifactId>banseon-maven2 artifactId>
<packaging>jarpackaging>
<version>1.0-SNAPSHOTversion>
<name>banseon-mavenname>
<url>http://www.baidu.com/banseonurl>
<description>A maven project to study maven. description>
<prerequisites>
<maven>2.0.3maven>
prerequisites>
project>
用于更复杂的一些pom配置的管理
声明环境
<profiles>
<profile>
<id>alwaysActiveProfileid>
<activation>
<activeByDefault>activeByDefault>
<jdk>jdk>
<os>
<name>Windows 7name>
<family>Windowsfamily>
<arch>x64arch>
<version>7.2.3580version>
os>
<property>
<name>mavenVersionname>
<value>2.0.3value>
property>
<file>
<exists>/usr/local/xbz/workspace/exists>
<missing>/usr/local/xbz/workspace/missing>
file>
activation>
<dependencies>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-extensionartifactId>
<version>3.3.2version>
dependency>
dependencies>
profile>
profiles>
激活环境
<activeProfiles>
<activeProfile>alwaysActiveProfileactiveProfile>
<activeProfile>anotherAlwaysActiveProfileactiveProfile>
activeProfiles>
定义变量
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<developer.organization>developer.organization>
<spring.version>1.2.6spring.version>
properties>
使用变量
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
内置变量
${basedir} 项目根目录
${project.build.directory} 构建目录,缺省为target
${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes
${project.build.finalName} 产出物名称,缺省为${project.artifactId}-${project.version}
${project.packaging} 打包类型,缺省为jar
${project.xxx} 当前pom文件的任意节点的内容
${settings.localRepository} settings文件
${user.home} java
<dependencies>
<dependency>
<groupId>org.apache.mavengroupId>
<artifactId>maven-artifactartifactId>
<version>3.8.1version>
<type>jartype>
<scope>testscope>
<exclusions>
<exclusion>
<artifactId>spring-coreartifactId>
<groupId>org.springframeworkgroupId>
exclusion>
exclusions>
<optional>trueoptional>
dependency>
dependencies>
<build>
<directory />
<finalName />
<filters />
<sourceDirectory />
<scriptSourceDirectory />
<testSourceDirectory />
<outputDirectory />
<testOutputDirectory />
<extensions>
<extension>
<groupId />
<artifactId />
<version />
extension>
extensions>
<resources>
<resource>
<targetPath />
<filtering />
<directory />
<includes />
<excludes />
resource>
resources>
<testResources>
<testResource>
<targetPath />
<filtering />
<directory />
<includes />
<excludes />
testResource>
testResources>
<plugins>
<plugin>
<groupId />
<artifactId />
<version />
<extensions />
<executions>
<execution>
<id />
<phase />
<goals />
<inherited />
<configuration />
execution>
executions>
<dependencies>
<dependency>
......
dependency>
dependencies>
<goals />
<inherited />
<configuration />
plugin>
plugins>
build>
构建主要是调用maven插件完成一系类的生命周期
父类中声明
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
<scope>testscope>
dependency>
dependencies>
dependencyManagement>
子类中引用
)1,不需要指定版本号和作用域,直接引用父类的
)2,如果指定了,那就会子类被重写
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
dependency>
dependencies>
插件的约束
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId />
<artifactId />
<version />
<extensions />
<executions>
<execution>
<id />
<phase />
<goals />
<inherited />
<configuration />
execution>
executions>
<dependencies>
<dependency>
......
dependency>
dependencies>
<inherited />
<configuration />
plugin>
plugins>
pluginManagement>
build>
插件开发请关注maven插件系列
主要是基于一定的业务需要,要干预或者增强打包的功能所运用的插件;
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-jar-pluginartifactId>
<configuration>
<excludes>
<exclude>applicationContext.xmlexclude>
<exclude>properties/**exclude>
<exclude>log4j.propertiesexclude>
excludes>
configuration>
plugin>
<plugin>
<groupId>org.codehaus.mojogroupId>
<artifactId>tomcat-maven-pluginartifactId>
<version>1.1version>
<configuration>
<path>/wppath>
<port>8080port>
<uriEncoding>UTF-8uriEncoding>
<url>http://localhost:8080/manager/htmlurl>
<server>tomcat6server>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-war-pluginartifactId>
<configuration>
<warName>demo-RestwarName>
<webResources>
<resource>
<directory>src/main/webapp/WEB-INFdirectory>
<filtering>truefiltering>
<targetPath>WEB-INFtargetPath>
<includes>
<include>web.xmlinclude>
includes>
resource>
webResources>
configuration>
plugin>
<plugin>
<groupId>org.codehaus.mojogroupId>
<artifactId>properties-maven-pluginartifactId>
<version>1.0.0version>
<configuration>
<files>
<file>profiles/${runtime.env}/jdbc.propertiesfile>
<file>profiles/${runtime.env}/redis.propertiesfile>
<file>profiles/${runtime.env}/batch.propertiesfile>
<file>profiles/${runtime.env}/config.propertiesfile>
files>
configuration>
<executions>
<execution>
<phase>initializephase>
<goals>
<goal>read-project-propertiesgoal>
goals>
execution>
executions>
plugin>
plugins>```