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

如何使用Ant exec任务执行自定义Ant构建文件中的指定目标?

问题:如何在Ant的exec任务中指定自定义构建文件的目标?

我们的项目使用一款自带自定义Ant可执行文件的专有产品构建部署制品,现有两个构建文件:build-artifcat.xml(包含需通过该产品自定义Ant执行的目标)和build.xml(包含可通过Apache Ant执行的目标)。我们使用Bamboo CI/CD工具,该工具仅支持执行Apache Ant目标,因此计划通过exec任务调用build-artifcat.xml中的目标。在旧项目中找到以下代码,但不清楚如何指定要执行的build-artifcat.xml中的目标,请求帮助。

旧项目中的代码:

<target name="buildArtifact">
  <echo>Running ant exec: ${run.custom.ant.exec}</echo>
  <exec dir="${run.ant.exec.dir}" executable="${run.custom.ant.exec}" error="${basedir}/${error.log.file}">
    <arg value="-f" />
    <arg file="${basedir}/build-artifcat.xml" />
    <arg value="-DimportDir=${import.location}" />
    <arg value="-DartifactDir=${artifact.dir}" />
    <arg value="-data" />
    <arg value="${temp.workspace}" />
    <arg value="-vmargs" />
    <arg value="-Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./crashes/my-heap-dump.hprof -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:./crashes/gc.log" />
    <arg value="-verbose" />
  </exec>
</target>

解决方案

要指定build-artifcat.xml中的目标,只需在exec任务的参数列表里直接添加目标名称作为参数即可,逻辑和命令行执行Ant命令一致(命令行格式为ant -f 构建文件.xml 目标名)。

修改后的示例代码

假设你要执行的目标是deploy,修改后的代码如下:

<target name="buildArtifact">
  <echo>Running ant exec: ${run.custom.ant.exec}</echo>
  <exec dir="${run.ant.exec.dir}" executable="${run.custom.ant.exec}" error="${basedir}/${error.log.file}">
    <arg value="-f" />
    <arg file="${basedir}/build-artifcat.xml" />
    <!-- 添加要执行的目标名称 -->
    <arg value="deploy" />
    <arg value="-DimportDir=${import.location}" />
    <arg value="-DartifactDir=${artifact.dir}" />
    <arg value="-data" />
    <arg value="${temp.workspace}" />
    <arg value="-vmargs" />
    <arg value="-Xmx1024m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./crashes/my-heap-dump.hprof -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:./crashes/gc.log" />
    <arg value="-verbose" />
  </exec>
</target>

扩展说明

  • 如果需要执行多个目标,依次添加多个<arg value="目标名称" />即可,比如同时执行clean和deploy:
    <arg value="clean" />
    <arg value="deploy" />
    
  • 目标参数的位置建议放在-f指定构建文件之后,其他参数(如-D定义的变量)之前,符合Ant命令的常规执行顺序。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 10:24:45