6

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.

Community
  • 1
  • 1
G.Spansky
  • 852
  • 6
  • 17
  • 32
  • 2
    all of the defined profiles rely on the ${id} property, is it defined in the rest of the POM? – A_Di-Matteo Dec 16 '15 at 15:50
  • My mistake was that ${id} placeholder value was full name of the defined app like [groupId:artifactId:version] instead profile id name only. :) – G.Spansky Dec 16 '15 at 15:58

1 Answers1

6

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>
G.Spansky
  • 852
  • 6
  • 17
  • 32
  • i have a question how to do when is the file of web.xml change ? examples: `web.dev.xml` `web.preprod.xml` `web.prod.xml` – Mercer Feb 01 '16 at 15:14
  • 1
    'mvn clean install -P dev' - this command typed in the console start Maven Lifecycle with dev profile. You can specify profile by "-P profile". – G.Spansky Feb 01 '16 at 22:47
  • Can we use this to overwrite one of my java class ? https://stackoverflow.com/questions/55869228/how-to-use-maven-resource-plugin-to-overwrite-files – Naxi Apr 27 '19 at 08:37
  • https://stackoverflow.com/questions/55880013/copy-resource-not-working-in-maven-resource-plugin – Naxi Apr 27 '19 at 11:20