I am building a jar using maven with simple maven install.
If I add a file to src/main/resources
it can be found on the classpath but it has a config folder where I want that file to go but moving it inside the config folder makes it disappear from the classpath.

- 2,240
- 2
- 13
- 20

- 4,880
- 11
- 39
- 51
3 Answers
A cleaner alternative of putting your config file into a subfolder of src/main/resources would be to enhance your classpath locations. This is extremely easy to do with Maven.
For instance, place your property file in a new folder src/main/config, and add the following to your pom:
<build>
<resources>
<resource>
<directory>src/main/config</directory>
</resource>
</resources>
</build>
From now, every files files under src/main/config is considered as part of your classpath (note that you can exclude some of them from the final jar if needed: just add in the build section:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>my-config.properties</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
so that my-config.properties can be found in your classpath when you run your app from your IDE, but will remain external from your jar in your final distribution).

- 84,998
- 9
- 154
- 353

- 3,876
- 1
- 14
- 14
-
1+1 | Have a look at the reference documentation for this feature: http://maven.apache.org/pom.html#Resources – rwitzel Mar 19 '14 at 10:58
-
1I would take issue with this being "cleaner" but possibly, at least, a useful alternative in cases where the directory structure can't be modified. Following the convention is always cleaner – StormeHawke Sep 03 '14 at 13:56
If you place anything in src/main/resources
directory, then by default it will end up in your final *.jar
. If you are referencing it from some other project and it cannot be found on a classpath, then you did one of those two mistakes:
*.jar
is not correctly loaded (maybe typo in the path?)- you are not addressing the resource correctly, for instance:
/src/main/resources/conf/settings.properties
is seen on classpath asclasspath:conf/settings.properties

- 4,548
- 2
- 33
- 43

- 33,595
- 11
- 64
- 74
-
7
-
Do I need to declare (via Spring @PropertySource for example) the location of the .properties file if it is not in the src/main/resources or, say, in /src/main/resources/conf? By default only src/main/resource files are put on the classpath or in any subdir too? Thanks – Andrey M. Stepanov Nov 24 '18 at 19:13
-
Do I need to import /src/main/resources/application.properties in the first place (@PropertySource ("application.properties") or the default location does not need to be imported? – Andrey M. Stepanov Nov 24 '18 at 19:17
By default maven does not include any files from "src/main/java".
You have two possible way to that.
put all your resource files (different than java files) to "src/main/resources" - this is highly recommended
Add to your pom (resource plugin):
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>

- 12,568
- 8
- 34
- 67

- 411
- 7
- 14