Maven:如何使用不同的属性值多次过滤相同的资源?

时间:2010-07-22 11:36:03

标签: 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
...

,而节点2的log4j.properties看起来像

...
log4j.appender.Application.File=D:/logs/application_2.log
...
log4j.appender.tx_info.File=D:/logs/tx_info_2.log
...

我们已经使用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
...

使用不同的${log.file.postfix}值多次运行Maven很容易,每次都会生成一个不同的日志属性文件。但是,我想要的是为一个版本中的每个服务器生成一个不同的属性文件(名称/路径不同)。我很确定这可以做到,例如通过antrun插件,但我不熟悉。实现这一目标的最简单方法是什么?

4 个答案:

答案 0 :(得分: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

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>

运行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

每个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插件编写更简洁和可维护的内容但是我无法从for获得ant-contrib任务在Maven下工作(原因不明, 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

但是,由于这些值似乎是特定于节点的,您是否考虑使用系统属性(您可以将其置于启动脚本中)?这是我已经完成的事情(使用log4j.xml),它运行良好,可以大大简化事情。

答案 1 :(得分:3)

虽然这有点旧,但我最近遇到了这个帖子,并希望使用iterator-maven-plugin提出更新的解决方案。可在此处找到概述:http://khmarbaise.github.io/iterator-maven-plugin/

如何实现目标的一个具体示例是将迭代器插件与复制资源和过滤器相结合。您甚至可以添加自定义属性文件以用作过滤器,以防您使用此方法获得每个节点唯一的其他属性。

<plugin>

    <groupId>com.soebes.maven.plugins</groupId>
    <artifactId>iterator-maven-plugin</artifactId>
    <version>0.3</version>

    <executions>
        <execution>
            <id>configure-log-properties</id>
            <phase>validate</phase>
            <goals>
                <goal>iterator</goal>
            </goals>
            <configuration>
                <iteratorName>log.file.postfix</iteratorName>
                <content>1,2,3,4,5</content>
                <pluginExecutors>

                    <pluginExecutor>
                        <plugin>
                            <artifactId>maven-resources-plugin</artifactId>
                            <version>2.7</version>
                        </plugin>

                        <goal>copy-resources</goal>

                        <configuration>
                            <outputDirectory>${project.build.directory}/nodes/${log.file.postfix}</outputDirectory>

                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                    <includes>
                                        <include>log4j.properties</include>
                                    </includes>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>

                    </pluginExecutor>
                </pluginExecutors>
            </configuration>
        </execution>
    </executions>
</plugin>

答案 2 :(得分:1)

以下是您可以尝试的一些方法:

  1. 使用antrun插件和copy任务在generate-resources阶段制作资源文件的副本。在copying with maven的问题答案中给出了使用antrun插件的一个例子。您甚至可以使用ant的属性扩展将每个$ {log.file.postfix}扩展为一个不同的值,即文字值1,2,3等或唯一占位符,$ {log.file.postfix1},$ { log.file.postfix2},当maven进行资源过滤时,它们最终被替换。

  2. 使用您的版本控制系统设置同一文件的多个副本,而不是使用antrun。然后,您可以运行resources:copy-resources目标的多个实例,每个实例都配置了不同的属性值,并且具有不同的目标文件名。

答案 3 :(得分:0)

如果需要复制很多目标配置,可以将maven-antrun-plugin与ant macrodef一起使用。

  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.3</version>
    <executions>
      <execution>
        <id>copy-resources-all-nodes</id>
        <phase>process-resources</phase>
        <configuration>
          <tasks>
             <macrodef name="copyConfigFile">
                <attribute name="node"/>

                <sequential>
                   <copy file="src/main/resources/log4j.properties"
                         toFile="target/antrun/log4j-@{node}.properties">
                      <filterset>
                         <!-- put the node-specific config in property files node1.properties etc -->
                         <filtersfile file="config/@{node}.properties"/>
                      </filterset>
                   </copy>
                </sequential>
             </macrodef>

             <copyConfigFile node="node1"/>
             <copyConfigFile node="node2"/>
             <copyConfigFile node="node3"/>
             ...

          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

如果你真的有很多目标配置,你也可以使用ant-contrib来迭代目标配置列表。

有一个例子如何执行此操作here