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

Groovy Ant Task执行外部文件时出现NoSuchMethodError问题排查

问题

试验Groovy Ant Task执行外部文件代码时,遇到版本不匹配导致的NoSuchMethodError。内嵌Groovy代码执行正常,但调用外部task.groovy时构建失败。

环境配置

  • 文件结构:
C:\tmp\groovy-ant-test\ 
   |_pom.xml
   |_task.groovy
  • pom.xml配置:
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <property name="compile_classpath" refid="maven.compile.classpath" />
                                <echo message="compile classpath: ${compile_classpath}" />

                                <taskdef resource="org/codehaus/groovy/antlib.xml">
                                    <classpath refid="maven.compile.classpath"/>
                                </taskdef>
                                <groovy>
                                    println "Hello from pom.xml!"
                                </groovy>
                                <groovy src="task.groovy" />
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.groovy</groupId>
            <artifactId>groovy</artifactId>
            <version>4.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.groovy</groupId>
            <artifactId>groovy-ant</artifactId>
            <version>4.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.10.12</version>
        </dependency>
    </dependencies>
</project>
  • task.groovy内容:
println "Hello from task.groovy!"

报错信息

构建失败时的错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.8:run (default) on project test: An Ant BuildException has occured: java.lang.NoSuchMethodError: org.apache.tools.ant.filters.util.ChainReaderHelper.<init>(Lorg/apache/tools/ant/Project;Ljava/io/Reader;Ljava/lang/Iterable;)V
[ERROR] around Ant part ...<groovy src="task.groovy"/>... @ 10:30 in C:\tmp\groovy-ant-test\target\antrun\build-main.xml

堆栈跟踪片段:

Caused by: java.lang.NoSuchMethodError: org.apache.tools.ant.filters.util.ChainReaderHelper.<init>(Lorg/apache/tools/ant/Project;Ljava/io/Reader;Ljava/lang/Iterable;)V
    at org.codehaus.groovy.ant.Groovy.readCommandFromReader (Groovy.java:432)

已确认项目依赖中包含groovy-ant:4.0.6和ant:1.10.12,且ant:1.10.12包含所需构造方法,dependency:tree未发现版本冲突,寻求失败原因及解决办法。


解决方案

1. 升级maven-antrun-plugin版本

maven-antrun-plugin:1.8版本过旧,默认依赖的Ant版本低于1.10.12,导致运行时加载旧版本Ant类引发方法缺失。建议升级到最新稳定版本(如3.1.0):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>3.1.0</version>
    <!-- 原有执行配置保持不变 -->
</plugin>

2. 为插件显式配置依赖

Maven插件的类路径与项目依赖类路径相互独立,即使项目声明了高版本Ant,插件仍可能使用自身默认的旧版本。需在插件内部显式指定依赖,确保使用正确版本的Ant和Groovy:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <!-- 原有执行配置保持不变 -->
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.10.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.groovy</groupId>
            <artifactId>groovy-ant</artifactId>
            <version>4.0.6</version>
        </dependency>
    </dependencies>
</plugin>

3. 验证依赖生效

执行mvn dependency:tree -Dverbose检查插件依赖树,确认Ant和Groovy版本是否为预期的1.10.12和4.0.6,确保无旧版本依赖被引入。

4. 清理并重新构建

执行mvn clean compile清理旧构建文件,确保新依赖生效。

修改后,Groovy Ant Task可正确加载外部文件代码,不会再出现方法缺失错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 05:30:47