1

from the manual:

24.3 Application property files SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:

A /config subdirectory of the current directory.

The current directory

A classpath /config package

The classpath root

It mentions current directory twice but this really doesn't mean anything:

I tried putting it in the root of my project (i.e. above src in the folder that matches the output of java.io.File( "." ).getCanonicalPath() and System.getProperty("user.dir");), and I tried putting it with the war files (i.e. in build\libs)

But the only place to put it that actually works is the default location (src\main\resources).

So what does "current directory" even mean and where do the files really go?

I need to find the correct external location for the files so I don't have to build database credentials into the app.

The guides say that putting application.properties in current directory will work and I found the exact current directory to put it in but it still doesn't work, which I can verify by the output of: System.out.println(System.getProperty("spring.datasource.url")); which is null It does output the correct value only with an embedded properties file.

clickcell
  • 439
  • 3
  • 6
  • 20
  • It's intentionally vague; "Current Directory" is a placeholder for "whatever directory maps to '.' for your application right now". It could be the current directory from which tomcat was launched, the directory of the executable, or even the root directory, depending on which application is the app server, and how it was started. Given all of the above, you've already found the best place for a properties file. If you don't want to build in credentials, set up a JNDI datasource that you can reference by name, and set the credentials up there. – Gus Nov 30 '16 at 17:12
  • You know what I found out the output of getproperty is null all the time now even though it is working. I am pretty sure I have seen it not be null in the past. I don't really know what's going on but I think it is picking up the properties file, possibly after running the getproperty command. – clickcell Dec 01 '16 at 15:57

5 Answers5

5

According to ConfigFileApplicationListener:

// Note the order is from least to most specific (last one wins)
private static final String DEFAULT_SEARCH_LOCATIONS =
       "classpath:/,classpath:/config/,file:./,file:./config/";

file:./ resolve to the working directory where you start the java process.

tan9
  • 3,490
  • 2
  • 18
  • 21
3

I agree with Stephane Nicoll's argument that we generally don't need this for development and test but needed for production where properties file is generally externalized and the one present in source code is not used. This is what works for me ,

java -jar myjar.jar --spring.config.location=file:D:\\RunRC\\application.properties

Directory - D:\\RunRC - mentioned in above command is sample from my machine.

I keep using properties file of source code i.e. from \src\main\resources\ in development and test but in production , I comment out entries and if I am starting my jar or war from D:\\RunRC then I provide Current Directory as shown in above java command and keep properties file there.

Just doing - @PropertySource({ "application.properties"}) or @PropertySource({ "file:application.properties"}) doesn't pick it up from the directory where jar or war is kept.

For database credentials, I would suggest to use OS specific environment variables and use syntax similar to - @PropertySource({"file:${CONF_DIR}database.properties" }) where CONF_DIR is existing environment variable pointing to that directory.

Hope it helps !!

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
0

I understand that the current directory is the root directory of your project. However, you can change this with -Dspring.config.location=your/config/dir/. Have a look at this post enter link description here

Community
  • 1
  • 1
kimy82
  • 4,069
  • 1
  • 22
  • 25
0

If you search for "current directory java", you'll end up here with this question. The intention is that if you put an application.properties in the same directory as the application, it will be picked up by default.

You will not use that feature in development or for test as you shouldn't rely on that feature. But when running your app in production, it might be handy to put environment-specific settings in a configuration file that sits next to the application itself.

Community
  • 1
  • 1
Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
  • That's why I tried to put it in the directory with the war file, I thought that is what was launched by the bootrun command. – clickcell Nov 30 '16 at 23:40
  • `java -jar your.war` is going to use the current directory as the working directory. boot run will put your home directory in the directory of the project. And you don't want to put a configuration file there. Again, not to be used in dev mode.... – Stephane Nicoll Dec 01 '16 at 10:33
  • Why cant I use the home directory? Well I can think of one reason, because I just tried it and it didn't work. I also tried having it with the war file and running 'java -jar my.war' and it still didn't work. I have the app write the configurations to the console and they always come up as 'null' but I know when I have the properties file built into the jar then they don't come up as null. – clickcell Dec 01 '16 at 11:51
  • working directory and home directory are two completely different things. How about reading the link I gave in my answer? – Stephane Nicoll Dec 01 '16 at 11:53
  • I pasted the code from your link into my app and I got this output: `Current dir:C:\Users\Blade\javaworkspace\AutomationStatisticsPortal Current dir using System:C:\Users\Blade\javaworkspace\AutomationStatisticsPortal` That is where I tried putting the file in the first place (i.e. above `src`) AutomationStatisticsPortal is the parent folder for src. So I did try that. – clickcell Dec 01 '16 at 13:53
  • I just tried adding `@PropertySources(value = {@PropertySource("file:/C:\\Users\\Blade\\javaworkspace\\AutomationStatisticsPortal\\application.properties" )})` and got the same result. It just doesn't want to let me do it. – clickcell Dec 01 '16 at 14:55
0

Current directory refers to where we execute our jar. Creating an executable jar via Spring Boot maven plugin and placing application.properties just beside the jar file will work. An example :here

Joe
  • 140
  • 1
  • 1
  • 9