Injecting log level to logback.xml with maven - java

I have a java swing project that will end up in a fat client. There is no JEE background, no application server or servlet container will be involved. Nor any other runtime container then a plain JVM.
I am using the build tool maven to manage dependencies and to build the application.
I am using logback api for logging purposes.
Now I have two build profiles, one for building a developer version and one for building the final version.
Is it possible to set values to the logback.xml file using a maven property? For example, I have the following <root level="${log.level}"> tag in my logback.xml. Now I want to define the value of ${log.level} in my maven build profile. Is it possible?
This question is not related to

It is certainly possible, with maven resource filtering.
https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
I would strongly recommend not to do it this way though. Managing 2 binaries just for the sake of having different properties seems an overkill.
I would recommend some setting on the target host (env variable, parameter to your program) that will allow you to select log level at runtime.
Passing log level, name or path of logging config, or name of target environment (dev/prod) are all acceptable solutions.

Related

Best Way to Remove Functionality from Java Spring Boot App at Build

I'm looking for a way to conditionally remove functionality from my Spring Boot app during the build. Whether that is a Maven plugin that strips out the actual Java classes or packages, or just is able to set a variable that cannot be later changed environmentally.
My spring boot app has a bunch of reports that can be generated but they shouldn't all be available in every environment they are deployed in. I would like an easy build config file where I can set which reports are enabled and disabled before build. I need them hard set at build, so someone with control of the environment can't just go in and update an associated property value and they become enabled again.
I was looking for some plugin that would remove classes or packages, or that could hardcode the variable value into the compiled class files but couldn't find anything. I figure there must be some way to make configuration stuck at build time. Have been unsuccessful finding a way to set properties at build that cannot be overridden.

How to manage Environment and Region/location Specific Configuration using Maven

We have following 3 [DEV,STAGING,PROD] environment. And multiple regions/location under each environment example [ ASIA,EUROPE,US ]. What is best way we can configure the project using maven so that it can pick correct configuration xml and properties based on the environment and region.
For example: if we want to test/build our application in STAGING on US region, STAGING-US.xml , STAGING-US.properties should be taken during the build.
Our application is Maven based .
It would be great help if you could suggest any other better alternative also, we are trying to use maven profile. But it seems we have to create multiple profiles.

Is there a way to declaratively configure the classpath used by maven itself (specifically to configure which slf4j implementation maven uses)?

What I am trying to do is to configure the logging implementation used by maven.
(i.e. mvn itself, not the project being built/tested).
I know that I can do this by dropping logback jars into $MAVEN_HOME/libexec/lib and dropping logback.xml into $MAVEN_HOME/confi/logging, but I would like to know whether there is a declarative way to do this, which doesn't involve physically copying jars.
I had the impression that this kind of thing might be possible using maven profiles but this seems like something which would need to be set before the mvn process is even started.

log4j2 logging for a Java SDK

Let's say we build a Java SDK and different projects can consume by adding it as a jar in the classpath or adding it as a dependency in the maven pom.xml or gradle file. Logs from the SDK are not visible at run-time when the other projects are consuming this library. I tried with SL4J, and none of the logs are visible at the run time when it is used by the other projects. Should I go with log4j2? If yes, should I provide with a log4j configuration /properties file in my SDK? Will the properties/configuration be picked-up at run time from the consumer libraries? What are the best practices for this? Can you please advise?
Best practice #1: never include the logging configuration file in your jar.
Applications using your library will likely want to provide their own logging configuration. If one of the jars contains a different configuration it becomes a “fun” guessing game for these applications to figure out why their configuration isn’t working. (And depending on the classpath order, their configuration may sometimes take precedence over the configuration in the library, so sometimes it will work, just to make it extra challenging...)

Avoid getting slf4j from maven

Maven uses slf4j, so as soon as it is launched, it initializes it with its default implementation contained into apache-maven-3.3.9\lib\slf4j-simple-1.7.5.jar and with configuration file defined in apache-maven-3.3.9\conf\logging\simplelogger.properties.
After that it loads the pom file and found my jetty-maven-plugin which launch a webapp. But in this webapp I want to use a different implementation for slf4j, but I can't because slf4j is already initialized.
I understand that maven is mainly a tool for build and not to launch apps, but I can't modify log configuration of apache-maven for each project to get pretty logs for each of them.
Is someone already face this issue and find a way to avoid that?
Note:
run-forked instead run works but in this case I can't no more debug from eclipse so I prefer an another solution.
older version of maven works as 3.0.3 because it didn't used slf4j

Categories