3

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>
John Manko
  • 1,828
  • 27
  • 51
  • What are the differences in those EAR / web.xml files ? BTW. Why not putting the appropriate web.xml into the corresponding war module ? – khmarbaise Jan 22 '18 at 22:43
  • The WAR is common to all EAR project, the difference only being the authentication realm. Each EAR has several EJB modules along with the common WAR. Some EJBs are common and included in all EARs, other are custom to the EAR that includes it. The only difference right now in the /WEB-INF/web.xml is the auth realm, as each realm needs to have it's own auth realm name. – John Manko Jan 23 '18 at 02:25
  • Have you considered another approach like https://tomcat.apache.org/tomcat-7.0-doc/mbeans-descriptors-howto.html? – Hannes Jan 27 '18 at 08:32
  • Using Payara. This is not a runtime issue, but a development issue. Production builds are just fine. – John Manko Jan 29 '18 at 00:02

3 Answers3

2

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
Rafael Odon
  • 1,211
  • 14
  • 20
0

you can run your war with the jetty-maven-plugin choosing the run-war goal. That goal run the packaged war. See: https://www.eclipse.org/jetty/documentation/9.4.x/jetty-maven-plugin.html#running-assembled-webapp-as-war

gdegani
  • 846
  • 6
  • 17
  • Jetty won't work. I need to run in a full EE profile container, as the EARs each have several ejb modules along with the WAR – John Manko Jan 23 '18 at 03:16
  • have you considered to deploy your ear using maven and the cargo plugin instead of deploying it with netbeans ? See https://codehaus-cargo.github.io/cargo/Maven2+plugin.html or https://stackoverflow.com/questions/42273099/how-to-deploy-a-maven-web-application-to-a-local-installed-glassfish?rq=1 – gdegani Jan 29 '18 at 16:17
0

First of all, deploy phase (of the build lifecycle) means deployment a production ready artifact to the remote Maven repository (e.g., Nexus or Artifactory). Roughly speaking, artifact deploy can be treated as copying the artifact. Read the Introduction to the Build Lifecycle for more details.

Secondly, Apache Maven does not support application deploy to the application server out-of-the-box. However, there are several ways to do it, depending on the application server you use. Which one do you use?

Special plugin can be found for JBoss Application Server - jboss-as-maven-plugin. See usage example:

<project>
    ...
    <build>
        ...
        <plugins>
            ...
            <plugin>
                <groupId>org.jboss.as.plugins</groupId>
                <artifactId>jboss-as-maven-plugin</artifactId>
                <version>7.9.Final</version>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>deploy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            ...
        </plugins>
        ...
    </build>
...
</project>

Similar plugin can be found for GlassFish Server: glassfish-maven-plugin.

Also, if this is acceptable for you, you can perform 2-steps deploy from Netbeans:

  1. Build the project: run mvn package with all your plugins are configured at package phase.
  2. Deploy the project: run application on the app server from Netbeans if it is supported (See NetBeans Java EE Servers Support page).

Hope this helps.

Gregory.K
  • 1,327
  • 6
  • 12