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

半城伤御伤魂 提交于 2019-12-12 09:55:12

问题


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>

回答1:


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



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/48390170/with-maven-how-to-modify-deploy-files-just-prior-to-deploy-phase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!