Search code examples
mavenmaven-3maven-release-pluginmaven-resources-plugin

How to overwrite resource file with less priority via Maven?


in our maven module we have multiple resource folders

src/main/prod/sql and src/main/dev/sql. In prod we have scripts for production where are a lot of data inserts. We need overwrite some files in prod/sql directory with files in dev/sql when profile local is activated.

Here is configuration

 <profiles>
    <profile>
        <id>local</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.1.0</version>
                    <executions>
                        <execution>
                            <id>copy-files</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <overwrite>true</overwrite>
                                <outputDirectory>${basedir}/target/classes/sql</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>src/main/dev/</directory>
                                        <filtering>true</filtering>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

When we build modile with clean install there are always scripts from prod/sql in target directory. Can you tell me what I do wrong? Thank you.


Solution

  • Do you build with mvn clean install?

    If thats the case, the <activeByDefault>true</activeByDefault> parameter is not allowing the local profile to be run by default, you should build with mvn clean install -Plocal which loads the local profile during build

    Alternatively, modify pom.xml to always use the local profile:

    <profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
    ...