Run ant task in maven build phase before war is packaged?

梦想的初衷 提交于 2019-12-06 09:23:26
A_Di-Matteo

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

A possible solution is as possible:

  • Disable the default Maven War Plugin
  • Add your execution within the package phase
  • Add a new Maven War Plugin execution for the package phase

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.

Executing the following:

mvn help:effective-pom

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>

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>

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.

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

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