之前的文章“profile与资源过滤”介绍过,通过激活不同的profile,可以改变同一个文件中的${propertyName}占位符。也就是说:通过profile与资源过滤,我们实现了:同一个文件,打包到不同的环境,文件的内容不同。
我们项目有这样的场景:我们知道log4j框架会自动加载classpath下的log4j.properties文件。我们开发环境和生产环境的日志配置文件名是:log4j-dev.properties,log4j-online.properties,虽然这2个文件都在classpath下,但是log4j并不会自动去加载,因为文件名称不是log4j.properties。
也就是说,我们想实现这样的效果:打dev环境的包时候,将log4j-dev.properties复制到log4j.properties;打online环境的包时候,将log4j-online.properties复制到log4j.properties。这样启动项目的时候,log4j框架就能自动加载配置文件了。通过maven-antrun-plugin插件可以实现这样的需求:
<!--${log.conf}读取profile下的属性值-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<move file="${project.build.directory}/classes/${log.conf}"
tofile="${project.build.directory}/classes/log4j.properties" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>