
HoRain云小助手个人主页 个人专栏: 《Linux 系列教程》《c语言教程》⛺️生活的理想就是为了理想的生活!⛳️ 推荐前些天发现了一个超棒的服务器购买网站性价比超高大内存超划算忍不住分享一下给大家。点击跳转到网站。专栏介绍专栏名称专栏介绍《C语言》本专栏主要撰写C干货内容和编程技巧让大家从底层了解C把更多的知识由抽象到简单通俗易懂。《网络协议》本专栏主要是注重从底层来给大家一步步剖析网络协议的奥秘一起解密网络协议在运行中协议的基本运行机制《docker容器精解篇》全面深入解析 docker 容器从基础到进阶涵盖原理、操作、实践案例助您精通 docker。《linux系列》本专栏主要撰写Linux干货内容从基础到进阶知识由抽象到简单通俗易懂帮你从新手小白到扫地僧。《python 系列》本专栏着重撰写Python相关的干货内容与编程技巧助力大家从底层去认识Python将更多复杂的知识由抽象转化为简单易懂的内容。《试题库》本专栏主要是发布一些考试和练习题库涵盖软考、HCIE、HRCE、CCNA等目录⛳️ 推荐专栏介绍构建配置文件的类型配置文件激活配置文件激活实例1、配置文件激活2、通过Maven设置激活配置文件3、通过环境变量激活配置文件4、通过操作系统激活配置文件5、通过文件的存在或者缺失激活配置文件构建配置文件是一系列的配置项的值可以用来设置或者覆盖 Maven 构建默认值。使用构建配置文件你可以为不同的环境比如说生产环境Production和开发Development环境定制构建方式。配置文件在 pom.xml 文件中使用 activeProfiles 或者 profiles 元素指定并且可以通过各种方式触发。配置文件在构建时修改 POM并且用来给参数设定不同的目标环境比如说开发Development、测试Testing和生产环境Production中数据库服务器的地址。构建配置文件的类型构建配置文件大体上有三种类型:类型在哪定义项目级Per Project定义在项目的POM文件pom.xml中用户级 Per User定义在Maven的设置xml文件中 (%USER_HOME%/.m2/settings.xml)全局Global定义在 Maven 全局的设置 xml 文件中 (%M2_HOME%/conf/settings.xml)配置文件激活Maven的构建配置文件可以通过多种方式激活。使用命令控制台输入显式激活。通过 maven 设置。基于环境变量用户或者系统变量。操作系统设置比如说Windows系列。文件的存在或者缺失。配置文件激活实例假定项目结构如下其中在src/main/resources文件夹下有三个用于测试文件文件名描述env.properties如果未指定配置文件时默认使用的配置。env.test.properties当测试配置文件使用时的测试配置。env.prod.properties当生产配置文件使用时的生产配置。注意这三个配置文件并不是代表构建配置文件的功能而是用于本次测试的目的比如我指定了构建配置文件为 prod 时项目就使用 env.prod.properties文件。注意下面的例子仍然是使用 AntRun 插件因为此插件能绑定 Maven 生命周期阶段并通过 Ant 的标签不用编写一点代码即可输出信息、复制文件等经此而已。其余的与本次构建配置文件无关。1、配置文件激活profile 可以让我们定义一系列的配置信息然后指定其激活条件。这样我们就可以定义多个 profile然后每个 profile 对应不同的激活条件和配置信息从而达到不同环境使用不同配置信息的效果。以下实例我们将 maven-antrun-plugin:run 目标添加到测试阶段中。这样我们可以在不同的 profile 中输出文本信息。我们将使用 pom.xml 来定义不同的 profile并在命令控制台中使用 maven 命令激活 profile。pom.xml 文件如下project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd modelVersion4.0.0/modelVersion groupIdcom.jsoft.test/groupId artifactIdtestproject/artifactId packagingjar/packaging version0.1-SNAPSHOT/version nametestproject/name urlhttp://maven.apache.org/url dependencies dependency groupIdjunit/groupId artifactIdjunit/artifactId version3.8.1/version scopetest/scope /dependency /dependencies profiles profile idtest/id build plugins plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-antrun-plugin/artifactId version1.8/version executions execution phasetest/phase goals goalrun/goal /goals configuration tasks echoUsing env.test.properties/echo copy filesrc/main/resources/env.test.properties tofile${project.build.outputDirectory}/env.properties overwritetrue/ /tasks /configuration /execution /executions /plugin /plugins /build /profile profile idnormal/id build plugins plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-antrun-plugin/artifactId version1.8/version executions execution phasetest/phase goals goalrun/goal /goals configuration tasks echoUsing env.properties/echo copy filesrc/main/resources/env.properties tofile${project.build.outputDirectory}/env.properties overwritetrue/ /tasks /configuration /execution /executions /plugin /plugins /build /profile profile idprod/id build plugins plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-antrun-plugin/artifactId version1.8/version executions execution phasetest/phase goals goalrun/goal /goals configuration tasks echoUsing env.prod.properties/echo copy filesrc/main/resources/env.prod.properties tofile${project.build.outputDirectory}/env.properties overwritetrue/ /tasks /configuration /execution /executions /plugin /plugins /build /profile /profiles /project注意构建配置文件采用的是profiles节点。说明上面新建了三个profiles其中id区分了不同的profiles执行不同的 AntRun 任务而 AntRun 的任务可以这么理解AntRun 监听 test 的 Maven 生命周期阶段当 Maven 执行 test 时就触发了 AntRun 的任务任务里面为输出文本并复制文件到指定的位置而至于要执行哪个 AntRun 任务此时构建配置文件起到了传输指定的作用比如通过命令行参数输入指定的id。执行命令mvn test -Ptest提示第一个 test 为 Maven 生命周期阶段第 2 个 test 为构建配置文件指定的 id 参数这个参数通过 -P 来传输当然它可以是 prod 或者 normal 这些由你定义的id。运行的结果如下可以看出成功的触发了AntRun的任务。并且是对应构建配置文件下的 id 为 test 的任务。再测试其余两个命令结果如下2、通过Maven设置激活配置文件打开%USER_HOME%/.m2目录下的settings.xml文件其中%USER_HOME%代表用户主目录。如果 setting.xml 文件不存在就直接拷贝%M2_HOME%/conf/settings.xml到 .m2 目录其中%M2_HOME%代表 Maven 的安装目录。配置 setting.xml 文件增加activeProfiles属性settings xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd ... activeProfiles activeProfiletest/activeProfile /activeProfiles /settings执行命令mvn test提示 1此时不需要使用 -Ptest 来输入参数了上面的 setting.xml 文件的 activeprofile 已经指定了 test 参数代替了。提示 2同样可以使用在%M2_HOME%/conf/settings.xml的文件进行配置效果一致。执行结果3、通过环境变量激活配置文件先把上一步测试的 setting.xml 值全部去掉。然后在 pom.xml 里面的 id 为 test 的 profile 节点加入 activation 节点project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd modelVersion4.0.0/modelVersion groupIdcom.jsoft.test/groupId artifactIdtestproject/artifactId packagingjar/packaging version0.1-SNAPSHOT/version nametestproject/name urlhttp://maven.apache.org/url dependencies dependency groupIdjunit/groupId artifactIdjunit/artifactId version3.8.1/version scopetest/scope /dependency /dependencies profiles profile idtest/id activation property nameenv/name valuetest/value /property /activation build plugins plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-antrun-plugin/artifactId version1.8/version executions execution phasetest/phase goals goalrun/goal /goals configuration tasks echoUsing env.test.properties/echo copy filesrc/main/resources/env.test.properties tofile${project.build.outputDirectory}/env.properties overwritetrue/ /tasks /configuration /execution /executions /plugin /plugins /build /profile profile idnormal/id build plugins plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-antrun-plugin/artifactId version1.8/version executions execution phasetest/phase goals goalrun/goal /goals configuration tasks echoUsing env.properties/echo copy filesrc/main/resources/env.properties tofile${project.build.outputDirectory}/env.properties overwritetrue/ /tasks /configuration /execution /executions /plugin /plugins /build /profile profile idprod/id build plugins plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-antrun-plugin/artifactId version1.8/version executions execution phasetest/phase goals goalrun/goal /goals configuration tasks echoUsing env.prod.properties/echo copy filesrc/main/resources/env.prod.properties tofile${project.build.outputDirectory}/env.properties overwritetrue/ /tasks /configuration /execution /executions /plugin /plugins /build /profile /profiles /project执行命令mvn test -Denvtest提示 1上面使用 -D 传递环境变量其中 env 对应刚才设置的 name 值test 对应value。提示 2在 Windows 10 上测试了系统的环境变量但是不生效所以只能通过 -D 传递。执行结果4、通过操作系统激活配置文件activation 元素包含下面的操作系统信息。当系统为 windows XP 时test Profile 将会被触发。profile idtest/id activation os nameWindows XP/name familyWindows/family archx86/arch version5.1.2600/version /os /activation /profile现在打开命令控制台跳转到 pom.xml 所在目录并执行下面的 mvn 命令。不要使用 -P 选项指定 Profile 的名称。Maven 将显示被激活的 test Profile 的结果。mvn test5、通过文件的存在或者缺失激活配置文件现在使用 activation 元素包含下面的操作系统信息。当 target/generated-sources/axistools/wsdl2java/com/companyname/group 缺失时test Profile 将会被触发。profile idtest/id activation file missingtarget/generated-sources/axistools/wsdl2java/ com/companyname/group/missing /file /activation /profile现在打开命令控制台跳转到 pom.xml 所在目录并执行下面的 mvn 命令。不要使用 -P 选项指定 Profile 的名称。Maven 将显示被激活的 test Profile 的结果。mvn test❤️❤️❤️本人水平有限如有纰漏欢迎各位大佬评论批评指正如果觉得这篇文对你有帮助的话也请给个点赞、收藏下吧非常感谢! Stay Hungry Stay Foolish 道阻且长,行则将至,让我们一起加油吧