【问题标题】:With maven, how to modify deploy files just prior to deploy phase?使用 Maven,如何在部署阶段之前修改部署文件?
【发布时间】:2018-01-22 20:59:15
【问题描述】:

我有一个 maven 项目,其中有一个 war 和几个 ear 项目。每个ear 项目需要一个略有不同的war/WEB-INF/web.xml。每个earpom.xml 使用com.google.code.maven-replacer-plugin:replacerorg.codehaus.mojo:truezip-maven-plugin 替换web.xml 中的标记,然后将新的web.xml 放在最后的<project>-app.ear/web.war/WEB-INF 中。这一切都非常适合构建和创建最终的 EAR 工件。

我遇到的问题是,当我 run(使用 Netbeans,但这不重要)时,用于部署的 web.xml (<project>/target/gfdeploy/first-app/web_war/WEB-INF/web.xml) 是标记化版本。我尝试为deploy 添加执行阶段,但这不起作用。

如何确保运行部署具有修改后的web.xml,以便在开发期间测试我的应用?

这里是耳朵的相关部分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>

【问题讨论】:

  • 那些 EAR/web.xml 文件有什么区别?顺便提一句。为什么不把合适的web.xml放到对应的war模块中呢?
  • WAR 对所有 EAR 项目都是通用的,不同之处仅在于身份验证领域。每个 EAR 都有几个 EJB 模块以及公共 WAR。一些 EJB 是通用的,并包含在所有 EAR 中,其他的则是包含它的 EAR 的自定义。 /WEB-INF/web.xml 现在唯一的区别是身份验证领域,因为每个领域都需要有自己的身份验证领域名称。
  • 您是否考虑过像tomcat.apache.org/tomcat-7.0-doc/mbeans-descriptors-howto.html这样的另一种方法?
  • 使用 Payara。这不是运行时问题,而是开发问题。生产版本很好。

标签: java maven netbeans netbeans-8


【解决方案1】:

我建议您保持默认的 src/main/webapp/WEB-INF/web.xml 在开发过程中运行完全正常。然后,保留一个名为 src/main/webapp/WEB-INF/web-ear.xml 的类似文件,其中包含所有替换准备。

将所有替换插件策略包装在一个 Maven 配置文件中,并定位到 web-ear.xml 文件。向此配置文件添加一个 maven-war-plugin 配置,该配置将在构建期间使用备用 web-ear.xml 文件,而不是默认的 web.xml (检查:@ 987654321@):

<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>

确保在 EAR maven 构建期间激活此配置文件:

mvn package -Pchange-war-profile

【讨论】:

    【解决方案2】:

    您可以使用 jetty-maven-plugin 选择 run-war 目标来运行您的战争。 那个目标运行了打包的战争。 见:https://www.eclipse.org/jetty/documentation/9.4.x/jetty-maven-plugin.html#running-assembled-webapp-as-war

    【讨论】:

    【解决方案3】:

    首先,部署阶段(构建生命周期)意味着部署一个生产就绪的工件到远程 Maven 存储库(例如,Nexus 或 Artifactory)。粗略地说,artifact deploy 可以被视为复制工件。阅读the Introduction to the Build Lifecycle了解更多详情。

    其次,Apache Maven 不支持开箱即用的应用程序部署到应用程序服务器。但是,有几种方法可以做到这一点,具体取决于您使用的应用程序服务器。你用的是哪一种?

    可以找到 JBoss 应用服务器的特殊插件 - jboss-as-maven-plugin。见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>
    

    可以找到适用于 GlassFish Server 的类似插件:glassfish-maven-plugin

    此外,如果您可以接受,您可以从 Netbeans 执行两步部署

    1. 构建项目:运行 mvn package 并在打包阶段配置所有插件。
    2. 部署项目:如果支持,则从 Netbeans 在应用服务器上运行应用程序(请参阅NetBeans Java EE Servers Support 页面)。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-10-03
      • 2013-05-18
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      相关资源
      最近更新 更多