You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven插件goal绑定test阶段后执行mvn test未触发如何解决

问题原因

你将maven-antrun-plugin的执行配置放在了<pluginManagement>节点下,该节点的作用是统一管理插件的版本、配置模板,供子模块继承使用,本身不会激活当前项目的插件生命周期绑定,因此你配置的test阶段绑定不会自动触发。而直接执行mvn antrun:run@ant-echo时,Maven可以直接匹配到pluginManagement中的对应执行配置,因此可以正常运行。

解决方法

有两种常用修正方案:

方案1:将插件配置移出pluginManagement,直接放在<build><plugins>节点下

修改后的build节点配置如下:

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>3.1.0</version>
      </plugin>
    </plugins>
  </pluginManagement>
  <!-- 新增外层plugins节点,放置需要当前项目生效的插件配置 -->
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-antrun-plugin</artifactId>
      <version>3.0.0</version>
      <executions>
        <execution>
          <id>ant-echo</id>
          <phase>test</phase>
          <configuration>
            <target name="app2">
              <echo level="info">This is how you echo information into the console.</echo>
              <echo level="info">Echo levels defined in Ant are the following </echo>
              <echo level="info">error, warning, info, verbose, debug</echo>
              <echo level="info"></echo>
              <echo level="info">The following is the way to echo property values.</echo>
              <echo level="info">${project.var1}</echo>
            </target>
          </configuration>
          <goals>
            <goal>run</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

方案2:保留pluginManagement中的插件配置,在外层plugins中声明引用

如果需要统一管理插件版本,也可以将插件版本和配置放在pluginManagement中,外层仅声明插件即可自动继承配置:

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>3.1.0</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <id>ant-echo</id>
            <phase>test</phase>
            <configuration>
              <target name="app2">
                <echo level="info">This is how you echo information into the console.</echo>
                <echo level="info">Echo levels defined in Ant are the following </echo>
                <echo level="info">error, warning, info, verbose, debug</echo>
                <echo level="info"></echo>
                <echo level="info">The following is the way to echo property values.</echo>
                <echo level="info">${project.var1}</echo>
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </pluginManagement>
  <!-- 外层声明插件,激活配置 -->
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-antrun-plugin</artifactId>
    </plugin>
  </plugins>
</build>

修正配置后执行mvn test即可看到ant任务的输出内容。

内容的提问来源于stack exchange,提问作者Rahul Khimasia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 00:36:05