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

Viewed 91

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.

2 Answers

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

If there are only limited files, I would rather go with <include>. It follows like this:

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

So, profile dev includes only dir1/dir1file.txt, while profile test includes 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

Related