zakaria amine
zakaria amine

Reputation: 3682

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.

Upvotes: 2

Views: 672

Answers (3)

mmoossen
mmoossen

Reputation: 1277

non of the current answers solves OPs problem.

Here is what I came up to:

The configuration under build > resources translates directly to an maven-resources-plugin execution with ID default-resources.

So you can overwrite that, with for instance:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
                <goal>resources</goal>
            </goals>
            <configuration>
                <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>
            </configuration>
        </execution>
    </executions>
</plugin>

What ever is set under build > resources on the parent-pom will be ignored.

Upvotes: 0

Aman
Aman

Reputation: 1744

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

Upvotes: 0

Yuri G.
Yuri G.

Reputation: 4648

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

Upvotes: 3

Related Questions