有没有一种方法可以在Maven中使用配置文件覆盖资源部分?

时间:2020-10-30 18:26:47

标签: java maven

尝试了一段时间后,我发现如果存在包含相同<resources>的配置文件,<directory>不会被覆盖,例如:

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>dir1/**.json</exclude>
                    <exclude>dir2/*.sh</exclude>
       
                </excludes>
                <filtering>true</filtering>
            </resource>
        </resources>
       <!-- plugins and other stuff-->
</build>

如果要在其他个人资料中使用其他内容:

<profile>
            <id>ci</id>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <excludes>
                             <exclude>dir1/**.json</exclude>
                            <exclude>dir1/*.js</exclude>
                             <exclude>dir1/*.css</exclude>
                             <exclude>dir2/*.sh</exclude>
                        </excludes>
                        <filtering>true</filtering>
                    </resource>
                </resources>
           </build>
</profile>

maven clean package -Pci似乎忽略了配置文件中指定的资源,其工作原理与没有配置文件时相同。

有什么建议解决此问题吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

行家将配置文件resources部分添加到主要部分。有效的pom将是

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>dir1/**</exclude>
                <exclude>dir2/*.sh</exclude>
            </excludes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>dir3/**</exclude>
                <exclude>dir4/*.css</exclude>
            </excludes>
            <filtering>true</filtering>
        </resource>
    </resources>
   <!-- plugins and other stuff-->
</build>

我认为最好的方法是将自定义资源放在不同的文件夹下,例如ci-resourcesnon-ci-resources,并定义2个配置文件cinon-ci

<profile>
        <id>ci</id>
        <build>
            <resources>
                <resource>
                    <directory>src/main/ci-resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
       </build>
</profile>
<profile>
        <id>non-ci</id>
        <build>
            <resources>
                <resource>
                    <directory>src/main/non-ci-resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
       </build>

,只要您需要任一配置文件中的资源,就可以激活该配置文件

答案 1 :(得分:0)

如果只有有限的文件,我宁愿使用<include>。它是这样的:

resources
   |
   |__ dir1
        |_ dir1file.txt
        |_ dir1file2.txt
        

因此,配置文件 dev 仅包含 dir1 / dir1file.txt ,而配置文件 test 包含 dir1 / dir1file2.txt

<profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                        <includes>
                            <include>dir1/dir1file.txt</include>
                        </includes>
                    </resource>
                </resources>
            </build>
        </profile>
         <profile>
            <id>test</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                        <includes>
                            <include>dir1/dir1file2.txt</include>
                        </includes>
                    </resource>
                </resources>
            </build>
        </profile>
    </profiles>

dev profile

test profile