Maven使用外部目录作为配置目录

时间:2018-08-02 10:49:26

标签: java maven

我正在使用maven生成一个jar,其中包含jar中的所有依赖项(如 this问题)。一切正常。问题是,我需要在外部目录(例如C:/ program / config)下有一些.properties文件,该目录中会有一些配置参数。这些参数在Spring中使用:

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

如何将这个目录添加到类路径中,以便Spring(或Java代码,只要有必要,它就可以)访问C:/ program / config下的文件。

请注意,我不想将文件包含在jar中,我也不希望classpath可以识别该目录(我知道我可以在set CLASSPATH =“”的cmd中完成此操作,但是我没有为了避免这种情况。)

谢谢。

2 个答案:

答案 0 :(得分:0)

配置spring.xml like.Put数据库属性到资源库中

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

答案 1 :(得分:0)

显然,最简单的解决方案是将c:\program\config明确地包含到类路径中,但是如果您想避免使用它,我想到了另一种选择:您需要将文件c:\program\config\*.properties包含到新的Maven库,然后将其设置为代码的系统依赖项

- 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

通过这种方式,您将获得两个库:一个用于代码,另一个仅用于配置文件。

相关问题