Home > Back-end >  Switch SLF4J SimpleLogger properties file with Maven
Switch SLF4J SimpleLogger properties file with Maven

Time:05-31

I've got a typical Maven project structure with the simplelogger.properties file on the classpath:

 MyProject
`-- src
    |-- main/java/com.mypackage.MyClass
    `-- resources
        `-- simplelogger.properties

This works perfectly from the IDE as the correct SimpleLogger configuration is already on the classpath. I'd like to have another simplelogger.properties file in the packaged finished .jar (output to file instead of to System.out).

Is there a way to replace the simplelogger.properties on the classpath file with another simplelogger.properties file from somewhere else with Maven when packaging a .jar?

CodePudding user response:

If your jar file has already been built you could override your properties:

java -jar myjar.jar --spring.config.location=D:\wherever\new-logger.properties

However, I checked it for Spring Boot projects.

You could check more information here:

CodePudding user response:

When you're running your application, you can place the simplelogger.properties file in your class path or working directory of your program.

`-- work-dir
    |-- myApp.jar
    `-- simplelogger.properties

If your application runs properly in IDE and if it isn't running properly when packaged, maybe your JAR file doesn't contain the resources. In that case, you can use the following configuration in your pom.xml file to copy the resources to your JAR file.

<configuration>
    <outputDirectory>${basedir}/target/classes</outputDirectory>
    <includeEmptyDirs>true</includeEmptyDirs>
    <resources>
        <resource>
            <directory>${basedir}/src/customize</directory>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>${basedir}/src/resources</directory>
            <filtering>false</filtering>
        </resource>
    </resources>
</configuration>

CodePudding user response:

This is somewhat an X-Y problem. I would suggest doing this instead:

Have two modules. The one you have, and put your simplelogger configuration file in the test part of the classpath. This will ensure it doesn't go in the resulting jar (which is much of the reason for your current problem).

Then have a deployment module which adds the deployment configuration file to your artifact from above in the form you need it. When you get to needing multiple deployments just create a module for each.

  • Related