Search code examples
javamaven

Is there a way to override the resources section with a profile in maven?


After trying for a while I noticed the <resources> are not overridden if there is a profile that contains the same <directory>, for example:

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

If want to have something different in another profile:

<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 seems to ignore the resources specified in the profile, and works the same as without the profile.

Any suggestions to work around this ?

Thanks.


Solution

  • maven adds the profile resources section to the main one. The effective pom will be

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

    I think the best would be to put the custom resources under different folders, e.g ci-resources and non-ci-resources and to define 2 profiles ci and non-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>
    

    and whenever you need a resource from either profile you can just activate the profile