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

Maven运行Ant脚本非调试模式下无法显示完整常规日志

在Maven中运行Ant构建时显示Target调用日志的解决方案

问题说明

将Ant项目迁移到Maven后,使用maven-antrun-plugin运行原build.xml时,非调试模式下无法看到Ant原生的Target调用日志(如default:、target1:这类标识),仅能看到具体任务的执行日志,无法追踪Target的调用流程。

解决方案

maven-antrun-plugin默认将Ant的Target启动/结束日志归类为DEBUG级别,常规INFO日志模式下会被过滤。以下两种方法可解决该问题:

方法1:配置插件日志级别

修改pom.xml中maven-antrun-plugin的配置,添加日志器设置,将Ant的Project日志级别设为INFO,这样就能在Maven常规日志中显示Target调用信息:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>${version.org.apache.maven.plugins.maven-antrun-plugin}</version>
  <dependencies>
    <!-- 保留原有依赖配置 -->
  </dependencies>
  <executions>
    <execution>
      <id>temp</id>
      <phase>install</phase>
      <configuration>
        <!-- 添加日志器配置,开启Ant Target日志 -->
        <loggers>
          <logger name="org.apache.tools.ant.Project" level="INFO"/>
        </loggers>
        <target>
          <ant antfile="${project.basedir}${file.separator}build.xml"/>
        </target>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>
</plugin>

方法2:在Ant任务中指定日志级别

直接在<ant>调用时添加loglevel="info"属性,强制Ant以INFO级别输出日志,无需修改插件全局配置:

<target>
  <ant antfile="${project.basedir}${file.separator}build.xml" loglevel="info"/>
</target>

验证效果

修改配置后执行mvn clean install,日志中将显示Ant的Target调用信息,与原生Ant运行时的输出一致:

[INFO] --- maven-antrun-plugin:3.1.0:run (temp) @ temp ---
[INFO] Executing tasks
[INFO] default:
[INFO]    [delete] Deleting directory C:\temp\test
[INFO]     [mkdir] Created dir: C:\temp\test
[INFO] target1:
[INFO]      [echo] HERE
[INFO] Executed tasks

问题复现详情

原生Ant运行日志

C:\temp>ant -file build.xml                                
ANT_OPTS is set to  -Djava.security.manager=allow
Buildfile: C:\temp\build.xml

default:
   [delete] Deleting directory C:\temp\test
    [mkdir] Created dir: C:\temp\test

target1:
     [echo] HERE

BUILD SUCCESSFUL
Total time: 0 seconds

Maven运行日志(原问题状态)

C:\temp>mvn clean install -e -U
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------------< temp:temp >------------------------------
[INFO] Building temp 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ temp ---
[INFO] Deleting C:\temp\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ temp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\temp\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ temp ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ temp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\temp\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ temp ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ temp ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ temp ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: C:\temp\target\temp-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ temp ---
[INFO] Installing C:\temp\target\temp-0.0.1-SNAPSHOT.jar to C:\Users\XXX\.m2\repository\temp\temp\0.0.1-SNAPSHOT\temp-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\temp\pom.xml to C:\Users\XXX\.m2\repository\temp\temp\0.0.1-SNAPSHOT\temp-0.0.1-SNAPSHOT.pom
[INFO]
[INFO] --- maven-antrun-plugin:3.1.0:run (temp) @ temp ---
[INFO] Executing tasks
[INFO]    [delete] Deleting directory C:\temp\test
[INFO]     [mkdir] Created dir: C:\temp\test
[WARNING]      [echo] HERE
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.954 s
[INFO] Finished at: 2024-04-30T09:19:43+10:00
[INFO] ------------------------------------------------------------------------

配置文件

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

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

    <properties>
        <skipTests>true</skipTests>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
        <!-- Versions -->
        <version.ant-contrib.ant-contrib>1.0b3</version.ant-contrib.ant-contrib>
        <version.org.apache.ant.ant>1.10.13</version.org.apache.ant.ant>
        <version.org.apache.maven.plugins.maven-antrun-plugin>3.1.0</version.org.apache.maven.plugins.maven-antrun-plugin>
    </properties>

    <profiles>
        <profile>
            <id>temp</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>${version.org.apache.maven.plugins.maven-antrun-plugin}</version>
                        <dependencies>
                            <dependency>
                                <groupId>org.apache.ant</groupId>
                                <artifactId>ant</artifactId>
                                <version>${version.org.apache.ant.ant}</version>
                            </dependency>
                            <dependency>
                                <groupId>ant-contrib</groupId>
                                <artifactId>ant-contrib</artifactId>
                                <version>${version.ant-contrib.ant-contrib}</version>
                            </dependency>
                        </dependencies>
                        <executions>
                            <execution>
                                <id>temp</id>
                                <phase>install</phase>
                                <configuration>
                                    <target>
                                        <ant antfile="${project.basedir}${file.separator}build.xml" />
                                    </target>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="temp" basedir="." default="default">
    <target name="default">
        <sequential>
            <delete dir="${basedir}${file.separator}test" />
            <mkdir dir="${basedir}${file.separator}test" />
            <antcall target="target1"></antcall>
        </sequential>
    </target>

    <target name="target1">
        <sequential>
            <echo>HERE</echo>
        </sequential>
    </target>
</project>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 23:40:55