Maven:如何使用来自 1 个项目的不同 log4j 配置构建 2 个 jar
Maven: how to build 2 jar with different log4j config from 1 project
我有一个包含 2 个主要 类 的 Maven 项目。我想生成 2 个不同 log4j2.properties
的 jar,我尝试了 copy-rename-maven-plugin 和 2 个执行 ID,但它不起作用。
processA_log4j2.properties
processB_log4j2.properties
src
--com.exampleA
----ProcessA.java
--com.exampleB
----ProcessB.java
--log4j.properties
test
--com.example.test
--log4j.properties
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<testResources>
<testResource>
<directory>test</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>rename-file1</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<sourceFile>src/processB_log4j2.properties</sourceFile>
<destinationFile>src/log4j2.properties</destinationFile>
<overWrite>true</overWrite>
</configuration>
</execution>
<execution>
<id>rename-file2</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<sourceFile>src/processA_log4j2.properties</sourceFile>
<destinationFile>src/log4j2.properties</destinationFile>
<overWrite>true</overWrite>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId><version>3.3.0</version>
<executions>
<execution>
<id>make-assembly1</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.exampleB.ProccessB</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>ProccessB</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
<execution>
<id>make-assembly2</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.exampleA.ProccessA</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>ProccessA</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resource1</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/../Tmp_output-bin</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<includes>
<include>ProccessB.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-resource2</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/../Tmp_output-bin</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<includes>
<include>ProccessA.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>${project.artifactId}</finalName>
</build>
我找不到使用 maven 执行此操作的方法,因此我在每个主要 class.
的代码中覆盖了配置
https://logging.apache.org/log4j/2.0/faq.html#reconfig_from_code
我有一个包含 2 个主要 类 的 Maven 项目。我想生成 2 个不同 log4j2.properties
的 jar,我尝试了 copy-rename-maven-plugin 和 2 个执行 ID,但它不起作用。
processA_log4j2.properties
processB_log4j2.properties
src
--com.exampleA
----ProcessA.java
--com.exampleB
----ProcessB.java
--log4j.properties
test
--com.example.test
--log4j.properties
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<testResources>
<testResource>
<directory>test</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>rename-file1</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<sourceFile>src/processB_log4j2.properties</sourceFile>
<destinationFile>src/log4j2.properties</destinationFile>
<overWrite>true</overWrite>
</configuration>
</execution>
<execution>
<id>rename-file2</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<sourceFile>src/processA_log4j2.properties</sourceFile>
<destinationFile>src/log4j2.properties</destinationFile>
<overWrite>true</overWrite>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId><version>3.3.0</version>
<executions>
<execution>
<id>make-assembly1</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.exampleB.ProccessB</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>ProccessB</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
<execution>
<id>make-assembly2</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.exampleA.ProccessA</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>ProccessA</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resource1</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/../Tmp_output-bin</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<includes>
<include>ProccessB.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-resource2</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/../Tmp_output-bin</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<includes>
<include>ProccessA.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>${project.artifactId}</finalName>
</build>
我找不到使用 maven 执行此操作的方法,因此我在每个主要 class.
的代码中覆盖了配置https://logging.apache.org/log4j/2.0/faq.html#reconfig_from_code