如果文件夹Y存在,Maven将覆盖类路径中的文件

时间:2016-11-11 10:23:45

标签: maven resources override

我想将一些本地配置文件添加到maven mavenprojekt的类路径中。 这些文件“config-local”将覆盖我的资源目录中的现有默认配置文件。 因此,只有当目录“config-local”存在时,默认的配置文件才会被本地配置替换。

我尝试将dir作为资源添加到我的maven Build中,但它不起作用,我不确定如果config-local不存在会发生什么。

1 个答案:

答案 0 :(得分:0)

答案很简单:

Maven Resources Plugin以反向声明顺序添加资源:

<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <resources>
        <!-- The resources will be placed in reversed order in the war
          that means first entry will be added as last resource and may override other resources -->
        <resource>
            <!-- ATTENTION! we need the config-local declration at first cause it shall be placed as last resource in the classpath -->
            <directory>config-local</directory>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
                <include>**/*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>

只需将配置本地资源移到顶部,它就会被添加为最后一个资源,并可能会覆盖其他默认文件。 如果文件夹不存在,Maven将跳过它而没有任何问题。