0

I'm using maven to generate a jar with all dependencies inside the jar (as shown in this question). This is working fine. The problem is, I need to have some .properties file un an external directory (like C:/program/config) where there will be some configurations parameters. These parameters are used in Spring:

<property name="driverClassName" value="${database.driver}" />

How can I add this directory to the classpath so that Spring (or the java code whenever it neets to) can access to the files under C:/program/config.

Note that I don't wan't to include the files inside the jar, I wan't that the classpath recognize this directory (I know I can do it in a cmd with set CLASSPATH="", but I neet to avoid this).

Thanks.

  • My advice: Don't do it. Builds are meant to be reproducible on other computers (like the build server). Your approach breaks that. No matter what problem you are trying to solve, there is probably a much better and cleaner solution. – J Fabian Meier Aug 02 '18 at 12:40

2 Answers2

0

Configure the spring.xml like.Put database properties in resource floder

<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:database.properties</value>
            </list>
        </property>
    </bean>

        <property name="driverClassName">
            <value>${DRIVER}</value>
        </property>
  • I know this works, the problem is that "database.properties" is stored in "C:/program/config", and those files are not in the classpath. –  Aug 02 '18 at 12:11
0

Obviously the simplest solution will be to include explictly c:\program\config into the classpath, but if you want to avoid it, I just think of another alternative: You need to include the files c:\program\config\*.properties into a new Maven library, and then set it as a system dependency of your code:

- myproject
  /pom.xml -> has a dependency on my_config_files
  /src/java/main -> source code

- my_config_files
  /pom.xml -> includes c:\program\config as a resource directory

In this way, you'll end up with two libraries: One for the code, and the other just for configuration files.

Little Santi
  • 8,563
  • 2
  • 18
  • 46