如何将maven antrun插件绑定到clean阶段

发布于 2024-09-28 10:40:51 字数 2531 浏览 3 评论 0 原文

我刚刚将一个 ant 项目翻译成 Maven,但是由于 Maven 并不真正处理部署,所以我在构建中引入了一些 antrun。但是,当我尝试执行它时,插件会跳过我的任务。例如,当我运行 mvn clean antrun:run 时,我收到以下消息:未定义 ant 目标 - 已跳过。第二阶段也发生了同样的情况,我试图覆盖部署阶段以进行实际部署,而不是上传到存储库。

请在下面找到我的 pom.xml 的摘录(类型:pom):

            <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <id>clean</id>
                    <configuration>
                        <task>
                            <echo>Cleaning deployed website</echo>
                        </task>
                        <tasks>
                            <delete dir="${deployRoot}/mydir/${env}"/>
                        </tasks>
                    </configuration>
                    <phase>clean</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
                <execution>
                    <id>deployment</id>
                    <configuration>
                        <task>
                            <echo>Deploying website</echo>
                        </task>
                        <tasks>
                            <echo>Copying website artifact to deployment </echo>
                            <mkdir dir="${deployRoot}/mydir/${env}" />
                            <unzip
                                src="${project.basedir}/target/${env}.${project.version}.zip"
                                dest="${deployRoot}/mydir/${env}" />
                            <chmod perm="ugo+rx">
                                <fileset dir="${deployRoot}/mydir/${env}/web-exploded/bin">
                                    <include name="**/*.sh" />
                                    <include name="**/*.bat" />
                                </fileset>
                            </chmod>
                        </tasks>
                    </configuration>
                    <phase>deploy</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

I have just translated an ant project into maven however since maven does not really deal with deployment I introduce some antrun into the build. However when I try to execute it the plugin skips my tasks. for exemple when I run mvn clean antrun:run I get the following message: No ant target defined - SKIPPED. the same happends to the second phase in which I am trying to override the deployment phase to do an actual deploy rather to upload to a repository.

Please find below an extract of my pom.xml (type:pom):

            <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <id>clean</id>
                    <configuration>
                        <task>
                            <echo>Cleaning deployed website</echo>
                        </task>
                        <tasks>
                            <delete dir="${deployRoot}/mydir/${env}"/>
                        </tasks>
                    </configuration>
                    <phase>clean</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
                <execution>
                    <id>deployment</id>
                    <configuration>
                        <task>
                            <echo>Deploying website</echo>
                        </task>
                        <tasks>
                            <echo>Copying website artifact to deployment </echo>
                            <mkdir dir="${deployRoot}/mydir/${env}" />
                            <unzip
                                src="${project.basedir}/target/${env}.${project.version}.zip"
                                dest="${deployRoot}/mydir/${env}" />
                            <chmod perm="ugo+rx">
                                <fileset dir="${deployRoot}/mydir/${env}/web-exploded/bin">
                                    <include name="**/*.sh" />
                                    <include name="**/*.bat" />
                                </fileset>
                            </chmod>
                        </tasks>
                    </configuration>
                    <phase>deploy</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疧_╮線 2024-10-05 10:40:51

pom.xml 中,您定义两种类型的执行:

  • 一种链接到 clean 阶段
  • 一种链接到 deploy 阶段。顺便请注意,对于 Maven,deploy 确实意味着在服务器上部署我的(网络)应用程序,但是部署远程存储库上的工件。请阅读部署插件信息了解更多详细信息。

因此,如果您运行命令mvn部署,当Maven生命周期到达deploy阶段时,它将运行插件执行(您的pom.xml中的第二个)。

但是,就您而言,您运行默认的 Maven 生命周期,因为您的命令是 mvn antrun:run (我不考虑 clean 这里的目标,因为它与问题无关)。这可以在 Maven 中转换为运行 antrun 插件,目标是运行。问题是您没有为 Ant 插件的直接调用定义任何配置(其中包含 Ant 任务)。

因此有两种解决方案:

  • 将第二次执行绑定到 install 阶段,然后运行 ​​mvn clean install 而不是 mvn antrun:run。请注意,在这种情况下,您将运行整个 Maven 生命周期(即编译、测试、打包)。
  • 创建此插件的配置,该配置与任何执行相关。从 XML 的角度来看,只需添加(或移动)第二个 块作为 定义的子级即可。

如果您选择第二种解决方案,您将拥有如下所示的 pom.xml

       <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.6</version>
        <!-- For cleaning -->
        <executions>
            <execution>
                <id>clean</id>
                <configuration>
                    <task>
                        <echo>Cleaning deployed website</echo>
                    </task>
                    <tasks>
                        <delete dir="${deployRoot}/mydir/${env}"/>
                    </tasks>
                </configuration>
                <phase>clean</phase>
                <goals>
                    <goal>run</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <tasks>
                <echo>Copying website artifact to deployment </echo>
                ...
            </tasks>
        </configuration>
    </plugin>

In your pom.xml, you define two types of executions:

  • One is linked to the clean phase
  • One is linked to the deploy phase. Note, by the way, that for Maven, deploy does not mean deploy my (web-)application on a server but deploy the artifact on a remote repository. Please read the deploy plugin information for more details.

So if you run the command mvn deploy, when the Maven lifecycle reaches the deploy phase, it will run the plugin execution (the second one in your pom.xml).

However, in your case, you are not running the default Maven lifecycle, as your command is mvn antrun:run (I am not considering the clean goal here as it does not matter for the problem). This can be translated in Maven to run the antrun plugin, with the goal run. The problem with that is that you do not define any configuration (which contains the Ant tasks) for a direct call to your Ant plugin.

So two solutions:

  • Bind the second execution to the install phase, and then run the mvn clean install instead of mvn antrun:run. Note that in this case, you will run the whole Maven lifecycle (i.e. compilation, tests, packaging).
  • Create a configuration of this plugin that is not related to any execution. In XML point of view, simply add (or move) the second <configuration> block to be a child of the <plugin> definition.

If you choose the second solution, you will have a pom.xml like this one:

       <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.6</version>
        <!-- For cleaning -->
        <executions>
            <execution>
                <id>clean</id>
                <configuration>
                    <task>
                        <echo>Cleaning deployed website</echo>
                    </task>
                    <tasks>
                        <delete dir="${deployRoot}/mydir/${env}"/>
                    </tasks>
                </configuration>
                <phase>clean</phase>
                <goals>
                    <goal>run</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <tasks>
                <echo>Copying website artifact to deployment </echo>
                ...
            </tasks>
        </configuration>
    </plugin>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文