Getting a logback.xml from a dependency jar and filtering it with the maven-war-plugin

1.2k Views Asked by At

We have a common logback.xml file that we would like to use across different web apps. It includes a RollinFileAppender which should see files named as the project artifactId. The logback.xml includes a property like so

<property name="LOG_FILE_NAME" value="$project.artifactId}"/>

Within our web project we would like to include a dependency e.g. logging-setup

<dependency>
    <groupId>our.group.id</groupId>
    <artifactId>logging-setup</artifactId>
</dependency>

How do we easily allow the maven-war-plugin to filter this file so that the ${project.artifactId} reference is replaced with the actual project.artifactId? I think it can be done using a combination of the maven-dependency-plugin and the maven-war-plugin something like below. However that would need to be included in every project POM. Is there an easier way?

Thanks, Paul

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>${maven-dependency-plugin.version}</version>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>our.group.id</groupId>
                                    <artifactId>logging-setup</artifactId>
                                    <version>${logging-setup.version}</version>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${basedir}/src/main/resources</outputDirectory>
                                    <includes>**/logback.xml</includes>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <webResources>
                        <resource>
                            <directory>${basedir}/src/main/resources</directory>
                            <filtering>true</filtering>
                            <targetPath>WEB-INF/classes</targetPath>
                            <includes>
                                <include>logback.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                    <packagingExcludes>WEB-INF/lib/logging-setup-*.jar</packagingExcludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
1

There are 1 best solutions below

1
Thorbjørn Ravn Andersen On

After this logback.xml file is put inside an artifact jar file, it cannot be changed easily anymore during build time.

I would suggest a slightly different approach:

  • rename this file to e.g. logback-YOURCOMPANY.xml and change it so it can be included.
  • create a tiny logback.xml file put in each project as-is which sets the property as you do, and then includes logback-YOURCOMPANY.xml
  • enable filtering on this tiny logback.xml file so the variable is expanded during Maven build.

See https://logback.qos.ch/manual/configuration.html#fileInclusion for details.