【问题标题】:Antcall: Call nested ant target from another fileAntcall:从另一个文件调用嵌套的 ant 目标
【发布时间】:2013-07-12 13:42:57
【问题描述】:

我有两个 ant 文件:

1) 主文件

<include file="otherFile.xml" as="otherFile"/>

<target name="firstTarget">
    <antcall target="otherFile.secondTarget"/>
</target>

2) 实用程序文件

<target name="secondTarget">
    <antcall target="thirdTarget"/>
</target>

<target name="thirdTarget">
     <echo message="ok"/> 
</target>

当我调用firstTarget 时,它说找不到thirdTarget。 如果我以这种方式更改secondTarget

<target name="secondTarget">
    <antcall target="otherFile.thirdTarget"/>
</target>

然后就可以了。但是我不能直接使用 secondTarget 。因为第二个文件不知道前缀 otherFile

【问题讨论】:

  • 那么...您在主文件中导入了实用程序文件?
  • 我用的是 include 而不是 import。

标签: file ant antcall


【解决方案1】:

你可以试试:

<ant antfile="otherFile.xml" target="secondTarget"/>

而且不需要包含 otherFile。

【讨论】:

  • 如果你添加useNativeBaseDir="true" 那么otherFile.xml 有它的原始路径。
【解决方案2】:

在将被直接调用和包含的任何文件中使用以下模式:

<project name="my-project">
  <!-- if this is the top-level build.xml, ${self} == "" -->
  <condition property="self" value="">
    <equals arg1="${ant.project.name}" arg2="my-project" />
  </condition>
  <!-- if this is an included build.xml, ${self} == "my-project." -->
  <property name="self" value="my-project." /><!-- note the trailing dot -->
  ...

然后对 antcall 使用以下代码:

<antcall target="${self}target" />

【讨论】:

    【解决方案3】:

    ANT 文档说:

    对于每个包含的文件,Ant 都会添加一个属性,该属性包含包含的构建文件的路径。使用此路径,包含的构建文件可以保留资源并能够相对于其位置引用它们。 因此,例如,如果我包含一个名为 builddocs 的 docsbuild.xml 文件,我可以将其路径作为 ant.file.builddocs 获取,类似于主构建文件的 ant.file 属性。

    您可以利用它来做您想做的事,即无论是从“包含”上下文还是顶级上下文调用 antcall 都可以正常工作。您应该做的是将属性的值设置为“otherFile.context”。如果属性 ant.file.otherFile 已定义,如果未定义,则为 ''(即空字符串)。然后你可以只使用属性扩展来调用目标;例如:

    <antcall target="${otherFile.context}thirdTarget"/>
    

    没有尝试过,但我看不出它为什么不起作用。

    【讨论】:

      【解决方案4】:

      您是否在没有 fileName 的情况下尝试过(只是 secondTarget ):

      <target name="firstTarget">
          <antcall target="secondTarget"/>
      </target>
      

      并在没有别名的情况下导入它;

      <include file="otherFile.xml"/>
      

      有一次,我确实喜欢这个,它奏效了。

      【讨论】:

        【解决方案5】:
        <import file="otherFile.xml"/>    
        <target name="firstTarget">
                <antcall target="secondTarget"/>
        </target>
        

        使用导入任务上传你的文件,这对我有用!!!!

        【讨论】:

          【解决方案6】:

          您可以对位于以下同一文件中的目标进行 ant 调用:

          <target name="secondTarget">
              <antcall target="thirdTarget" antfile="${ant.file}"/>
          </target>
          

          【讨论】:

          • Ant 1.9.6: "antcall 不支持 "antfile" 属性"
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-01-11
          • 2011-08-21
          • 1970-01-01
          • 1970-01-01
          • 2011-05-15
          相关资源
          最近更新 更多