(...) 我很确定这可以做到,例如通过 antrun 插件,但我对此并不熟悉。实现这一目标的最简单方法是什么?
您确实可以在您的 POM 中使用resources:copy-resources
and 几个<execution>
(请注意,resources:copy-resources
虽然不允许更改目标文件的名称)。
假设您具有以下结构:
$ tree .
.
├── pom.xml
└── src
├── main
│ ├── filters
│ │ ├── filter-node1.properties
│ │ └── filter-node2.properties
│ ├── java
│ └── resources
│ ├── log4j.properties
│ └── another.xml
└── test
└── java
在哪里log4j.properties
使用占位符并且filter-nodeN.properties
文件包含值。例如:
# filter-node1.properties
log.location=D:/logs
log.file.postfix=_1
然后,在您的 中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>
运行 mvnprocess-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
在每个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
这有点工作,但很冗长,如果您有大量节点,这可能是个问题。
我尝试使用 Maven AntRun 插件编写更简洁和可维护的东西,但我无法在 Maven 下for
完成任务ant-contrib
(由于未知原因,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>
请注意,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
但是,由于这些值似乎是特定于节点的,您是否考虑改用系统属性(可以放置在启动脚本中)?这是我已经做过的事情(使用 a log4j.xml
),效果很好,并且可以高度简化事情。