Pét*_*rök 17 java resources maven-2 filtering maven-antrun-plugin
我们的项目使用Log4J,通过log4j.properties文件配置.我们有多个生产服务器,可以记录到不同的日志文件,以便区分日志.所以节点1的log4j.properties如下所示:
...
log4j.appender.Application.File=D:/logs/application_1.log
...
log4j.appender.tx_info.File=D:/logs/tx_info_1.log
...
Run Code Online (Sandbox Code Playgroud)
而节点2的log4j.properties看起来像
...
log4j.appender.Application.File=D:/logs/application_2.log
...
log4j.appender.tx_info.File=D:/logs/tx_info_2.log
...
Run Code Online (Sandbox Code Playgroud)
我们已经使用Maven配置文件生成我们的服务器配置.到目前为止,它包含几个不同的log4j.properties文件,这些文件仅在日志文件名中有所不同,如上所示.我想使用像这样的资源模板文件使用Maven生成这些文件:
...
log4j.appender.Application.File=${log.location}/application${log.file.postfix}.log
...
log4j.appender.tx_info.File=${log.location}/tx_info${log.file.postfix}.log
...
Run Code Online (Sandbox Code Playgroud)
使用不同的${log.file.postfix}值多次运行Maven很容易,每次都会生成一个不同的日志属性文件.但是,我想要为一个构建中的每个服务器生成一个不同的属性文件(名称/路径不同).我相信这可以做到,例如通过antrun插件,但我不熟悉.实现这一目标的最简单方法是什么?
Pas*_*ent 15
(...)我很确定这可以做到,例如通过antrun插件,但我不熟悉.实现这一目标的最简单方法是什么?
您确实可以在POM中使用resources:copy-resources几个<execution>(请注意,resources:copy-resources虽然不允许更改目标文件的名称).
假设您有以下结构:
$ tree .
.
??? pom.xml
??? src
??? main
? ??? filters
? ? ??? filter-node1.properties
? ? ??? filter-node2.properties
? ??? java
? ??? resources
? ??? log4j.properties
? ??? another.xml
??? test
??? java
Run Code Online (Sandbox Code Playgroud)
凡log4j.properties使用占位符和filter-nodeN.properties文件包含的值.例如:
# filter-node1.properties
log.location=D:/logs
log.file.postfix=_1
Run Code Online (Sandbox Code Playgroud)
然后,在您的pom.xml配置资源插件中,定义<execution>每个节点一个,以copy-resources使用特定的输出目录和要使用的特定过滤器进行调用:
<project>
...
<build>
<resources>
<!-- this is for "normal" resources processing -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering><!-- you might still want to filter them -->
<excludes>
<!-- we exclude the file from "normal" resource processing -->
<exclude>**/log4j.properties</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources-node1</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/node1</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/log4j.properties</include>
</includes>
</resource>
</resources>
<filters>
<filter>src/main/filters/filter-node1.properties</filter>
</filters>
</configuration>
</execution>
<execution>
<id>copy-resources-node2</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/node2</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/log4j.properties</include>
</includes>
</resource>
</resources>
<filters>
<filter>src/main/filters/filter-node2.properties</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
运行mvn process-resources会产生以下结果:
$ tree .
.
??? pom.xml
??? src
? ??? main
? ? ??? filters
? ? ? ??? filter-node1.properties
? ? ? ??? filter-node2.properties
? ? ??? java
? ? ??? resources
? ? ??? log4j.properties
? ? ??? another.xml
? ??? test
? ??? java
??? target
??? classes
? ??? another.xml
??? node1
? ??? log4j.properties
??? node2
??? log4j.properties
Run Code Online (Sandbox Code Playgroud)
每个都有适当的值log4j.properties.
$ cat target/node1/log4j.properties
log4j.appender.Application.File=D:/logs/application_1.log
log4j.appender.tx_info.File=D:/logs/tx_info_1.log
Run Code Online (Sandbox Code Playgroud)
这有点工作,但是很冗长,如果你有相当数量的节点,这可能是一个问题.
我尝试使用Maven AntRun插件编写更简洁和可维护的东西,但我无法让.for任务从ant-contribMaven下工作(原因不明,for任务无法识别)我放弃了
这是使用Maven AntRun插件的替代方案.没有什么复杂的,没有循环,我只是将源文件复制到另一个位置,动态更改其名称并过滤内容:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>copy-resources-all-nodes</id>
<phase>process-resources</phase>
<configuration>
<tasks>
<copy file="src/main/resources/log4j.properties" toFile="target/antrun/log4j-node1.properties">
<filterset>
<filter token="log.location" value="D:/logs"/>
<filter token="log.file.postfix" value="_1"/>
</filterset>
</copy>
<copy file="src/main/resources/log4j.properties" toFile="target/antrun/log4j-node2.properties">
<filterset>
<filter token="log.location" value="D:/logs"/>
<filter token="log.file.postfix" value="_2"/>
</filterset>
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
请注意,Ant @默认使用令牌作为分隔符(无法使其使用maven样式分隔符),因此log4j.properties变为:
log4j.appender.Application.File=@log.location@/application@log.file.postfix@.log
log4j.appender.tx_info.File=@log.location@/tx_info@log.file.postfix@.log
Run Code Online (Sandbox Code Playgroud)
但是,由于这些值似乎是特定于节点的,您是否考虑使用系统属性(可以放在启动脚本中)?这是我已经完成的事情(有了a log4j.xml),它运行良好,它会极大地简化事情.
您可以尝试以下一些方法:
使用 antrun 插件,以及copy在该阶段复制资源文件的任务generate-resources。关于使用 maven 进行复制的问题的答案中给出了使用 antrun 插件的示例。您甚至可以使用 ant 的属性扩展将每个 ${log.file.postfix} 扩展为不同的值,可以是文字值 1,2,3 等,也可以是唯一的占位符 ${log.file.postfix1}, ${ log.file.postfix2} 最终在maven进行资源过滤时被替换。
不要使用 antrun,而是使用版本控制系统来设置同一文件的多个副本。然后,您可以运行resources:copy-resources目标的多个实例,每个实例都配置不同的属性值和不同的目标文件名。