万州建设工程信息网站,网页浏览器证书失效怎么修复,wordpress显示运行时间,单位网站改版文章目录 第一节 重新认识MavenMaven 的完整功能项目管理功能的具体体现 POM 的四个层次超级 POM父 POM有效 POM概念查看有效 POM 小结 属性的声明与引用help 插件的各个目标使用 help:evaluate 查看属性值通过 Maven 访问系统属性Java 系统属性一览使用 Maven 访问系统属性 访… 文章目录 第一节 重新认识MavenMaven 的完整功能项目管理功能的具体体现 POM 的四个层次超级 POM父 POM有效 POM概念查看有效 POM 小结 属性的声明与引用help 插件的各个目标使用 help:evaluate 查看属性值通过 Maven 访问系统属性Java 系统属性一览使用 Maven 访问系统属性 访问系统环境变量访问 project 属性访问一级标签访问子标签访问列表标签 访问 settings 全局配置用途 build 标签详解一睹真容build 标签组成定义约定的目录结构备用插件管理生命周期插件 典型应用指定 JDK 版本编写源文件代码补充说明 典型应用SpringBoot 定制化打包插件的七个目标 典型应用Mybatis 逆向工程小结 依赖配置补充依赖范围importsystemruntime 可选依赖本质含义 [版本仲裁最短路径优先路径相同时先声明者优先小结 profile 详解profile 概述项目的不同运行环境profile 声明和使用的基本逻辑 profile 配置profiles/profile 标签 激活 profile命令行激活 操作举例编写 Lambda 表达式代码配置 profile执行构建命令 资源属性过滤简介操作演示 第一节 重新认识Maven
Maven 的完整功能
在入门的时候我们介绍说 Maven 是一款『构建管理』和『依赖管理』的工具。但事实上这只是 Maven 的一部分功能。Maven 本身的产品定位是一款『项目管理工具』。
项目管理功能的具体体现
下面是 spring-boot-starter 的 POM 文件可以看到除了我们熟悉的坐标标签、dependencies 标签还有 description、url、organization、licenses、developers、scm、issueManagement 等这些描述项目信息的标签。
?xml version1.0 encodingUTF-8?
project xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance!-- This module was also published with a richer model, Gradle metadata, --!-- which should be used instead. Do not delete the following line which --!-- is to indicate to Gradle or any Gradle module metadata file consumer --!-- that they should prefer consuming it instead. --!-- do_not_remove: published-with-gradle-metadata --modelVersion4.0.0/modelVersiongroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactIdversion2.5.6/versionnamespring-boot-starter/namedescriptionCore starter, including auto-configuration support, logging and YAML/descriptionurlhttps://spring.io/projects/spring-boot/urlorganizationnamePivotal Software, Inc./nameurlhttps://spring.io/url/organizationlicenseslicensenameApache License, Version 2.0/nameurlhttps://www.apache.org/licenses/LICENSE-2.0/url/license/licensesdevelopersdevelopernamePivotal/nameemailinfopivotal.io/emailorganizationPivotal Software, Inc./organizationorganizationUrlhttps://www.spring.io/organizationUrl/developer/developersscmconnectionscm:git:git://github.com/spring-projects/spring-boot.git/connectiondeveloperConnectionscm:git:ssh://gitgithub.com/spring-projects/spring-boot.git/developerConnectionurlhttps://github.com/spring-projects/spring-boot/url/scmissueManagementsystemGitHub/systemurlhttps://github.com/spring-projects/spring-boot/issues/url/issueManagementdependenciesdependency……/dependency/dependencies
/project所以从『项目管理』的角度来看Maven 提供了如下这些功能
项目对象模型POM将整个项目本身抽象、封装为应用程序中的一个对象以便于管理和操作。全局性构建逻辑重用Maven 对整个构建过程进行封装之后程序员只需要指定配置信息即可完成构建。让构建过程从 Ant 的『编程式』升级到了 Maven 的『声明式』。构件的标准集合在 Maven 提供的标准框架体系内所有的构件都可以按照统一的规范生成和使用。构件关系定义Maven 定义了构件之间的三种基本关系让大型应用系统可以使用 Maven 来进行管理 继承关系通过从上到下的继承关系将各个子构件中的重复信息提取到父构件中统一管理聚合关系将多个构件聚合为一个整体便于统一操作依赖关系Maven 定义了依赖的范围、依赖的传递、依赖的排除、版本仲裁机制等一系列规范和标准让大型项目可以有序容纳数百甚至更多依赖 插件目标系统Maven 核心程序定义抽象的生命周期然后将插件的目标绑定到生命周期中的特定阶段实现了标准和具体实现解耦合让 Maven 程序极具扩展性项目描述信息的维护我们不仅可以在 POM 中声明项目描述信息更可以将整个项目相关信息收集起来生成 HTML 页面组成的一个可以直接访问的站点。这些项目描述信息包括 公司或组织信息项目许可证开发成员信息issue 管理信息SCM 信息
POM 的四个层次
超级 POM
经过我们前面的学习我们看到 Maven 在构建过程中有很多默认的设定。例如源文件存放的目录、测试源文件存放的目录、构建输出的目录……等等。但是其实这些要素也都是被 Maven 定义过的。定义的位置就是超级 POM。
关于超级 POMMaven 官网是这样介绍的 The Super POM is Maven’s default POM. All POMs extend the Super POM unless explicitly set, meaning the configuration specified in the Super POM is inherited by the POMs you created for your projects. 译文Super POM 是 Maven 的默认 POM。除非明确设置否则所有 POM 都扩展 Super POM这意味着 Super POM 中指定的配置由您为项目创建的 POM 继承。 所以我们自己的 POM 即使没有明确指定一个父工程父 POM其实也默认继承了超级 POM。就好比一个 Java 类默认继承了 Object 类。
那么超级 POM 中定义了哪些东西呢点击这里查看。
父 POM
和 Java 类一样POM 之间其实也是单继承的。如果我们给一个 POM 指定了父 POM那么继承关系如下图所示 有效 POM
概念
有效 POM 英文翻译为 effective POM它的概念是这样的——在 POM 的继承关系中子 POM 可以覆盖父 POM 中的配置如果子 POM 没有覆盖那么父 POM 中的配置将会被继承。按照这个规则继承关系中的所有 POM 叠加到一起就得到了一个最终生效的 POM。显然 Maven 实际运行过程中执行构建操作就是按照这个最终生效的 POM 来运行的。这个最终生效的 POM 就是有效 POM英文叫effective POM。
查看有效 POM mvn help:effective-pom 小结
综上所述平时我们使用和配置的 POM 其实大致是由四个层次组成的
超级 POM所有 POM 默认继承只是有直接和间接之分。父 POM这一层可能没有可能有一层也可能有很多层。当前 pom.xml 配置的 POM我们最多关注和最多使用的一层。有效 POM隐含的一层但是实际上真正生效的一层。
属性的声明与引用
help 插件的各个目标
官网说明地址https://maven.apache.org/plugins/maven-help-plugin
目标说明help:active-profiles列出当前已激活的 profilehelp:all-profiles列出当前工程所有可用 profilehelp:describe描述一个插件和/或 Mojo 的属性help:effective-pom以 XML 格式展示有效 POMhelp:effective-settings为当前工程以 XML 格式展示计算得到的 settings 配置help:evaluate计算用户在交互模式下给出的 Maven 表达式help:system显示平台详细信息列表如系统属性和环境变量
使用 help:evaluate 查看属性值
定义属性
propertiescom.atguigu.hellogood morning maven/com.atguigu.hello
/properties运行命令 运行结果 通过 Maven 访问系统属性
Java 系统属性一览
Java 代码
Properties properties System.getProperties();
SetObject propNameSet properties.keySet();
for (Object propName : propNameSet) {String propValue properties.getProperty((String) propName);System.out.println(propName propValue);
}运行结果 java.runtime.name Java™ SE Runtime Environment sun.boot.library.path D:\software\Java\jre\bin java.vm.version 25.141-b15 java.vm.vendor Oracle Corporation java.vendor.url http://java.oracle.com/ path.separator ; java.vm.name Java HotSpot™ 64-Bit Server VM file.encoding.pkg sun.io user.country CN user.script sun.java.launcher SUN_STANDARD sun.os.patch.level java.vm.specification.name Java Virtual Machine Specification user.dir D:\idea2019workspace\atguigu-maven-test-prepare java.runtime.version 1.8.0_141-b15 java.awt.graphicsenv sun.awt.Win32GraphicsEnvironment java.endorsed.dirs D:\software\Java\jre\lib\endorsed os.arch amd64 java.io.tmpdir C:\Users\ADMINI~1\AppData\Local\Temp line.separator java.vm.specification.vendor Oracle Corporation user.variant os.name Windows 10 sun.jnu.encoding GBK java.library.path D:\software\Java\bin;C:\WINDOWS\Sun\Java\bin;C:\WIN…… java.specification.name Java Platform API Specification java.class.version 52.0 sun.management.compiler HotSpot 64-Bit Tiered Compilers os.version 10.0 user.home C:\Users\Administrator user.timezone java.awt.printerjob sun.awt.windows.WPrinterJob file.encoding UTF-8 java.specification.version 1.8 java.class.path D:\software\Java\jre\lib\charsets.jar;D:\softw…… user.name Administrator java.vm.specification.version 1.8 sun.java.command com.atguigu.maven.MyTest java.home D:\software\Java\jre sun.arch.data.model 64 user.language zh java.specification.vendor Oracle Corporation awt.toolkit sun.awt.windows.WToolkit java.vm.info mixed mode java.version 1.8.0_141 java.ext.dirs D:\software\Java\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext sun.boot.class.path D:\software\Java\jre\lib\resources.jar;D:\sof…… java.vendor Oracle Corporation file.separator java.vendor.url.bug http://bugreport.sun.com/bugreport/ sun.io.unicode.encoding UnicodeLittle sun.cpu.endian little sun.desktop windows sun.cpu.isalist amd64 使用 Maven 访问系统属性 访问系统环境变量
${env.系统环境变量名} 访问 project 属性
含义
使用表达式 ${project.xxx} 可以访问当前 POM 中的元素值。
访问一级标签
${project.标签名} 访问子标签
${project.标签名.子标签名} 访问列表标签
${project.标签名[下标]} 访问 settings 全局配置
${settings.标签名} 可以访问 settings.xml 中配置的元素值。 用途
在当前 pom.xml 文件中引用属性资源过滤功能在非 Maven 配置文件中引用属性由 Maven 在处理资源时将引用属性的表达式替换为属性值
build 标签详解
一睹真容
在实际使用 Maven 的过程中我们会发现 build 标签有时候有有时候没这是怎么回事呢其实通过有效 POM 我们能够看到build 标签的相关配置其实一直都在只是在我们需要定制构建过程的时候才会通过配置 build 标签覆盖默认值或补充配置。这一点我们可以通过打印有效 POM 来看到。 打印有效 pom mvn help:effective-pom 目标说明help:active-profiles列出当前已激活的 profilehelp:all-profiles列出当前工程所有可用 profilehelp:describe描述一个插件和/或 Mojo 的属性help:effective-pom以 XML 格式展示有效 POMhelp:effective-settings为当前工程以 XML 格式展示计算得到的 settings 配置help:evaluate计算用户在交互模式下给出的 Maven 表达式help:system显示平台详细信息列表如系统属性和环境变量 所以本质上来说我们配置的 build 标签都是对超级 POM 配置的叠加。那我们又为什么要在默认配置的基础上叠加呢很简单在默认配置无法满足需求的时候定制构建过程。
build 标签组成
从完整示例中我们能够看到build 标签的子标签大致包含三个主体部分 定义约定的目录结构
配置大于编码约定大于配置
参考示例中的如下部分
sourceDirectoryD:\idea2019workspace\atguigu-maven-test-prepare\src\main\java/sourceDirectory
scriptSourceDirectoryD:\idea2019workspace\atguigu-maven-test-prepare\src\main\scripts/scriptSourceDirectory
testSourceDirectoryD:\idea2019workspace\atguigu-maven-test-prepare\src\test\java/testSourceDirectory
outputDirectoryD:\idea2019workspace\atguigu-maven-test-prepare\target\classes/outputDirectory
testOutputDirectoryD:\idea2019workspace\atguigu-maven-test-prepare\target\test-classes/testOutputDirectory
resourcesresourcedirectoryD:\idea2019workspace\atguigu-maven-test-prepare\src\main\resources/directory/resource
/resources
testResourcestestResourcedirectoryD:\idea2019workspace\atguigu-maven-test-prepare\src\test\resources/directory/testResource
/testResources
directoryD:\idea2019workspace\atguigu-maven-test-prepare\target/directory我们能看到各个目录的作用如下
目录名作用sourceDirectory主体源程序存放目录scriptSourceDirectory脚本源程序存放目录testSourceDirectory测试源程序存放目录outputDirectory主体源程序编译结果输出目录testOutputDirectory测试源程序编译结果输出目录resources主体资源文件存放目录testResources测试资源文件存放目录directory构建结果输出目录
备用插件管理
pluginManagement 标签存放着几个极少用到的插件
maven-antrun-pluginmaven-assembly-pluginmaven-dependency-pluginmaven-release-plugin
通过 pluginManagement 标签管理起来的插件就像 dependencyManagement 一样子工程使用时可以省略版本号起到在父工程中统一管理版本的效果。情看下面例子
被 spring-boot-dependencies 管理的插件信息
plugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdversion2.6.2/version
/plugin子工程使用的插件信息
buildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins
/build生命周期插件
plugins 标签存放的是默认生命周期中实际会用到的插件这些插件想必大家都不陌生所以抛开插件本身不谈我们来看看 plugin 标签的结构
pluginartifactIdmaven-compiler-plugin/artifactIdversion3.1/versionexecutionsexecutioniddefault-compile/idphasecompile/phasegoalsgoalcompile/goal/goals/executionexecutioniddefault-testCompile/idphasetest-compile/phasegoalsgoaltestCompile/goal/goals/execution/executions
/plugin坐标部分
artifactId 和 version 标签定义了插件的坐标作为 Maven 的自带插件这里省略了 groupId。
执行部分
executions 标签内可以配置多个 execution 标签execution 标签内
id指定唯一标识phase关联的生命周期阶段goals/goal关联指定生命周期的目标 goals 标签中可以配置多个 goal 标签表示一个生命周期环节可以对应当前插件的多个目标。
典型应用指定 JDK 版本
提出问题
前面我们在 settings.xml 中配置了 JDK 版本那么将来把 Maven 工程部署都服务器上脱离了 settings.xml 配置如何保证程序正常运行呢思路就是我们直接把 JDK 版本信息告诉负责编译操作的 maven-compiler-plugin 插件让它在构建过程中按照我们指定的信息工作。
暂时取消 settings.xml 配置
为了测试对 maven-compiler-plugin 插件进行配置的效果我们暂时取消 settings.xml 中的 profile 配置。
!-- 配置Maven工程的默认JDK版本 --
!-- profileidjdk-1.8/idactivationactiveByDefaulttrue/activeByDefaultjdk1.8/jdk/activationpropertiesmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.targetmaven.compiler.compilerVersion1.8/maven.compiler.compilerVersion/properties
/profile --编写源文件代码
很明显这里用到了 Lambda 表达式这是 JDK 1.8 才支持的语法。
package com.atguigu.maven;public class Hello {public void hello() {new Thread(()-{System.out.println(thread ...);}).start();}}此时我们执行编译命令 配置构建过程
!-- build 标签意思是告诉 Maven你的构建行为我要开始定制了 --
build!-- plugins 标签Maven 你给我听好了你给我构建的时候要用到这些插件 --plugins!-- plugin 标签这是我要指定的一个具体的插件 --plugin!-- 插件的坐标。此处引用的 maven-compiler-plugin 插件不是第三方的是一个 Maven 自带的插件。 --groupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion3.1/version!-- configuration 标签配置 maven-compiler-plugin 插件 --configuration!-- 具体配置信息会因为插件不同、需求不同而有所差异 --source1.8/sourcetarget1.8/targetencodingUTF-8/encoding/configuration/plugin/plugins
/build再次执行编译命令 两种配置方式比较
settings.xml 中配置仅在本地生效如果脱离当前 settings.xml 能够覆盖的范围则无法生效。在当前 Maven 工程 pom.xml 中配置无论在哪个环境执行编译等构建操作都有效。
补充说明 source 标签含义 查看 Maven 官网页面 (opens new window)我们找到 source 标签的介绍 翻译过来就是调用 Java 编译器命令时传入的 -source 参数。那对编译器来说-source 参数是啥意思呢 『提供与指定发行版的源兼容性』这句话我的理解是 我们写代码是按 JDK 1.8 写的——这就是『源兼容性』里的『源』。指定发行版就是我们指定的 JDK 1.8。『兼容性』是谁和谁兼容呢现在源代码是既定的所以就是要求编译器使用指定的 JDK 版本来兼容我们的源代码。 target 标签含义 翻译过来就是调用 Java 编译器命令时传入的 -target 参数。那对编译器来说-target 参数是啥意思呢 『生成特定 VM 版本的类文件』这句话我的理解是 VM 指 JVM类文件指 *.class 字节码文件整体意思就是源文件编译后生成的 *.class 字节码文件要符合指定的 JVM 版本 这个功能还可以通过在 properties 标签中配置 maven.compiler.source 属性来实现。所以我们也经常会看到类似这样的配置 propertiesmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding
/properties典型应用SpringBoot 定制化打包
需求
很显然 spring-boot-maven-plugin 并不是 Maven 自带的插件而是 SpringBoot 提供的用来改变 Maven 默认的构建行为。 具体来说是改变打包的行为。默认情况下 Maven 调用 maven-jar-plugin 插件的 jar 目标生成普通的 jar 包。 普通 jar 包没法使用 java -jar xxx.jar 这样的命令来启动、运行但是 SpringBoot 的设计理念就是每一个『微服务』导出为一个 jar 包这个 jar 包可以使用 java -jar xxx.jar 这样的命令直接启动运行。 这样一来打包的方式肯定要进行调整。所以 SpringBoot 提供了 spring-boot-maven-plugin 这个插件来定制打包行为。 示例代码
所有的一切已经都被 SpringBoot 封装好了所以配置非常简单提供插件坐标即可。
buildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdversion2.5.5/version/plugin/plugins
/build插件的七个目标 目标名称作用spring-boot:build-imagePackage an application into a OCI image using a buildpack.spring-boot:build-infoGenerate a build-info.properties file based on the content of the current MavenProject.spring-boot:helpDisplay help information on spring-boot-maven-plugin. Call mvn spring-boot:help -Ddetailtrue -Dgoal to display parameter details.spring-boot:repackageRepackage existing JAR and WAR archives so that they can be executed from the command line using java -jar. With layoutNONE can also be used simply to package a JAR with nested dependencies (and no main class, so not executable).spring-boot:runRun an application in place.spring-boot:startStart a spring application. Contrary to the run goal, this does not block and allows other goals to operate on the application. This goal is typically used in integration test scenario where the application is started before a test suite and stopped after.spring-boot:stopStop an application that has been started by the ‘start’ goal. Typically invoked once a test suite has completed.
典型应用Mybatis 逆向工程
使用 Mybatis 的逆向工程需要使用如下配置MBG 插件的特点是需要提供插件所需的依赖
!-- 控制 Maven 在构建过程中相关配置 --
build!-- 构建过程中用到的插件 --plugins!-- 具体插件逆向工程的操作是以构建过程中插件形式出现的 --plugingroupIdorg.mybatis.generator/groupIdartifactIdmybatis-generator-maven-plugin/artifactIdversion1.3.0/version!-- 插件的依赖 --dependencies!-- 逆向工程的核心依赖 --dependencygroupIdorg.mybatis.generator/groupIdartifactIdmybatis-generator-core/artifactIdversion1.3.2/version/dependency!-- 数据库连接池 --dependencygroupIdcom.mchange/groupIdartifactIdc3p0/artifactIdversion0.9.2/version/dependency!-- MySQL驱动 --dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.8/version/dependency/dependencies/plugin/plugins
/build小结
不知大家有没有发现通常需要用到 build 标签的时候底层都会帮我们封装好需要我们配置的地方不多。即使有些地方需要我们配置也不会真的我们自己去写把现成的案例复制过来就行。
所以对 build 标签来说我们的掌握要求就是能大致看懂就行。
依赖配置补充
TIP
Maven 官网介绍依赖机制(opens new window)
依赖范围
import
管理依赖最基本的办法是继承父工程但是和 Java 类一样Maven 也是单继承的。如果不同体系的依赖信息封装在不同 POM 中了没办法继承多个父工程怎么办这时就可以使用 import 依赖范围。
典型案例当然是在项目中引入 SpringBoot、SpringCloud 依赖
dependencyManagementdependencies!-- SpringCloud 依赖导入 --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversionHoxton.SR9/versiontypepom/typescopeimport/scope/dependency!-- SpringCloud Alibaba 依赖导入 --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-alibaba-dependencies/artifactIdversion2.2.6.RELEASE/versiontypepom/typescopeimport/scope/dependency!-- SpringBoot 依赖导入 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion2.3.6.RELEASE/versiontypepom/typescopeimport/scope/dependency/dependencies
/dependencyManagementimport 依赖范围使用要求
打包类型必须是 pom必须放在 dependencyManagement 中 官网说明如下 This scope is only supported on a dependency of type pom in the dependencyManagement section. It indicates the dependency is to be replaced with the effective list of dependencies in the specified POM’s dependencyManagement section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency. system
以 Windows 系统环境下开发为例假设现在 D:\tempare\atguigu-maven-test-aaa-1.0-SNAPSHOT.jar 想要引入到我们的项目中此时我们就可以将依赖配置为 system 范围
dependencygroupIdcom.atguigu.maven/groupIdartifactIdatguigu-maven-test-aaa/artifactIdversion1.0-SNAPSHOT/versionsystemPathD:\tempare\atguigu-maven-test-aaa-1.0-SNAPSHOT.jar/systemPathscopesystem/scope
/dependency但是很明显这样引入依赖完全不具有可移植性所以不要使用。如果需要引入体系外 jar 包我们后面会讲专门的办法。
runtime
专门用于编译时不需要但是运行时需要的 jar 包。比如编译时我们根据接口调用方法但是实际运行时需要的是接口的实现类。典型案例是
!--热部署 --
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional
/dependency可选依赖
配置举例
!--热部署 --
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional
/dependency本质含义
可选其实就是『可有可无』。官网的解释是 其核心含义是Project X 依赖 Project AA 中一部分 X 用不到的代码依赖了 B那么对 X 来说 B 就是『可有可无』的。 [版本仲裁
最短路径优先
在下图的例子中对模块 pro25-module-a 来说Maven 会采纳 1.2.12 版本。 路径相同时先声明者优先 此时 Maven 采纳哪个版本取决于在 pro29-module-x 中对 pro30-module-y 和 pro31-module-z 两个模块的依赖哪一个先声明。
小结
其实 Maven 的版本仲裁机制只是在没有人为干预的情况下自主决定 jar 包版本的一个办法。而实际上我们要使用具体的哪一个版本还要取决于项目中的实际情况。所以在项目正常运行的情况下jar 包版本可以由 Maven 仲裁不必我们操心而发生冲突时 Maven 仲裁决定的版本无法满足要求此时就应该由程序员明确指定 jar 包版本。
profile 详解
profile 概述
单词释义
这里我们可以对接 profile 这个单词中『侧面』这个含义项目的每一个运行环境相当于是项目整体的一个侧面。 项目的不同运行环境 通常情况下我们至少有三种运行环境
开发环境供不同开发工程师开发的各个模块之间互相调用、访问内部使用测试环境供测试工程师对项目的各个模块进行功能测试内部使用生产环境供最终用户访问——所以这是正式的运行环境对外提供服务
而我们这里的『环境』仍然只是一个笼统的说法实际工作中一整套运行环境会包含很多种不同服务器
MySQLRedisElasticSearchRabbitMQFastDFSNginxTomcat……
就拿其中的 MySQL 来说不同环境下的访问参数肯定完全不同
开发环境测试环境生产环境dev.drivercom.mysql.jdbc.Driver dev.urljdbc:mysql://124.71.36.17:3306/db-sys dev.usernameroot dev.passwordatguigutest.drivercom.mysql.jdbc.Driver test.urljdbc:mysql://124.71.36.89:3306/db-sys test.usernamedev-team test.passwordatguiguproduct.drivercom.mysql.jdbc.Driver product.urljdbc:mysql://39.107.88.164:3306/prod-db-sys product.usernameroot product.passwordatguigu
可是代码只有一套。如果在 jdbc.properties 里面来回改那就太麻烦了而且很容易遗漏或写错增加调试的难度和工作量。所以最好的办法就是把适用于各种不同环境的配置信息分别准备好部署哪个环境就激活哪个配置。
在 Maven 中使用 profile 机制来管理不同环境下的配置信息。但是解决同类问题的类似机制在其他框架中也有而且从模块划分的角度来说持久化层的信息放在构建工具中配置也违反了『高内聚低耦合』的原则。
所以 Maven 的 profile 我们了解一下即可不必深究。
profile 声明和使用的基本逻辑
首先为每一个环境声明一个 profile 环境 Aprofile A环境 Bprofile B环境 Cprofile C…… 然后激活某一个 profile
默认 profile
其实即使我们在 pom.xml 中不配置 profile 标签也已经用到 profile了。为什么呢因为根标签 project 下所有标签相当于都是在设定默认的 profile。这样一来我们也就很容易理解下面这句话project 标签下除了 modelVersion 和坐标标签之外其它标签都可以配置到 profile 中。
profile 配置
外部视角配置文件
从外部视角来看profile 可以在下面两种配置文件中配置
settings.xml全局生效。其中我们最熟悉的就是配置 JDK 1.8。pom.xml当前 POM 生效
内部实现具体标签
从内部视角来看配置 profile 有如下语法要求
profiles/profile 标签
由于 profile 天然代表众多可选配置中的一个所以由复数形式的 profiles 标签统一管理。由于 profile 标签覆盖了 pom.xml 中的默认配置所以 profiles 标签通常是 pom.xml 中的最后一个标签。
id 标签
每个 profile 都必须有一个 id 标签指定该 profile 的唯一标识。这个 id 标签的值会在命令行调用 profile 时被用到。这个命令格式是-Pprofile id。其它允许出现的标签
一个 profile 可以覆盖项目的最终名称、项目依赖、插件配置等各个方面以影响构建行为。
build defaultGoalfinalNameresourcestestResourcesplugins reportingmodulesdependenciesdependencyManagementrepositoriespluginRepositoriesproperties
激活 profile
默认配置默认被激活
前面提到了POM 中没有在 profile 标签里的就是默认的 profile当然默认被激活。
基于环境信息激活
环境信息包含JDK 版本、操作系统参数、文件、属性等各个方面。一个 profile 一旦被激活那么它定义的所有配置都会覆盖原来 POM 中对应层次的元素。大家可以参考下面的标签结构
profileiddev/idactivation!-- 配置是否默认激活 --activeByDefaultfalse/activeByDefaultjdk1.5/jdkosnameWindows XP/namefamilyWindows/familyarchx86/archversion5.1.2600/version/ospropertynamemavenVersion/namevalue2.0.5/value/propertyfileexistsfile2.properties/existsmissingfile1.properties/missing/file/activation
/profile这里有个问题是多个激活条件之间是什么关系呢
Maven 3.2.2 之前遇到第一个满足的条件即可激活——或的关系。Maven 3.2.2 开始各条件均需满足——且的关系。
下面我们来看一个具体例子。假设有如下 profile 配置在 JDK 版本为 1.6 时被激活
profilesprofileidJDK1.6/idactivation!-- 指定激活条件为JDK 1.6 --jdk1.6/jdk/activation……/profile
/profiles这里需要指出的是Maven 会自动检测当前环境安装的 JDK 版本只要 JDK 版本是以 1.6 开头都算符合条件。下面几个例子都符合
1.6.0_031.6.0_02……
命令行激活
列出活动的 profile
# 列出所有激活的 profile以及它们在哪里定义
mvn help:active-profiles指定某个具体 profile
mvn compile -Pprofile id操作举例
编写 Lambda 表达式代码
Lambda 表达式代码要求 JDK 版本必须是 1.8我们可以以此来判断某个指定更低 JDK 版本的 profile 是否被激活生效。
Test
public void test() {new Thread(()-{System.out.println(Thread.currentThread().getName() is working);}).start();
}以目前配置运行这个测试方法 配置 profile
profilesprofileidmyJDKProfile/id!-- build 标签意思是告诉 Maven你的构建行为我要开始定制了 --build!-- plugins 标签Maven 你给我听好了你给我构建的时候要用到这些插件 --plugins!-- plugin 标签这是我要指定的一个具体的插件 --plugin!-- 插件的坐标。此处引用的 maven-compiler-plugin 插件不是第三方的是一个 Maven 自带的插件。 --groupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion3.1/version!-- configuration 标签配置 maven-compiler-plugin 插件 --configuration!-- 具体配置信息会因为插件不同、需求不同而有所差异 --source1.6/sourcetarget1.6/targetencodingUTF-8/encoding/configuration/plugin/plugins/build/profile
/profiles执行构建命令
mvn clean test -PmyJDKProfile资源属性过滤
简介
Maven 为了能够通过 profile 实现各不同运行环境切换提供了一种『资源属性过滤』的机制。通过属性替换实现不同环境使用不同的参数。
操作演示
配置 profile
profilesprofileiddevJDBCProfile/idpropertiesdev.jdbc.userroot/dev.jdbc.userdev.jdbc.passwordatguigu/dev.jdbc.passworddev.jdbc.urlhttp://localhost:3306/db_good/dev.jdbc.urldev.jdbc.drivercom.mysql.jdbc.Driver/dev.jdbc.driver/propertiesbuildresourcesresource!-- 表示为这里指定的目录开启资源过滤功能 --directorysrc/main/resources/directory!-- 将资源过滤功能打开 --filteringtrue/filtering/resource/resources/build/profile
/profiles创建待处理的资源文件
dev.user${dev.jdbc.user}
dev.password${dev.jdbc.password}
dev.url${dev.jdbc.url}
dev.driver${dev.jdbc.driver}执行处理资源命令
mvn clean resources:resources -PdevJDBCProfile找到处理得到的资源文件 延伸
我们时不时会在 resource 标签下看到 includes 和 excludes 标签。它们的作用是
includes指定执行 resource 阶段时要包含到目标位置的资源excludes指定执行 resource 阶段时要排除的资源
情看下面的例子
buildresourcesresource!-- 表示为这里指定的目录开启资源过滤功能 --directorysrc/main/resources/directory!-- 将资源过滤功能打开 --filteringtrue/filteringincludesinclude*.properties/include/includesexcludesexcludehappy.properties/exclude/excludes/resource/resources
/build执行处理资源命令
mvn clean resources:resources -PdevJDBCProfile执行效果如下 当然我们这里只是以 properties 文件为例并不是只能处理 properties 文件。