maven,maven-resources-plugin根据不同环境覆盖配置文件

一、maven-resources-plugin

利用maven-resources-plugin 插件,根据不同环境 复制不同环境配置文件到指定目录。

1、pom配置如下:

<build>
		<!-- <finalName>product-experience</finalName> -->
		<finalName>ROOT</finalName>

		<!-- 主资源目录 -->
		<resources>
			<resource>
				<!-- 设置主资源目录 -->
				<directory>src/main/resources</directory>
				<!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,是否对主资源目录开启资源过滤 -->
				<filtering>true</filtering>
				<!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,只处理如下配置中包含的资源类型 -->
				<!-- 包括文件 -->
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
				</includes>
				<!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,不处理如下配置中包含的资源类型(剔除下如下配置中包含的资源类型)-->
				<!-- 排除文件,这些文件打包的时候应该被排除 -->
				<excludes>
					<exclude>dev/*.properties</exclude>
					<exclude>test/*.properties</exclude>
					<exclude>prod/*.properties</exclude>
				</excludes>
			</resource>
		</resources>

		<plugins>
			<!-- 复制指定环境配置文件到指定目录 -->
			<plugin>
				<artifactId>maven-resources-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-resources</id>
						<phase>package</phase>
						<goals>
							<goal>copy-resources</goal>
						</goals>
						<configuration>
							<resources>
								<resource>
									<directory>src/main/resources/app/${profiles.active}</directory>
									<includes>
										<include>app.properties</include>
									</includes>
								</resource>
							</resources>
							<outputDirectory>${project.build.directory}/classes/app</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

2、war中的配置文件没有被替换

打完包之后,查看target/class/app/app.properties文件已经被正常替换,但打开ROOT.war文件,查看对应app.properties,文件没有被替换。查看打包日志如下:

D:\>mvn clean package -P test
[INFO] Scanning for projects...
Downloading from nexus: 
//这一段都是从中央仓库下载jar包 ,省略。。。。。。。。
[INFO]
//执行clean阶段
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ test ---
[INFO] Deleting D:\myFile\test\target
[INFO]
//执行 default-resources 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 9 resources
[INFO]
//(default-compile.........................
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ test ---
[INFO] Changes detected - recompiling the module!
//将编译后的文件都放到了target\classes目录下
[INFO] Compiling 69 source files to D:\myFile\test\target\classes

[INFO]
//default-testResources
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\myFile\test\src\test\resources
[INFO]
//default-testCompile
[INFO] --- maven-compiler-plugin:3.2:testCompile (default-testCompile) @ test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\myFile\test\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ test ---
[WARNING] The parameter forkMode is deprecated since version 2.14. Use forkCount and reuseForks instead.
[WARNING] useSystemClassloader setting has no effect when not forking
[INFO]
[INFO] --- maven-war-plugin:2.5:war (default-war) @ test ---
//将webapp 目录加到 ROOT下
[INFO] Packaging webapp
[INFO] Assembling webapp [test] in [D:\myFile\test\target\ROOT]
[INFO] Processing war project
[INFO] Copying webapp resources [D:\myFile\test\src\main\webapp]
[INFO] Webapp assembled in [2733 msecs]
//打包war
[INFO] Building war: D:\myFile\test\target\ROOT.war
[INFO]
//打完包后,才开始执行copy-resources,所以打成的ROOT.war包的配置没有改变
[INFO] --- maven-resources-plugin:2.6:copy-resources (copy-resources) @ test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  18.833 s
[INFO] Finished at: 2020-08-21T13:27:46+08:00
[INFO] ------------------------------------------------------------------------

观察上面日志执行流程如下:

//执行clean阶段
//执行 default-resources 
//(default-compile.........................
//将编译后的文件都放到了target\classes目录下
//default-testResources
//default-testCompile
//将webapp 目录加到 ROOT下
//打包war
//打完包后,才开始执行copy-resources,所以打成的ROOT.war包的配置没有改变

根据以上观察,复制配置文件,是在打包war之后才进行的,所以配置war包下的配置文件没有被替换,而target下的配置文件被替换。

3、修改打包 phase

从上面日志可以看出,是打包阶段出了问题,需要将复制配置文件这个阶段放到 打成war之前,
在此前,我要了解一下maven的生命周期,然后才能知道 将这个修改成哪个阶段

4、maven生命周期

Maven 构建生命周期就是 Maven 将一个整体任务划分为一个个的阶段,类似于流程图,按顺序依次执行。也可以指定该任务执行到中间的某个阶段结束。
Maven 的内部有三个构建生命周期,分别是 clean, default, site。其中 default 生命周期的核心阶段如下所示:
在这里插入图片描述

4.1、如何指定生命周期

mvn install

执行 mvn install 命令,将完成 validate, compile, test, package, verify, install 阶段,并将 package 生成的包发布到本地仓库中。其中某些带有连字符的阶段不能通过 shell 命令单独指定。

mvn clean deploy

执行 mvn clean deploy 命令,首先完成的 clean lifecycle,将以前构建的文件清理,然后再执行 default lifecycle 的 validate, compile, test, package, verify, insstall, deploy 阶段,将 package 阶段创建的包发布到远程仓库中。

4.2 阶段与插件的关系

Maven 将构建过程定义为 default lifecycle,并将 default lifecycle 划分为一个个的阶段 phase,这一系列 phase 仅仅是规定执行顺序,至于每个阶段做什么工作?由谁来做?答案就在 插件(plugins) 中。
Maven 对工程的所有操作实实在在的都是由 插件 来完成的。一个插件可以支持多种功能,称之为目标(goal),例如:compiler 插件有两个目标:compile 和 testCompile,分别实现编译源代码 和 编译测试代码。
如何将插件与 Maven 的构建生命周期绑定在一起呢?通过将插件的目标(goal)与 build lifecycle 中 phase 绑定到一起,这样,当要执行某个 phase 时,就调用插件来完成绑定的目标。

每一个阶段可以绑定0 个 或 多个目标,每个插件可以提供 1 个或多个目标。
在这里插入图片描述

4.3、为自己的工程构建生命周期

4.3.1、不同的packing类型有不同的生命周期

packaging 类型为 jar 时,phase 与 plugin goal 的映射关系。

在这里插入图片描述

4.3.2 maven的默认plugin

默认的 pom.xml 文件并没有配置各种 plugin,但是也能正常构建工程?是因为 Maven 自己默认指定了 plugin。

下面是一个没有配置任何 plugin 的 pom.xml,
执行 mvn install 的输出日志,从日志中可以看到 一系列的 插件(plugin):版本号:目标(phase),例如
maven-resources-plugin:2.6:resources (default-resources),
maven-compiler-plugin:3.1:compile (default-compile) ,
maven-resources-plugin:2.6:testResources (default-testResources),
maven-compiler-plugin:3.1:testCompile (default-testCompile),
maven-surefire-plugin:2.12.4:test (default-test),
maven-jar-plugin:2.4:jar (default-jar) ,
maven-install-plugin:2.4:install (default-install),

5、完整的 clean, default, site build lifecycle

5.1、clean lifecycle

在这里插入图片描述

5.2、default lifecycle

phasefunction
validatevalidate the project is correct and all necessary information is available.
initializeinitialize build state, e.g. set properties or create directories.
generate-sourcesgenerate any source code for inclusion in compilation.
process-sourcesprocess the source code, for example to filter any values.
generate-resourcesgenerate resources for inclusion in the package.
process-resourcescopy and process the resources into the destination directory, ready for packaging.
compile compile thesource code of the project.
process-classespost-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
generate-test-sourcesgenerate any test source code for inclusion in compilation.
process-test-sourcesprocess the test source code, for example to filter any values.
generate-test-resourcescreate resources for testing.
process-test-resourcescopy and process the resources into the test destination directory.
test-compilecompile the test source code into the test destination directory
process-test-classespost-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
testrun tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
prepare-packageperform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
packagetake the compiled code and package it in its distributable format, such as a JAR.
pre-integration-testperform actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-testprocess and deploy the package if necessary into an environment where integration tests can be run.
post-integration-testperform actions required after integration tests have been executed. This may including cleaning up the environment.
verifyrun any checks to verify the package is valid and meets quality criteria.
installinstall the package into the local repository, for use as a dependency in other projects locally.
deploydone in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

5.3、site lifecycle

在这里插入图片描述

6、将阶段改为:process-resources

再次打包,观察日志如下:

[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ test ---
[INFO] Deleting D:\myFile\test\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 9 resources
[INFO]
[INFO] --- maven-resources-plugin:2.6:copy-resources (copy-resources) @ test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]

[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ test---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 69 source files to D:\myFile\test\target\classes

[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\myFile\test\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.2:testCompile (default-testCompile) @ test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\myFile\test\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ test ---
[WARNING] The parameter forkMode is deprecated since version 2.14. Use forkCount and reuseForks instead.
[WARNING] useSystemClassloader setting has no effect when not forking
[INFO]
[INFO] --- maven-war-plugin:2.5:war (default-war) @ test ---
[INFO] Packaging webapp
[INFO] Assembling webapp [test] in [D:\myFile\test\target\ROOT]
[INFO] Processing war project
[INFO] Copying webapp resources [D:\myFile\test\src\main\webapp]
[INFO] Webapp assembled in [2056 msecs]
[INFO] Building war: D:\myFile\test\target\ROOT.war

再次查看target和ROOT.war下的文件 已经正常替换。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值