(...) これは、たとえば 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
リソース プラグインを構成し、ノードごとに 1 つを定義して、特定の出力ディレクトリと特定のフィルタを使用し<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
これはちょっと機能しますが、冗長であり、適切な量のノードがある場合、これは問題になる可能性があります。
for
Maven AntRun Plugin を使用して、より簡潔で保守可能なものを作成しようとしましたが、タスクをMaven で動作させることができず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
しかし、これらの値はノード固有のように見えるため、代わりにシステム プロパティを使用することを検討しましたか (起動スクリプトに配置できます)。これは私がすでに( を使用してlog4j.xml
)行ったことであり、うまく機能し、物事を非常に単純化します。