Spring, Skipped (empty) config file - spring

I'm running spring in debug mode, and after struggling to get it to recognize an external properties file I finally got it to do so
Loaded config file 'file:C:/Users/udyj/git/hermes-mq-tool.lib/rte/application-ALD-INT.properties'
However right after that line
Skipped (empty) config file 'file:C:/Users/udyj/git/hermes-mq-tool.lib/rte/application-ALD-INT.properties'
The thing is though, it isn't empty
Name=ALD-INT
Queues=
QueueManager=whatever
TAS_MQS_PASSWORD=pw
TAS_MQS_USER=name
I'm running spring this way,
pb = new ProcessBuilder().inheritIO().command(java, "-noverify", "-cp", classpath,
String.format("-Dspring.config.location=%s", "C:\\Users\\udyj\\git\\hermes-mq-tool.lib\\rte\\application-ALD-INT.properties"),
clazz.getCanonicalName()
);
What does the empty config file thing mean?

This is an open issue
The misleading logging is an unfortunate side-effect of it being
performed in a generic location that doesn't (and shouldn't)
understand the difference between .properties and .yaml configuration
files.

Related

Spring boot read map from .properities file

I have .properties file witch are values for a map of type
AF:Afghanistan
AL:Albania
DZ:Algeria
AS:American Samoa
AD:Andorra
AO:Angola
AI:Anguilla
the properties file has a name countries.properties so its not the application.properties one i have tried a lot of things but nothing seems to work.The java code is this
#Value("#{${}}")
private Map<String, String> countryOptions;
The annotaion is intenionaly left blank.
How can i make it load from the countries.properties?
If you're using a version prior to SpringBoot 2.4.0, Spring Boot allows including additional configuration files using the spring.config.location and spring.config.additional-location properties defined in application.properties file.
After Spring Boot 2.4.0, a new property called spring.config.import has been introduced which supports multiple features:
Adding several files or directories
Files can be loaded either from the classpath or from an external directory
Indicating if the startup process should fail if a file is not found, or if it's an optional file
Importing extensionless files
Example:
# Case 1
spring.config.import=classpath:additional-application.properties
# Case 2
spring.config.import=classpath:additional-application[.yml]
# Case 3
spring.config.import=optional:file:./external.properties
# Case 4
spring.config.import=classpath:additional-application-properties
i found a solution using #PropertySource("classpath:countryOptions.properties")
#Value("#{${countryOptions}}")
and at countryOptions.properties i changed the map to `countryOptions={"US":"United States",....}

Is there a way to include properties file in another properties file?

I'd like to avoid cluttering the application.properties file with lots of things than, in my opinion, would be better in a separate file.
application.properties should be something like
#include module1.properties
#include module1.properties
...
###################################
######### Spring Misc #############
###################################
# Direct log to a log file
logging.file=/tmp/kmp-manager.log
#local listening port
server.port=8082
spring.profiles=nr_dev nr_testing production
spring.profiles.active=production
spring.datasource.platform=postgresql
java.security.egd=file:/dev/./urandom
Is this at all possibile?
If not, what would be a sane way to avoid cluttering?
Spring Boot Spring Boot 2.4 has added a feature for importing
We can now use spring.config.import=developer.properties to import other file. Check this blog post for more details
It's possible in YML file and the configuration are very simple
EXAMPLE:
To include properties of application-DATABASE.yml file in application.yml, use
spring:
profiles:
include: DATABASE
[EDIT - for 2.4.0 and above]
spring.profiles.group.prod=DATABASE
OR
Add the file name in application.properties
spring.config.import=classpath:application-DEV.yml,classpath:application-UDEV.yml,classpath:application-PRO.yml,classpath:application-SBA.yml
spring.config.import: file:${CLOUDJAVA_ROOT}/config/application.yaml
Imports are processed as they are discovered, and are treated as additional documents inserted immediately below the one that declares the import. Values from the imported file will take precedence over the file that triggered the import.
See spring-boot docs: Importing Additional Data

Spring Boot on Tomcat set file Paths in properties file

I want to set my relative file path in a properties file so my SaxReader can pick it up when it runs on Tomcat server. I know this should be easy but I've forgotten forgive me :)
I know Spring Boot has application.properties file but I don't see a way to hook in here.
Is there a way to set the relative path in a properties file that will get picked up by Spring Boot and the SaxReader will see it?
As it is I'm hard coding just the filename and putting the file in the resources folder that serves up the templates and static content such as css and js files. The filePath system.out gives: org.dom4j.DocumentException C:sts-bundle\sts-3.7.2.RELEASE\myFileName the toolsuite root location??? weird!!
Please tell me how to specify the relative path in a properties file.
Thanks!!
You can set the file path like any other string property in Spring Boot properties file and access it from inside the path.
E.g. I have the following set in my application.properties file:
download.directory=temp
and it is used as follows in java class:
#Value("${download.directory}")
private String downloadDirectory;
It is used to download the files, now, if I start the application with jar file present in let's say G:/applications folder then the files will be downloaded into G:/applications/temp.

TOMCAT 7 Access properties file outside WEB INF/classes

So i can access properties file if its in WEB-INF/classes.
However if I keep the same file under TOMCAT/conf and updating catalina.properties to point to the path, I get an error like Name not bound.
I have almost tried everything...even tried with absolute path
if your file.properties file is in the WEB-INF/classes folder? Then:?
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/file.properties");
Only way I could solve it was
1. Create a env folder under tomcat_home and put the config.properties there
2. update catalina.properties and add the tomcat_home\env path in common loader
3. Comment out from spring security XML
Once I commented the above line It ran fine.

Loading a file as a string content in a Spring service contained in a JAR built with Maven

I am using Spring with Maven and I would like to load a file contained in the maven folder src/main/resources inside a spring service annotated with #Service contained in a JAR file (used by another WAR file as a Maven dependency). This does not seem to work for many different reasons (read: for many different exceptions according to many different attempts).
I know that Maven, when building, puts the stuff in src/main/resources at the root of the JAR file. I've opened the JAR file and I've verified that.
I would prefer a File object (something I can use with utility libraries like Apache Commons IO), but also "something" else that's working is fine.
I would prefere going for stuff within the Java class annotated with #Service (i.e. no application context XML file, with stuff inside that I recall within the service etc.)
The file contains stuff that I eventually use as a List<String> where every item is a line in the original file. I need to keep this file as it is, I can not use a database or another solution different than a file.
This is not a .properties file and is not intended to be.
The following are the not working attempts (throwing NullPointerException, FileNotFoundException, IllegalArgumentException: URI is not hierarchical and so on...).
Among the attempts I have tried also adding/removing the prefix src/main/resources while trying to figure out what was wrong, but then I figured out that Maven puts all the stuff at the root of the JAR file as stated before.
#Value("classpath:src/main/resources/myFile.txt")
private Resource myFileResource;
private File myFile =
new File(this.getClass().getResource("/myFile.txt").toURI());
private File myFile =
new FileSystemResource("src/main/resources/myFile.txt").getFile();
private BufferedReader bufferedReaderMyFile =
new BufferedReader(
new InputStreamReader(getClass().getResourceAsStream("myFile.txt")));
What's wrong with what I am doing?
File inside jar file is no longer a File, so treat it as Resource and read it via Stream
InputStream inputStream = this.getClass().getResourceAsStream("/myFile.txt");
assumming you have actually packed that myFile.txt inside jar file and it is available at the root of JAR file

Resources