1)数学中的坐标
使用 x、y、z 三个『向量』作为空间的坐标系,可以在『空间』中唯一的定位到一个『点』
2)Maven中的坐标
jar包
超级POM
<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>
父 POM
和 Java 类一样,POM 之间其实也是单继承的。如果我们给一个 POM 指定了父 POM,那么继承关系如下图所示:
当前POM
<!-- 当前Maven工程的坐标 -->
<groupId>com.atguigu.maven</groupId>
<artifactId>pro01-maven-java</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 当前Maven工程的打包方式,可选值有下面三种: -->
<!-- jar:表示这个工程是一个Java工程 -->
<!-- war:表示这个工程是一个Web工程 -->
<!-- pom:表示这个工程是“管理其他工程”的工程 -->
<packaging>jar</packaging>
<name>pro01-maven-java</name>
<url>http://maven.apache.org</url>
<properties>
<!-- 工程构建过程中读取源码时使用的字符集 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 当前工程所依赖的jar包 -->
<dependencies>
<!-- 使用dependency配置一个具体的依赖 -->
<dependency>
<!-- 在dependency标签内使用具体的坐标依赖我们需要的一个jar包 -->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!-- scope标签配置依赖的范围 -->
<scope>test</scope>
</dependency>
</dependencies>
有效 POM
所有 POM 叠加到一起,就得到了一个最终生效的 POM
综上所述,平时我们使用和配置的 POM 其实大致是由四个层次组成的:
另外还有一个 target 目录专门存放构建操作输出的结果
作用域scope | 编译 | 测试 | 运行 | 打包 | 举例 |
---|---|---|---|---|---|
compile(默认) | ✅ | ✅ | ✅ | ✅ | spring-core |
test | ❌ | ✅ | ❌ | ❌ | junit |
provided | ✅ | ✅ | ❌ | ❌ | sevlet-api、lombok |
runtime | ❌ | ✅ | ✅ | ✅ | jdbc驱动 |
system | ✅ | ✅ | ❌ | ❌ | 本地包 |
默认
会使用该依赖范围运行时无效
编译主代码时无效
<systemPath>
使用<includeSystemScope>true</includeSystemScope>
,否则可执行jar中没有<scope>system</scope>
的依赖<dependency>
<groupId>com.xc.maven</groupId>
<artifactId>xc-maven-test</artifactId>
<version>1.0-SNAPSHOT</version>
<systemPath>${project.basedir}/src/main/resources/lib/open-api-sdk-2.0.jar</systemPath>
<scope>system</scope>
</dependency>
<!--配合打包插件配置-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
<type>pom</type>
用法一:
<scope>import</scope>
和 <type>pom</type>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.1.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
用法二:
<type>pom</type>
<dependencies>
<dependency>
<groupId>org.sonatype.mavenbook</groupId>
<artifactId>persistence-deps</artifactId>
<version>1.0</version>
<type>pom</type>
</dependency>
</dependencies>
<!--热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
传递的原则
举例:
一个工程,三个模块,install到本地仓库
第一步:project1依赖project2
<dependencies>
<dependency>
<groupId>com.xc</groupId>
<artifactId>project2</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
第二步:project2依赖project3
<dependencies>
<dependency>
<groupId>com.xc</groupId>
<artifactId>project3</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
第三步:查看project1所有的依赖
project2直接依赖,project3间接依赖
修改第二步,project2依赖project3的范围
<dependencies>
<dependency>
<groupId>com.xc</groupId>
<artifactId>project3</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
第三步:查看project1所有的依赖
此时只有直接依赖project2
首要判断:最短路径优先
在下图的例子中,对模块 pro25-module-a 来说,Maven 会采纳 1.2.12 版本
再者判断:路径相同时先声明者优先
此时 Maven 采纳哪个版本,取决于在 pro29-module-x 中,对 pro30-module-y 和 pro31-module-z 两个模块的依赖哪一个先声明
概念
配置方式
project1依赖project2,project2依赖proejct3,依赖默认范围compile情况下,project1排查project3
1)前两步如依赖传递的第一步和第二步 2)第三步在A依赖B的时候排除C
<dependency>
<groupId>com.xc</groupId>
<artifactId>project2</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
<!-- 使用excludes标签配置依赖的排除 -->
<exclusions>
<!-- 在exclude标签中配置一个具体的排除 -->
<exclusion>
<!-- 指定要排除的依赖的坐标(不需要写version) -->
<groupId>com.xc</groupId>
<artifactId>project3</artifactId>
</exclusion>
</exclusions>
</dependency>
3)查看project1和project2的所有依赖
版本号组成
snapshot
表示快照版,它不是个稳定版本,属于开发过程中使用的版本内部测试alpha版
、外部测试beta版
两种正式版本
,大部分的正式版是啥也不带,就一个单纯的版本号,比如1.0、1.7.1等。也有个别的是这些rc
、final
、stable
、release
、ga
等概念
Maven工程之间,A 工程继承 B 工程。本质上是 A 工程的 pom.xml 中的配置继承了 B 工程中 pom.xml 的配置
创建父工程
<groupId>com.xc.maven</groupId>
<artifactId>pro-maven-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 当前工程作为父工程,它要去管理子工程,所以打包方式必须是 pom -->
<packaging>pom</packaging>
创建子工程
如果子工程坐标中的groupId和version与父工程一致,那么可以省略
<!-- 使用parent标签指定当前工程的父工程 -->
<parent>
<!-- 父工程的坐标 -->
<groupId>com.xc.maven</groupId>
<artifactId>pro-maven-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<!-- 子工程的坐标 -->
<!-- <groupId>com.atguigu.maven</groupId> -->
<artifactId>pro01-maven-module</artifactId>
<!-- <version>1.0-SNAPSHOT</version> -->
父工程中配置依赖的统一管理
被管理的依赖并没有真正被引入到工程
<!-- 使用dependencyManagement标签配置对依赖的管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
子工程中引用那些被父工程管理的依赖
<!-- 具体来说是由父工程的dependencyManagement来决定。 -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</dependency>
</dependencies>
在父工程中声明自定义属性
通过自定义属性,统一指定Spring的版本
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 自定义标签,维护Spring版本数据 -->
<my.spring.version>4.3.6.RELEASE</my.spring.version>
</properties>
在需要的地方使用${}的形式来引用自定义的属性名
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${my.spring.version}</version>
</dependency>
使用一个“总工程”将各个“模块工程”汇集起来,作为一个整体对应完整的项目
好处
聚合的配置
在总工程中配置 modules 即可
<modules>
<module>pro01-maven-module</module>
<module>pro02-maven-module</module>
<module>pro03-maven-module</module>
</modules>
作用
为了让构建过程自动化完成,Maven 设定了三个生命周期,生命周期中的每一个环节对应构建过程中的一个操作
三个生命周期
生命周期名称 | 作用 | 各个环节 |
---|---|---|
Clean | 清理操作相关 | pre-cleancleanpost-clean |
Site | 生成站点相关 | pre-sitesitepost-sitedeploy-site |
Default | 主要构建过程 | validategenerate-sourcesprocess-sourcesgenerate-resourcesprocess-resources 复制并处理资源文件,至目标目录,准备打包compile 编译项目 main 目录下的源代码process-classesgenerate-test-sourcesprocess-test-sourcesgenerate-test-resourcesprocess-test-resources复制并处理资源文件,至目标测试目录test-compile编译测试源代码process-test-classestest 使用合适的单元测试框架运行测试。这些测试代码不会被打包或部署prepare-packagepackage 接受编译好的代码,打包成可发布的格式pre-integration-testpost-integration-testverifyinstall 将包安装至本地仓库,以让其它项目依赖deploy 将最终的包复制到远程的仓库,以让其它开发人员共享;或者部署到服务器上运行 |
特点
插件
目标
<!-- build 标签:用来配置对构建过程的定制 -->
<build>
<!-- plugins 标签:定制化构建过程中所使用到的插件 -->
<plugins>
<!-- plugin 标签:一个具体插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
举例1
pom文件:
...
<groupId>com.xc</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
...
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
打包结果:
举例2
pom文件:
...
<groupId>com.xc</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
...
<build>
<finalName>my-mybatis</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
打包结果:
打包的名称
,不再使用artifactId + 版本号方式jar的后缀
不写<resources>标签
<resources>标签只设置src/main/java
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
最终方案
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
1、设定打包方式
<packaging>maven-plugin</packaging>
2、引入依赖
核心依赖
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.5.2</version>
</dependency>
使用注解标注执行目标名称
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.5.2</version>
</dependency>
3、创建 Mojo 类
@Mojo(name = "sayHello")//执行目标名称
public class MyHelloPlugin extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("---> hello word my maven plugin <---");
}
}
4、配置build
<build>
<plugins>
<plugin>
<groupId>com.xc</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<!-- 指定唯一标识 -->
<id>hello1</id>
<!-- 指定和目标关联的生命周期阶段 -->
<phase>validate</phase>
<!-- 关联指定生命周期的目标 -->
<goals>
<!-- goals 标签中可以配置多个 goal 标签,表示一个生命周期环节可以对应当前插件的多个目标 -->
<goal>sayHello</goal>
</goals>
</execution>
<execution>
<id>hello2</id>
<phase>compile</phase>
<goals>
<goal>sayHello</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
5、执行compile,绑定两个周期,执行两次
6、插件识别方式
前置匹配:
中间匹配: