Adding external config file to application.properties - java

I have a configuration file in the resources folder that I wanna add the path to in the application.properties folder. But the following code is not working.
application.properties
appconfig=config.cfg
Because of the nature of my application, I require an external .cfg file. Is there a way I can add the file to application.properties?

Related

Externalize the files under src/main/resources not only application.properties but also other folders present in it

I'm having a requirement that we are developing a application that requires some files like txt, json etc., currently these files are present inside the src/main/resources folder but we are distributing this app as jar file. So we need to externalize these files present inside resources folder like we used to externalize application.properties file outside the jar
Is there any way to externalize the files along with the application.properties. I had tried put these files local to the jar file but spring is not overriding the files, any help is appreciated

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.

How to specify a profile when deploying a Spring boot war file to Tomcat?

At the moment, all my properties are defined in the file src/main/resources/application.properties. However, I would like to have properties files relating to different profiles in the src/main/resources/config folder, and I want to be able to choose any of them. such as:
application-DEV.properties
application-TEST.properties
application-SERVER1.properties
So, the question is how to select these properties. If I was compiling to a jar file, I could do that easily by specifiying the profile when running the jar file, but here I just copy the generated war file to a Tomcat webapps directory.
Well, I've found a way to do that. In the conf directory of Tomcat, add this line to the file catalina.properties there.
spring.profiles.active=<YOUR_PROFILE>
Replace <YOUR_PROFILE> here of course with your profile's name. For example if you are using application-TEST.properties, it would be the following.
spring.profiles.active=TEST
You can define Jvm Argument -Dspring.profiles.active=<PROFILE> on server start up file (.bat/.sh) depending on your environment.

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 ...)

Spring cant find properties file in Tomcat classpath.

I have Spring configured to look up a conf/database.properties file to load some configuration.
This works well outside Tomcat, and in Junit tests, but in Tomcat, it never load. Below the images of this problem.
Configurations:
And:
The properties file in project folder:
The parameters to run Tomcat inside Eclipse:
The temp0 Tomcat Folder, where is all the files being generated Ok:
The Tomcat error log:
https://gist.github.com/4060538
I solved the problem. I think I was using spring in the wrong way.
I changed the follow:
In the library I'm importing/referencing, I removed the line where was importing database.properties file.
I created a spring.xml in my main web app, where in this file I imported the database.properties file and the other app-context.xml files I need to reference.
I think Spring spring don't load properties file outside of the jar. You need to load properties file locally in you main application, and so, references another spring-context.xml files needed.

Categories

Resources