Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to replace files in Maven?

Tags:

java

maven

I have Maven app with 3 different profiles, specified below

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profileVersion>DEV</profileVersion>
                <webXmlFolder>${id}</webXmlFolder>
            </properties>
        </profile>

        <profile>
            <id>test</id>
            <properties>
                <profileVersion>1.0.0-RC1</profileVersion>
                <webXmlFolder>${id}</webXmlFolder>
            </properties>
        </profile>

        <profile>
            <id>prod</id>
            <properties>
                <profileVersion>1.0.0-Final</profileVersion>
                <webXmlFolder>${id}</webXmlFolder>
            </properties>
        </profile>
    </profiles>

And I have Maven structure like this:

src/main/config/default/WEB-INF/web.xml

src/main/config/dev/WEB-INF/web.xml

src/main/config/test/WEB-INF/web.xml

src/main/config/prod/WEB-INF/web.xml

src/main/webapp/WEB-INF/

My task is to set appointed web.xml into webapp/WEB-INF while building, depends on specified profile. If no profile specified, then web.xml is copying from default folder.

I have plugin, but its not working.

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>copy-prod-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <overwrite>true</overwrite>
                        <outputDirectory>${project.build.outputDirectory}/classes/WEB-INF</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/config/${webXmlfolder}/WEB-INF</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Any ideas? I have spend a lot of time with that problem and Im little confused now.

like image 496
G.Spansky Avatar asked Dec 16 '15 13:12

G.Spansky


1 Answers

Ok, everything is working now. Here is my final code, that works:

<properties>
    <webXmlFolder>default</webXmlFolder>
    <profileVersion>defaultVersion</profileVersion>
</properties>

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <profileVersion>DEV</profileVersion>
            <webXmlFolder>dev</webXmlFolder>
        </properties>
    </profile>

    <profile>
        <id>test</id>
        <properties>
            <profileVersion>1.0.0-RC1</profileVersion>
            <webXmlFolder>test</webXmlFolder>
        </properties>
    </profile>

    <profile>
        <id>prod</id>
        <properties>
            <profileVersion>1.0.0-Final</profileVersion>
            <webXmlFolder>prod</webXmlFolder>
        </properties>
    </profile>
</profiles>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>copy-web.xml</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <overwrite>true</overwrite>
                        <outputDirectory>${basedir}/target/classes/WEB-INF</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/config/${webXmlFolder}/WEB-INF</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
like image 121
G.Spansky Avatar answered Oct 19 '22 18:10

G.Spansky