【问题标题】:Maven override files in classpath if folder Y exists如果文件夹 Y 存在,Maven 会覆盖类路径中的文件
【发布时间】:2016-11-11 10:23:45
【问题描述】:

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

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

【问题讨论】:

    标签: maven resources overriding


    【解决方案1】:

    答案很简单:

    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>
    

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

    【讨论】:

      猜你喜欢
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      相关资源
      最近更新 更多