在打包战争之前在Maven构建阶段运行ant任务? [英] Run ant task in maven build phase before war is packaged?

查看:101
本文介绍了在打包战争之前在Maven构建阶段运行ant任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在部署Web应用程序时,我需要合并一些文件,目前这是通过ant任务实现的.我正在尝试使用以下内容在Maven构建过程中运行此任务:

When deploying a webapp I need to concat some files, currently this is achieved via an ant task. I'm trying to run this task in the maven build process using something like the following:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <phase>package</phase>
            <configuration>
                <target>
                    <move file="${project.build.directory}/${project.name}-${project.version}/WEB-INF/classes/log4j.dev.properties"
                        tofile="${project.build.directory}/${project.name}-${project.version}/WEB-INF/classes/log4j.properties" />

                    <move file="${project.build.directory}/${project.name}-${project.version}/WEB-INF/classes/hibernate.cfg.dev.xml"
                        tofile="${project.build.directory}/${project.name}-${project.version}/WEB-INF/classes/hibernate.cfg.xml" />
                    <delete>
                        <fileset dir="${project.build.directory}/${project.name}-${project.version}/WEB-INF/classes/"
                            includes="**/hibernate.cfg.*.xml" />
                        <fileset dir="${project.build.directory}/${project.name}-${project.version}/WEB-INF/classes/"
                            includes="**/log4j.*.properties" />     
                    </delete>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

以上操作失败,因为尚未将文件复制/删除到目标目录中.如果我将阶段设置为打包",则ant任务运行良好,并且所有文件都已被复制/删除,但这无济于事,因为在运行ant目标之前已经建立了.war.

The above fails because the files have not yet been copied/deleted into target directory. If I set the phase to "package" the ant task runs fine and all the files are copied/deleted, but it's no help as the .war has already been built before the ant target is run.

基本上,我需要在prepare-package阶段结束时运行我的ant目标.

Basically, I need to run my ant target near the end of the prepare-package phase.

查看了《生命周期参考》 我无法锻炼如何将更精细的目标暴露给antrun插件.

Having looked though the Lifecycle Reference I can't workout how to expose the more granular Goals to the antrun plugin.

问题:如何实现此方案?

推荐答案

而不是在prepare-package阶段结束时执行它,您可能想在package阶段中但在之前执行 war包装(通过Maven War插件).

Rather then executing it at the end of the prepare-package phase, you probably want to execute it within the package phase but before the war packaging (via the Maven War Plugin).

可能的解决方案是:

  • 禁用默认的Maven War插件
  • package阶段添加执行力
  • package阶段添加新的Maven War插件执行
  • Disable the default Maven War Plugin
  • Add your execution within the package phase
  • Add a new Maven War Plugin execution for the package phase

由于Maven将按照声明顺序在同一阶段执行插件,因此您将首先执行ant,然后再执行war打包.

Since Maven will execute plugins within the same phase by the declaration order, you would then have your ant execution first, then the war packaging.

执行以下操作:

mvn help:effective-pom

您可以检查默认情况下,战争包装是否已经为我们配置了Maven战争插件执行:

You can check that by default the war packaging already configures a Maven War Plugin execution for us:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <id>default-war</id>
            <phase>package</phase>
            <goals>
                <goal>war</goal>
            </goals>
        </execution>
    </executions>
</plugin>

然后我们可以在maven-antrun-plugin声明之后将以下内容添加到我们的POM中:

We could then add to our POM the following after the maven-antrun-plugin declaration:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <id>default-war</id>
            <phase>none</phase>
            <goals>
                <goal>war</goal>
            </goals>
        </execution>
        <execution>
            <id>package-war</id>
            <phase>package</phase>
            <goals>
                <goal>war</goal>
            </goals>
        </execution>
    </executions>
</plugin>

注意:我们实际上使用的是相同的默认执行ID,并将其绑定到不存在的阶段(none),并因此禁用它;然后我们添加一个新的执行(package-war id),它将在您执行maven-antrun-plugin之后由Maven执行,但仍处于打包阶段.

Note: we are actually using the same default execution id and binding it to a non existing phase (none) and as such disabling it; then we are adding a new execution (the package-war id), which will be executed by Maven after your maven-antrun-plugin execution but still within the package phase.

请注意,声明顺序对于实现您的目标非常重要,因此最好添加一条注释(对于同事(如果有的话)以及对您自己的未来).

Note that the declaration order is really important to achieve your goal, so better to also add a comment (for colleagues, if any, and for the future yourself).

进一步说明:似乎您正在将开发文件与类文件交换,您可能可以通过

Further note: it seems you are swapping dev files with prod-like files, you could probably achieve the same via Maven profiles and Maven resource filtering delegating to properties values the differences between environments and as such avoid swapping files.

检查此SO帖子对这种替代方法感兴趣的一些提示.

Check this SO post for some hints if interested in this alternative approach.

这篇关于在打包战争之前在Maven构建阶段运行ant任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆