spring boot log4j file external to jar? - java

how to pass? The only way I can get this to work is to put log4j.xml on the classpath.
passing: -Dlog4j.configuration=file:///c:\log4j2.xml on the command line doesn't work (although it does in a non spring-boot test application just fine).
I also tried putting this as an environment variable/property in spring.

Try to put this line into your application.properties:
logging.config=file:log4j.xml
Second option is to pass system variable to -Dlogging.config=file:log4j.xml
In this case it is expected to be located in current directory outside of the JAR file.

you must put the "-Dlog4j" before the "-jar XXXX.jar".i try it my own .work for me.

If you are using gradle, and trying to run a test method you will have to add following to the build.gradle. This enables the test method to pick -Dlog4j.configurationFile= to be picked during the test run :
test {
systemProperty "log4j.configurationFile", System.getProperty("log4j.configurationFile")
}

Related

PropertiesLauncher command line arguments not working with Spring Boot executable Jar

So I have basic multi-module Spring Boot project. The goal, that I had was to build executable jar and pass additional properties with the help of -Dloader.path=....
For some reason (if I understand purpose of this argument) loader.path is being ignored completely.
My project structure is following:
\-
|--conf
|---default
|--pets-api
|--pets-app (this module contains the Main-Class)
|--pets-domain
|--pets-infrastructure
Since no custom active profile is being passed it uses "default". Jar contains application-default.propeties file, that has single configuration server.servlet.context-path=/v1.
/conf/default location has 2 properties files:
application.properties
random.properties - this is bind to #ConfigurationProperties(prefix = "...") inside application
When I run it normally everything is fine java -jar pets-app-0.0.1-SNAPSHOT.jar. It just uses application-default.properties file and that is it.
Now when I am trying to utilize -Dloader.path argument as in java -Dloader.path=PATH/TO/conf/default -jar pets-app-0.0.1-SNAPSHOT.jar it starts application same as before, as if I am not adding 2 more file to classpath.
What is used:
Java 17
Spring Boot 2.6.12
Gradle
Did anyone come across this as well? Any suggestion on how to resolve it?
PS. If there is need to see the code, I can upload it to GitHub.

Pass gradle command line arguments to application.properties

I have a Spring Boot Gradle application and many MySQL servers and databases.
In different scenarios, I want to start the application with different databases or create them if they do not exist. I want to use this mechanism with command line arguments.
The URL for database is stored in application.properties file of Spring:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb1?createDatabaseIfNotExist=true&useSSL=true
I want to run gradle with command line arguments that will apply to the application.properties file so in my head will be something like this.
application.properties :
spring.datasource.url=jdbc:mysql://${linkNewDB}?createDatabaseIfNotExist=true&useSSL=true
And to run the program as :
gradle bootRun -Pargs=--linkNewDB="someNewDB:3309"
Does anybody know how can I achieve this mechanism? I tried different options but none of them worked. Thanks!
How about changing the whole URL, like this:
gradle bootRun --args='--spring.datasource.url=jdbc:mysql://someNewDB:3309?createDatabaseIfNotExist=true&useSSL=true'
reference: https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#running-your-application.passing-arguments

How to edit app.properties file with build parameter in Jenkins

I have a selenium script which I want to run from Jenkins. I have a properties file called app.properties. This file consists properties such as:
browser=chrome
I configured my project as parameterized so when I run my build, it asks for browser parameter. I want to select this parameter(for example firefox), so that it will change browser property in app.properties and will run the automation in Firefox.
Normally, when I change the browser property in app.properties in Intellij, my program runs with that browser. So there is nothing wrong with my program in that sense.
Is there a way to change app.properties with respect to my Jenkins build parameter and run the program with that configuration?
EDIT: I found the following solution:
Install surefire plugin.
Add a browser parameter.
In your property managing class, take browser parameter as
System.getProperty("browser");
From jenkins, configure a browser parameter
Invoke maven command: mvn test "-Dbrowser=${BROWSER}"
You can pass system properties to change configuration.
First you should configure your project to read both system properties and configuration file, where system properties will have higher priority. I'd recommend Apache Commons Composite Configuration. It can look like this:
CompositeConfiguration configuration = new CompositeConfiguration();
try {
configuration.addConfiguration(new SystemConfiguration());
configuration.addConfiguration(new PropertiesConfiguration("app.properties"));
} catch (ConfigurationException e) {
e.printStackTrace();
}
//Read your configuration values here
This way when you provide system property -Dbrowser=chrome it will override value from configuration file.
Secondly, you'll need to configure Jenkins job. Since you're passing a parameter you can use it in your build step definition:
mvn clean test -Dbroswer=${browser-param}
“The way parameters take effect is also different depending on the parameter type you choose ... String parameters are exposed as environment variables of the same name.”
https://wiki.jenkins.io/plugins/servlet/mobile?contentId=34930782#content/view/34930782

Spring boot application.yml configuration bugging out

Ok, so, I've noticed a strange bug with spring boot's application.yml configuration.
I have a normal application.yml and take my values as
#Value("${path.to.value}")
private type myVal
This works fine in devel mode when I have my config inside my src/main/res dir however when I upload the jar to a server and run it like this:
java -Xms2024m -Xmx6564m -Dlog4j.configuration=/path/to/log4j2.xml -Dspring.profiles.active=dev -Dspring.config.location=/path/to/application.yml -jar myApp.jar
The configuration isn't read by Spring,
even worse, the parameters that I set in my previous config seem to be hard-coded at compilation O.o. So the application doesn't fail, rather, it runs with the parameter from the application.yml that was in the same directory as it during compilation, which seems like a very very dangerous bug if application.yml is indeed intended as a configuration file and I'm not misinterpreting the whole thing.
How am I supposed to specific the path to an application.yml ? How comes config values get hard-coded at compile time in my code ? Is there a way to stop this ?
Try using file://path/to/application.yml.

How to set Spring Classpath

I am using IntelliJ and jus checked out working code from the svn.I am struggling to run the jar.
Its a simple core java Spring project.
Since I get the above error.I understand that the spring path is not set fine.
How do I handle it.?
private ApplicationContext appContext = new AnnotationConfigApplicationContext(ApplicationRepositoryConfiguration.class);
Application context file is a bean class here (#Bean annotation is used). I am not using a xml file.
It seems like you are running your project from command line. Run following command :
java -classpath spring.jar;spring-sec.jar,......so on com.example.UrMainClass
while specifying jar in command, make sure you provide complete path of jar. Also check this link https://stackoverflow.com/a/10122038/1065180

Categories