使用maven,如何在部署阶段之前修改部署文件?

时间:2018-01-22 20:59:15

标签: java maven netbeans netbeans-8

我有一个maven项目,其中包含一个war和几个ear个项目。每个ear项目都需要略有不同的war / WEB-INF/web.xml。每个ear pom.xml使用com.google.code.maven-replacer-plugin:replacerorg.codehaus.mojo:truezip-maven-plugin来替换web.xml中的令牌,然后将新web.xml放入最后<project>-app.ear/web.war/WEB-INF {1}}。这一切都非常适合构建和创建最终的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>

3 个答案:

答案 0 :(得分: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 (check:{ {3}}):

year  month  Faltas Pre Temp Canc FaltasTemp
2018 January   2     6   5    0       1

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

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

答案 1 :(得分:0)

你可以用jetty-maven-plugin选择战争目标来进行战争。 这个目标是打包战争。 请参阅:DESTDIR

答案 2 :(得分:0)

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

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

可以在JBoss Application Server中找到特殊插件 - jboss-as-maven-plugin。见usage example

template <typename F, class C>
class this_call_wrapper
{
public:
    // Creates a wrapper function that calls `operator()` on `object`
    // `operator()` should take the same arguments as `F`
    this_call_wrapper(const C* object);
    // Deallocates memory used by this and the wrapper
    ~this_call_wrapper();
    // Returns the pointer to the function wrapper
    F* get_wrapper();
private:
    C* object;
    F* wrapper;
};

可以在GlassFish Server上找到类似的插件:glassfish-maven-plugin

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

  1. 构建项目:运行<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> ,所有插件都在包阶段配置。
  2. 部署项目:如果支持,则从Netbeans在应用服务器上运行应用程序(请参阅NetBeans Java EE Servers Support页面)。
  3. 希望这有帮助。