How can Change log4j configuration file path? - maven

I am working on a maven project, i put the log4j.xml in this path: /src/main/java/log4j.xml and it is working correctly, Can i move the log4j.xml configuration file to /src/main/resources/ ?? what is the new configurations will be?

Log4j reads its properties file from the classpath, so your properties file can be placed anywhere that's on the classpath without needing any configuration change to tell it where to look. Anything placed in /src/main/java and /src/main/resources ends up on the artifact classpath when maven builds, but /src/main/resources is the more appropriate place for a resource (anything that isn't compiled).

Related

Adding external jars in classpath to an executable WAR file

I have a spring-boot executable WAR file which I launch using java -jar . Now I want to add the flexibility of having it reference JAR files outside of WAR file so as to enable customization of code.
So e.g. I want to have to flexibility of having a jar being present in d:\mylib\my.jar and reference the classes in my.jar from code within the WAR file.
Is this possible?
If yes, how?
I tried adding Class-Path and even editing the spring-boot-lib path in manifest.mf but no use.

Could not open ServletContext resource in Spring boot application [duplicate]

I have a Spring Boot application using Google Pub Sub API. I need to inject Google credentials and other properties using file credentials.json. I put the file in my src/main/resources (otherwise, it will not put the file in the built jar) like this:
spring.cloud.gcp.credentials.location=file:src/main/resources/credentials.json
However, when I build the jar, this file is placed in the root directory and this path is no longer valid. So I am able to run my application from Eclipse, since by the that time, the file is still in my resources directory but I can't run it as a standalone jar after built because the path is suddently just file:credentials.json.
Is there some easy way how to specify the path as relative so it works both in IDE and when running my jar? I can inject the path through env. variables but I would do so only if absolutely necessary.
If you use the classpath prefix then Spring will look for the file on your classapth.
If you put the file in src/main/resources then Maven will, by default, copy it to the root of your classpath and it will then be addressable as follows:
spring.cloud.gcp.credentials.location=classpath:credentials.json
This should hold true whether ...
You are running in your IDE; your IDE's Maven integration will copy the file from src/main/resources to the root of your classpath - typically target/classes
You are running a built JAR; Maven will copy the file from src/main/resources to the root of your JAR

Add classpath pointing to jars in Maven

I have lot of external jar files that needs to be pointed in the project. Since these jar files will be available on end users system by default, I am maintaining config parameter file which will hold that directory location.
Is there any way to include this classpath in maven?

How to put a directory first on the classpath with Spring Boot?

I am building my Spring Boot application using Maven, so I can start it with:
java -jar myjar-1.0-SNAPSHOT.jar --spring.profiles.active=prod
I want to have a directory first on the classpath that would allow me to place some files on the filesystem without having to unzip the jar to change them.
I have tried using loader.path, but it does not seem to work.
java -Dloader.path="config/*" -jar myjar-1.0-SNAPSHOT.jar --spring.profiles.active=prod
The config dir is a subdirectory of where the jar is located. I am trying to load a keystore file which is injected as a Resource in my application. There is such a file in the src/main/resources, but that only works in my IDE, not when packaged as a jar. So I want to put a file first on the classpath so that that one is found first on the classpath.
You can use loader.path but only if the Main-Class is PropertiesLauncher (so it depends how you built the JAR file). Maybe you need to re-build the JAR with packaging=ZIP in the Boot plugin (e.g. docs here)? Can you not set the path to the keystore as a "file:" URL?

How to place context.xml in war's META-INF?

I am building a webapp through Gradle's war plugin. In order to disable Tomcat's session persistence, I need to place the file context.xml in the META-INF directory of the war's root.
I attempted the following:
Create the file src/main/webapp/META-INF/context.xml of the main project
Create the file src/main/resources/META-INF/context.xml of the main project
However, when I build the project using gradle clean war, the produced war file contains a META-INF with only a MANIFEST.MF in it. It is as if my directory gets overridden.
How do I place context.xml in the war?
src/main/webapp/META-INF/context.xml is correct and works fine for me. Chances are that you went wrong somewhere, or that there is a problem with your build script.

Resources