log4j xml configuration file outside the classpath - java

I am having my log4j xml configuration file outside of the project. I am loading the xml configuration file by using absolute path as
DOMConfigurator.configure("D:/E-Signature/ESignature logs/log4j_Properties_Xml/log4j-properties.xml");. Since I am going to convert my project to executable jar I have kept it outside of the project.
Is there any other efficient way to handle this without giving the absolute path.

If your project is a maven project, you can put the log4j.properties in the src/main/resources folder so you don't need to give the absolute path and if it is a simple java project put it in the src folder it will work.

Related

how to access text file from a jar using yaml file in spring boot?

We have a maven project which has only yaml files and relevant text files under src/main/resource folder. We are packing this as Jar file. The structure is as:
src\main\resource
application-configone.yml
application-configtwo.yml
license.txt
application-configone.yml file has entry for license.txt file
license
path: src\main\resource\license.txt
Now we import this Jar artifact in another web application. In web application yml file, we are importing the yml file from Jar as
spring.active.profile = configone, configtwo
The issue that we are getting is that web application fails to start as it is not able to read/get license.txt file defined in application-configone file.
How can we read/access license.txt file which is packaged in a jar in our web application
The easiest way is to put the file under resources directory.
In project where read, create a
appContext=new ClassPathXmlApplicationContext()
and use that to reach the file.
Use relative path from resources folder (in your example you use resource without s on the end, maven default is resources). You can obtain any files on your classpath to this anyway.
res = appContext.getResource("classpath:licence.txt")
It will give back a Resource object. You can use it's inputStream now as you want.
Take a look inside the jar which contains the resources (appconfs and txt), so you can be sure it was built properly, and the files was copied into the classpath.

Take logback.xml to outside of the jar

I am using logback with slf4j in my Maven Java project. Currently logback config file (logback.xml) is in src -> main -> resources folder. And it is working fine.
My issue is, I need to give my client the ability to configure logging as he prefers. For that logback.xml should be outside the jar when I build it. But as xml is inside src folder it is inside the jar and no one can change it after build.
How to achieve this?
Specifying the location of the default configuration file as a system property
You may specify the location of the default configuration file with a system property named "logback.configurationFile". The value of this property can be a URL, a resource on the class path or a path to a file external to the application.
java -Dlogback.configurationFile=/path/to/config.xml -jar myapp.jar
From offcial docs
Logback config file location can be specified in application.properties or application.yml.
application.yml
logging:
config: logback-spring.xml
This allows you to place jar and log-back.xml at the same folder.
Please note that logback-spring.xml file in your project folder should not be included in your jar. This can be achieved setting on build.gradle or pom.xml.
build.gradle
bootJar {
archiveName 'your-project.jar'
exclude("*.xml")
}
The logback.xml file needs to be on the classpath, but it doesn't need to be inside any specific jar. The details of how you want to do this depend on the exact deployment mechanism that's being used: How does whatever's starting this application set the classpath? Whatever that mechanism is, you should be able to configure it to include wherever you're putting the logback.xml file, and then just don't include in in the src/main/resources to be embedded in the jar file.
Depending on the complexity of what you're going for, you may find the maven-assembly-plugin useful for creating your distribution of dependencies.
Using Scala SBT (1.2.1) on Windows:
Batch file:
#cd %~dp0
#set JAVA_OPTS=-Dlogback.configurationFile=logback.xml
#sbt clean run
worked for me (strange ...)

Dependency jar not loading its own resource file

I have a dependency utilities.jar which has a dev.properties file in the root directory when compiled (in src/main/resources when uncompiled). The jar has a class (PropertiesUtil.java) that loads the properties with:
PropertiesUtil.class.getResourceAsStream("/dev.properties");
This jar is included as a dependency in my webapp. The webapp has its own dev.properties file in its root directory when compiled (in uncompiled form its in the src/main/resources folder).
The utilities.jar does not load its own internal dev.properties file but instead the webapp's dev.properties file.
I tried different methods like
PropertiesUtil.class.getClassLoader.getResourceAsStream("dev.properties");
without success.
I am using Gradle to compile the utilities.jar and the webapp into a war. The properties filename need to be the same because I pass in a JVM property
-Dproperty.filename=[dev|qa|prd].properties
when starting up the webapp. This system property is used to load the correct properties files for the webapp and the utilities.jar.
The utilities.jar is a separate project and packaged with gradle clean build and uploaded to an artifact server. The webapp pulls the utilities.jar from the artifact server when building the war.
I think this problem is occurring because you have two different files with exactly the same path (even though one is inside a jar) inside your classpath. It's very possible one dev.properties is getting overwritten.
I recommend changing the path of at least one of the properties files to be outside of the project root. For example, put the uncompiled dev.properties for Utilities at:
/src/main/resources/utilities/dev.properties
Then access it with:
PropertiesUtil.class.getClassLoader.getResourceAsStream("/utilities/dev.properties");
Then the two properties files won't stomp on each other anymore, and that should fix your problem.

Specifying logback.xml in Java Maven project

I have a Java maven project where I need to use logback as the logging framework. I have placed the logback.xml configuration file in the src/main/conf folder. However, it looks like it is not getting picked up because src/main/conf is not in the class path. On building the project I copy all the contents of that folder to target/root/conf and wanted to have that directory as part of my classpath. Can anyone let me know how can I force that?
In a Maven project the default directory for resource file is src/main/resources. This is the layout directory,
if you still want to change this default, follow these instructions.

How to access a XML file in a maven project so it stays available when packaged

I currently started working on a maven web-app project that needs to be launched with the jetty:run-exploded goal for development/debugging in eclipse.
Now, I have an XML file which contents I need to access at runtime. My problem is: where to put the file so that the code that does the reading works both in "exploded" and packaged (i.e. in the WAR) mode?
Putting the file in src/main/java (so as to be in the classpath) won't cut it since maven filters out all non-java files on packaging.
When the file is in src/main/resources, one mean would be to figure out the root path of the project (during eclipse development) and look into that directory - but this won't be the case anymore when the project will be packaged.
Of course I could go into writing code that tries to read the file from both locations, but this seems rather cumbersome. Any suggestions?
Files in src/main/resources are copied to the target/classes directory and are available on the class path. Just read them from the class path. As explained in How do I add resources to my JAR? from the maven documentation (with a test resource here):
In a unit test you could use a simple
snippet of code like the following to
access the resource required for
testing:
...
// Retrieve resource
InputStream is = getClass().getResourceAsStream("/test.properties" );
// Do something with the resource
...
In such case I put the file under src/main/resources directory and use Spring's ClassPathResource. This way the file is accessible in IDE, during Maven build process and in runtime.

Categories

Resources