Maven
Maven
安装maven,配置环境变量,配置maven的settings.xml(本地仓库地址,远程镜像地址,nexus私服地址),idea的maven配置暂时不讲
这里只来记录下本人使用maven的pom.xml的配置及常用标签
当我们选择maven构建spring boot的项目,会自动生成一个pom.xml文件
pom.xml 就是 maven 的配置文件,用以描述项目的各种信息。
常用标签
1.project - 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
</project>
2.modelVersion - modelVersion 指定 pom.xml 符合哪个版本的描述符。maven 2 和 3 只能为 4.0.0
<modelVersion>4.0.0</modelVersion>
3.parent - maven 支持继承功能。子 POM 可以使用 parent 指定父 POM ,然后继承其配置。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath />
</parent>
一般直接创建的spring boot项目继承spring-boot-starter-parent,parent在聚合项目中用的比较多,现在的微服务项目大多都是聚合项目
4.modules- 聚合项目子模块列表 ,和parent配合使用。
父模块zzy-parent 的 pom.xml
<groupId>com.zzy</groupId>
<artifactId>zzy-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>zzy-service</module>
<module>zzy-common</module>
<module>cmall-common-db</module>
<module>cmall-eureka</module>
<module>cmall-gateway</module>
</modules>
子模块zzy-service 的 pom.xml
<parent>
<groupId>com.zzy</groupId>
<artifactId>zzy-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
5.artifactId - 单独项目的唯一标识符。比如我们的 tomcat、commons 等。不要在 artifactId 中包含点号(.)。
<artifactId>zzy-parent</artifactId>
6.groupId - 团体、组织的标识符。团体标识的约定是,它以创建这个项目的组织名称的逆向域名(reverse domain name)开头。一般对应着 java 的包结构
<groupId>com.zzy</groupId>
7.version - 项目的特定版本
<version>1.0-SNAPSHOT</version>
maven 在版本管理时候可以使用几个特殊的字符串 SNAPSHOT、LATEST、RELEASE。比如 1.0-SNAPSHOT。各个部分的含义和处理逻辑如下说明:
SNAPSHOT - 这个版本一般用于开发过程中,表示不稳定的版本。
LATEST - 指某个特定构件的最新发布,这个发布可能是一个发布版,也可能是一个 snapshot 版,具体看哪个时间最后。
RELEASE :指最后一个发布版。
8.packaging - 项目的类型,描述了项目打包后的输出,默认是 jar。常见的输出类型为:pom, jar, maven-plugin, ejb, war, ear, rar, par。
<packaging>jar</packaging>
9.dependencies - 依赖坐标集
10.dependency - 依赖坐标
<dependencies>
<dependency>
<groupId>org.gavaghan</groupId>
<artifactId>geodesy</artifactId>
<scope>system</scope>
<version>0.0.1-SNAPSHOT</version>
<systemPath>${project.basedir}/src/main/resources/lib/geodesy-1.1.3.jar</systemPath>
</dependency>
</dependencies>
11.type - 对应 packaging 的类型,如果不使用 type 标签,maven 默认为 jar。
12.scope - 此元素指的是任务的类路径(编译和运行时,测试等)以及如何限制依赖关系的传递性。有 5 种可用的限定范围:
- compile - 如果没有指定
scope标签,maven 默认为这个范围。编译依赖关系在所有 classpath 中都可用。此外,这些依赖关系被传播到依赖项目。 - provided - 与 compile 类似,但是表示您希望 jdk 或容器在运行时提供它。它只适用于编译和测试 classpath,不可传递。
- runtime - 此范围表示编译不需要依赖关系,而是用于执行。它是在运行时和测试 classpath,但不是编译 classpath。
- test - 此范围表示正常使用应用程序不需要依赖关系,仅适用于测试编译和执行阶段。它不是传递的。
- system - 此范围与 provided 类似,除了您必须提供明确包含它的 jar。该 artifact 始终可用,并且不是在仓库中查找。
13.optional - optional 让其他项目知道,当您使用此项目时,您不需要这种依赖性才能正常工作。
14.exclusions - 包含一个或多个排除元素,每个排除元素都包含一个表示要排除的依赖关系的 groupId 和 artifactId。与可选项不同,可能或可能不会安装和使用,排除主动从依赖关系树中删除自己。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
<exclusions>
<exclusion>
<artifactId>mybatis</artifactId>
<groupId>org.mybatis</groupId>
</exclusion>
<exclusion>
<artifactId>mybatis-spring</artifactId>
<groupId>org.mybatis</groupId>
</exclusion>
<exclusion>
<artifactId>jsqlparser</artifactId>
<groupId>com.github.jsqlparser</groupId>
</exclusion>
</exclusions>
</dependency>
15.dependencyManagement dependencyManagement 是表示依赖 jar 包的声明。即你在项目中的 dependencyManagement 下声明了依赖,maven 不会加载该依赖,dependencyManagement 声明可以被子 POM 继承。
dependencyManagement 的一个使用案例是当有父子项目的时候,父项目中可以利用 dependencyManagement 声明子项目中需要用到的依赖 jar 包,之后,当某个或者某几个子项目需要加载该依赖的时候,就可以在子项目中 dependencies 节点只配置 groupId 和 artifactId 就可以完成依赖的引用。
dependencyManagement 主要是为了统一管理依赖包的版本,确保所有子项目使用的版本一致,类似的还有plugins和pluginManagement。
这个可以去spring-boot-starter-parent -> spring-boot-dependencie 里查看
ctrl+左键
16.properties - 属性列表。定义的属性可以在 pom.xml 文件中任意处使用。使用方式为 ${propertie} 。
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<resource.delimiter>@</resource.delimiter>
<maven.compiler.source>${java.version}</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
接下来通过一个案例来学习其它几个标签
我们实际开发中一个项目至少对应开发、测试、生成三种环境,如何方便的管理多环境
1.Spring Profile
Spring Profile 是 Spring 提供的多环境管理方案。
在resources目录下创建
- application.yml
- application-dev.yml
- application-test.yml
- application-prod.yml
spring 默认加载application.yml文件,其它配置文件为 application- 开头
spring:
profiles:
active: dev
并通过其中配置确定加载那个环境的配置文件
Spring Profile 给出的多环境管理方案。通过改变 spring.profiles.active的值来切换不同的环境。这种方法简单易懂,但有两个问题。
- 每次切换环境要手动修改
spring.profiles.active的值 - 打包的时候,要手动删除其它环境的配置文件,不然其它环境的敏感信息就都打包进去了
为了解决这两个问题,我们需要 maven profile 的配合
2.maven profile
profile可以在settings.xml中定义(功能不全,全局影响)
可以定义jdk版本
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<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>
profile可以具体在pom.xml中定义(功能全,影响当前项目)
解决上面的多环境配置和资源过滤需要两个标签
-
profile
-
resources
<profiles>
<profile>
<id>dev</id>
<activation>
<!-- activeByDefault 为 true 表示,默认激活 id为dev 的profile-->
<activeByDefault>true</activeByDefault>
</activation>
<!-- properties 里面可以添加自定义节点,如下添加了一个env节点 -->
<properties>
<!-- 这个节点的值(env可以随意定制)可以在maven的其他地方引用,可以简单理解为定义了一个叫env的变量 -->
<env>dev</env>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
<build>
<!--项目打包的名字-->
<finalName>zzyNotes</finalName>
<!--spring boot 自带插件-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!--资源-->
<resources>
<resource>
<!--影响的目录-->
<directory>src/main/resources</directory>
<!--排除的文件,或者目录-->
<excludes>
<!--先排除application开头的配置文件-->
<exclude>application*.yml</exclude>
</excludes>
</resource>
<resource>
<!--影响的目录-->
<directory>src/main/resources</directory>
<!--filtering 需要设置为 true,这样在include的时候,才会把
配置文件中的@env@ 这个maven`变量`替换成当前环境的对应值 -->
<filtering>true</filtering>
<includes>
<!--引入所需环境的配置文件-->
<include>application.yml</include>
<include>application-${env}.yml</include>
</includes>
</resource>
</resources>
</build>
pom中 使用属性值
${}
spring 中使用pom中定义的属性值 ,注意 true
spring:
profiles:
active: @env@
16.profile
profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。需要掌握profile的定义以及激活条件
1.默认激活
<profiles>
<profile>
<id>profileTest4</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
2.根据mvn命令 -P (注意大写P)设置环境profile (最常用)
mvn clean install -P dev
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
2.5!!!使用Idea开发会发现maven工具栏出现了profiles,本地启动项目时就可以通过设置这里来快速切换环境
3.根据jdk环境来激活
<profiles>
<profile>
<id>profileTest1</id>
<jdk>1.5</jdk>
</profile>
<profiles>
4.根据操作系统环境来激活
<profiles>
<profile>
<id>profileTest2</id>
<activation>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
</activation>
</profile>
</profiles>
5.根据文件是否存在激活
<profiles>
<profile>
<id>profileTest3</id>
<activation>
<file>
<missing>target</missing>
</file>
</activation>
</profile>
</profiles>
更多推荐



所有评论(0)