javalogginglogback

specify logback.xml custom location


I have 2 files: logback.xml and logback-local.xml

I am trying to build war file based on logback-local.xml

I am trying to use command with adding file location like this

mvn clean package -DskipTests -DLogback.configurationFile=cdi-web/src/main/jobs/logback-local.xml

Maven giving me the error

[ERROR] Unknown lifecycle phase ".configurationFile=cdi-web/src/main/jobs/logback-local.xml"

Please advice how to resolve

Thanks in advance


Solution

  • -Dlogback.configurationFile(aware the lowercase log) should be set in runtime, and not during the build. i.e. it should be set when running the web application, instead of building the WAR.

    In your case, this could be done by (elaborate on life888888 comment)

    1. exclude other environment logback file(logback-xxx.xml)
    2. include only the desired environment logback file
    3. rename the file from logback-xxx.xml to logback.xml(set it to default so there is no need to specify the config file when running the application)

    To configure use different logback file for different environment, we can setup profile for each environment.

    Below pom.xml snippet demonstrate what mentioned above

    <?xml version="1.0" encoding="UTF-8"?>
    <project
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
            xmlns="http://maven.apache.org/POM/4.0.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        ...
        <profiles>
            <!-- The configuration of the development profile -->
            <profile>
                <id>local</id>
                <properties>
                    <logback.file>logback-local.xml</logback.file>
                </properties>
            </profile>
            <profile>
                <id>dev</id>
                <properties>
                    <logback.file>logback-dev.xml</logback.file>
                </properties>
            </profile>
            ...
        </profiles>
    
        <dependencies>
            ...
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.2.0</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <excludes>
                                    <include>logback*.xml</include>
                                </excludes>
                            </resource>
                            <resource>
                                <directory>src/main/resources</directory>
                                <includes>
                                    <include>${logback.file}</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>com.coderplus.maven.plugins</groupId>
                    <artifactId>copy-rename-maven-plugin</artifactId>
                    <version>1.0.1</version>
                    <executions>
                        <execution>
                            <id>rename-file</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>rename</goal>
                            </goals>
                            <configuration>
                                <sourceFile>${project.build.outputDirectory}/${logback.file}</sourceFile>
                                <destinationFile>${project.build.outputDirectory}/logback.xml</destinationFile>
                                <overWrite>true</overWrite>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                ...
            </plugins>
        </build>
    </project>