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

使用maven-antrun-plugin整合Ant项目时org.apache.tools.ant.util包缺失

问题描述

我正尝试使用maven-antrun-plugin将Ant项目整合到Maven中,插件配置如下:

<plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.7</version>
            <executions>
                <execution>
                    <id>ant-tasks</id>
                    <phase>compile</phase>
                    <goals>
                    <goal>run</goal>
                    </goals>
                    <configuration>
            <target> 
            <ant antfile="build.xml"
            </ant>
            </target>
            <ivy:settings file="../.ivy2/ivysettings.xml" />
            <ivy:retrieve pattern="./[conf]/[artifact].[ext]" />
            <exportAntProperties>true</exportAntProperties>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>ant</groupId>
                    <artifactId>ant-nodeps</artifactId>
                    <version>1.6.5</version>
                </dependency>
                <dependency>
                    <groupId>ant</groupId>
                    <artifactId>ant-apache-regexp</artifactId>
                    <version>1.6.5</version>
                </dependency> 
                <dependency>
                    <artifactId>jakarta-regexp</artifactId>
                    <groupId>jakarta-regexp</groupId>
                    <version>1.4</version>
                </dependency>
                <dependency>
                    <groupId>com.sun</groupId>
                    <artifactId>tools</artifactId>
                    <version>1.5.0</version>
                    <scope>system</scope>
                    <systemPath>${java.home}/../lib/tools.jar</systemPath>
                </dependency>
                <dependency>
                    <groupId>org.apache.ivy</groupId>
                    <artifactId>ivy</artifactId>
                    <version>2.0.0</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.ant</groupId>
                    <artifactId>ant</artifactId>
                    <version>1.7.0</version>
                </dependency>
                            </dependencies>
       </plugin>
  </plugins>

Maven构建时出现如下错误:

[javac] /__w/1/s/***/src/main/java/com/******/*****/***/****/******/******.java:29: package org.apache.tools.ant.util does not exist
[javac] import org.apache.tools.ant.util.FileUtils;

已尝试通过插件设置ANT_HOME环境变量,也更换过不同版本的Ant依赖,但问题仍未解决。该如何让Maven找到所需的Ant库?注:此构建属于流水线的一部分。

解决方案

1. 统一Ant依赖版本并清理冲突

org.apache.tools.ant.util.FileUtils属于Ant核心库的类,你当前同时引入了版本不一致的ant:ant-nodeps:1.6.5和org.apache.ant:ant:1.7.0,版本冲突会导致类无法被正确加载。处理方式:

  • 移除ant:ant-nodeps依赖,仅保留org.apache.ant:ant,确保所有Ant相关依赖版本统一
  • 若业务必须使用nodeps版本,将所有Ant依赖(包括ant-apache-regexp)的版本调整为一致

2. 将Ant依赖加入项目编译classpath

插件的依赖仅用于插件自身执行,不会自动加入项目的编译路径。需要在项目根<dependencies>节点下添加Ant核心依赖:

<dependencies>
    <!-- 其他项目依赖 -->
    <dependency>
        <groupId>org.apache.ant</groupId>
        <artifactId>ant</artifactId>
        <version>1.7.0</version>
        <scope>provided</scope> <!-- 流水线环境已预装Ant时用provided;否则改为compile -->
    </dependency>
</dependencies>

3. 修正maven-antrun-plugin的配置错误

你的配置中存在两处问题:

  • <ivy:settings>和<ivy:retrieve>直接放在<configuration>下是无效的,必须嵌套在<target>标签内部
  • <ant>标签未闭合,需要补充/>

修正后的配置片段:

<configuration>
    <target> 
        <ant antfile="build.xml" />
        <ivy:settings file="../.ivy2/ivysettings.xml" />
        <ivy:retrieve pattern="./[conf]/[artifact].[ext]" />
    </target>
    <exportAntProperties>true</exportAntProperties>
</configuration>

4. 流水线环境的额外检查

  • 确保流水线使用的JDK版本与tools.jar的版本匹配(当前依赖的1.5.0版本过旧,建议升级为项目兼容的JDK版本对应的tools.jar)
  • 在流水线中添加mvn dependency:tree步骤,查看依赖树,确认Ant依赖是否被正确引入且无冲突
  • 若使用容器化流水线,确保容器内的Maven仓库配置正确,能拉取到所需的Ant依赖

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 01:42:05