|
||||||||||||
|
>>分享流行的Java框架以及开源软件,对孙卫琴的《精通Spring:Java Web开发技术详解》提供技术支持 | 书籍支持 |  卫琴直播 |  品书摘要 |  在线测试 |  资源下载 |  联系我们 |
Java技术网站 → 『 Java开发技术 』 → 『 Spring框架和各种开源软件技术专题 』 → Maven 的配置文件 |
您是本文章第 21422 个阅读者 |
|
注意:这三个配置文件并不是代表构建配置文件的功能,而是用于本次测试的目的;比如,我指定了构建配置文件为 prod 时,项目就使用 envprod.properties文件。 注意:下面的例子仍然是使用 AntRun 插件,因为此插件能绑定 Maven 生命周期阶段,并通过 Ant 的标签不用编写一点代码即可输出信息、复制文件等,经此而已。其余的与本次构建配置文件无关。 1、配置文件激活profile 可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个 profile,然后每个 profile 对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。以下实例,我们将 maven-antrun-plugin:run 目标添加到测试阶段中。这样我们可以在不同的 profile 中输出文本信息。我们将使用 pom.xml 来定义不同的 profile,并在命令控制台中使用 maven 命令激活 profile。 pom.xml 文件如下:
注意: 构建配置文件采用的是 <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 文件,增加
执行命令: mvn test 提示 1:此时不需要使用 -Ptest 来输入参数了,上面的 setting.xml 文件的 <activeprofile> 已经指定了 test 参数代替了。 提示 2:同样可以使用在 %M2_HOME%/conf/settings.xml 的文件进行配置,效果一致。 执行结果: 3、通过环境变量激活配置文件 先把上一步测试的 setting.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 http://maven.apache.org/maven-v4_0_0.xsd " > < modelVersion > 4.0.0 </ modelVersion > < groupId > com.jsoft.test </ groupId > < artifactId > testproject </ artifactId > < packaging > jar </ packaging > < version > 0.1-SNAPSHOT </ version > < name > testproject </ name > < url > http://maven.apache.org </ url > < dependencies > < dependency > < groupId > junit </ groupId > < artifactId > junit </ artifactId > < version > 3.8.1 </ version > < scope > test </ scope > </ dependency > </ dependencies > < profiles > < profile > < id > test </ id > < activation > < property > < name > env </ name > < value > test </ value > </ property > </ activation > < build > < plugins > < plugin > < groupId > org.apache.maven.plugins </ groupId > < artifactId > maven-antrun-plugin </ artifactId > < version > 1.8 </ version > < executions > < execution > < phase > test </ phase > < goals > < goal > run </ goal > </ goals > < configuration > < tasks > < echo > Using env.test.properties </ echo > < copy file = " src/main/resources/env.test.properties " tofile = " ${project.build.outputDirectory}/env.properties " overwrite = " true " /> </ tasks > </ configuration > </ execution > </ executions > </ plugin > </ plugins > </ build > </ profile > < profile > < id > normal </ id > < build > < plugins > < plugin > < groupId > org.apache.maven.plugins </ groupId > < artifactId > maven-antrun-plugin </ artifactId > < version > 1.8 </ version > < executions > < execution > < phase > test </ phase > < goals > < goal > run </ goal > </ goals > < configuration > < tasks > < echo > Using env.properties </ echo > < copy file = " src/main/resources/env.properties " tofile = " ${project.build.outputDirectory}/env.properties " overwrite = " true " /> </ tasks > </ configuration > </ execution > </ executions > </ plugin > </ plugins > </ build > </ profile > < profile > < id > prod </ id > < build > < plugins > < plugin > < groupId > org.apache.maven.plugins </ groupId > < artifactId > maven-antrun-plugin </ artifactId > < version > 1.8 </ version > < executions > < execution > < phase > test </ phase > < goals > < goal > run </ goal > </ goals > < configuration > < tasks > < echo > Using env.prod.properties </ echo > < copy file = " src/main/resources/env.prod.properties " tofile = " ${project.build.outputDirectory}/env.properties " overwrite = " true " /> </ tasks > </ configuration > </ execution > </ executions > </ plugin > </ plugins > </ build > </ profile > </ profiles > </ project > |
< profile > < id > test </ id > < activation > < os > < name > Windows XP </ name > < family > Windows </ family > < arch > x86 </ arch > < version > 5.1.2600 </ version > </ os > </ activation > </ profile > |
< profile > < id > test </ id > < activation > < file > < missing > target/generated-sources/axistools/wsdl2java/ com/companyname/group </ missing > </ file > </ activation > </ profile > |
参考:https://www.cnblogs.com/EasonJim/p/6828743.html程序猿的技术大观园:www.javathinker.net
IP: 已设置保密 |
|
威望: 0 级别: 新手上路 魅力: 经验: 现金: 2944 发文章数: 243 篇 注册时间: 0001-01-01 |
|
|||||||
2022-10-28 20:04:55 |
|
|
中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2 荟萃Java程序员智慧的结晶,分享交流Java前沿技术。 联系我们 如有技术文章涉及侵权,请与本站管理员联系。 |