Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With maven, how to modify deploy files just prior to deploy phase?

I have a maven project with one war and several ear projects. Each ear project requires a slightly different war/WEB-INF/web.xml. Each ear's pom.xml uses com.google.code.maven-replacer-plugin:replacer and org.codehaus.mojo:truezip-maven-plugin to replace tokens in the web.xml, and then place that new web.xml in the final <project>-app.ear/web.war/WEB-INF. This all works great with building and creating the final EAR artifacts.

The problem I'm having is that when I run (using Netbeans, but that shouldn't matter), the web.xml used for deployment (<project>/target/gfdeploy/first-app/web_war/WEB-INF/web.xml) is the tokenized version. I tried adding execution phases for deploy, but that doesn't work.

How can I ensure that the run deploy has the modified web.xml so I can test my app during development?

Here is the relevant parts of the ear pom.xml:

        <plugin>
            <groupId>com.google.code.maven-replacer-plugin</groupId>
            <artifactId>replacer</artifactId>
            <version>1.5.3</version>
            <executions>
                <execution>
                    <id>package-replace</id>
                    <phase>package</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                </execution>
                <execution>
                    <id>deploy-replace</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>                                        
                <file>${project.parent.basedir}/${web.xml}</file>
                <outputFile>${project.build.directory}/${web.xml}</outputFile>
                <replacements>
                    <replacement>
                        <token>@REALM_NAME@</token>
                        <value>${web.realm}</value>
                    </replacement>
                </replacements>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>truezip-maven-plugin</artifactId>
            <version>1.2</version>
            <executions>
                <execution>
                    <id>package-replace-web-xml</id>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <files>
                            <file>
                                <source>${project.build.directory}/${web.xml}</source>
                                <outputDirectory>${project.build.directory}/${project.build.finalName}/${web.zip}/WEB-INF</outputDirectory>
                            </file>
                        </files>
                    </configuration>
                </execution>
                <execution>
                    <id>package-replace-web</id>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <files>
                            <file> 
                               <source>${project.build.directory}/${project.build.finalName}/${web.zip}</source>
                                <outputDirectory>${project.build.directory}/${project.build.finalName}.ear</outputDirectory>
                            </file>
                        </files>
                    </configuration>
                </execution>
                <execution>
                    <id>deploy-replace-web-xml</id>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <phase>deploy</phase>
                    <configuration>
                        <files>
                            <file>
                                <source>${project.build.directory}/${web.xml}</source>
                                <outputDirectory>${project.build.directory}/gfdeploy/${project.artifactId}/web-${project.version}_war/WEB-INF</outputDirectory>
                            </file>
                        </files>
                    </configuration>
                </execution>
            </executions>
        </plugin>
like image 290
John Manko Avatar asked Jan 27 '26 12:01

John Manko


1 Answers

I suggest you to keep your default src/main/webapp/WEB-INF/web.xml fully functional for running during development. Then, keep a similar file called src/main/webapp/WEB-INF/web-ear.xml with all the replacement preparation.

Wrap all your replacement plugin strategy inside a maven profile and targeted to the web-ear.xml file. Add to this profile a maven-war-plugin configuration that will use the alternative web-ear.xml file during build, instead of the default web.xml (check: http://maven.apache.org/plugins/maven-war-plugin/):

<profiles>
    <profile>
        <id>change-war-profile</id>
        <build>
            <plugins>
                <!-- all your replacement plugins here -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <configuration>
                        <webXml>src/main/webapp/WEB-INF/web-ear.xml</webXml>
                    </configuration>
                </plugin>
            <plugins>
        </build> 
    </profile>
</profiles>

Make sure to activate this profile during the EAR maven build:

mvn package -Pchange-war-profile
like image 200
Rafael Odon Avatar answered Jan 29 '26 01:01

Rafael Odon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!