从include'd/import'ed项目中检索属性值

b10*_*10y 5 ant

我试图使用ant的include或import任务来使用公共构建文件.我被困在从包含文件中检索属性.

这些是我的非工作样本,试图检索"儿童财产"

使用ant导入

父文件

<?xml version="1.0" encoding="UTF-8"?>
<project name="parent" basedir=".">
    <import file="child.xml" />
    <target name="parent-target">
        <antcall target="child-target" />
        <echo message="(From Parent) ${child-property}" />
    </target>
</project>
Run Code Online (Sandbox Code Playgroud)

子文件

<?xml version="1.0" encoding="UTF-8"?>
<project name="child" basedir=".">
    <target name="child-target">
        <property name="child-property" value="i am child value" />
        <echo message="(From Child) ${child-property}" />
    </target>
</project>
Run Code Online (Sandbox Code Playgroud)

产量

parent-target:

child-target:
     [echo] (From Child) i am child value
     [echo] (From Parent) ${child-property}
Run Code Online (Sandbox Code Playgroud)

使用ant包括

父文件

<project name="parent" basedir=".">
    <include file="child.xml" />
    <target name="parent-target">
        <antcall target="child.child-target" />
        <echo message="(From Parent) ${child-property}" />
        <echo message="(From Parent2) ${child.child-property}" />
    </target>
</project>
Run Code Online (Sandbox Code Playgroud)

子文件

与上述相同

产量

parent-target:

child.child-target:
     [echo] (From Child) i am child value
     [echo] (From Parent) ${child-property}
     [echo] (From Parent2) ${child.child-property}
Run Code Online (Sandbox Code Playgroud)

如何从父母那里获取"儿童财产"?

mar*_*ton 4

当您使用该antcall任务时,将为 antcall'ed 任务启动一个新的 Ant 周期 - 但这不会影响调用者的上下文:

被调用的目标在新项目中运行;请注意,这意味着被调用目标设置的属性、引用等将不会保留回调用项目。

使您的简单示例发挥作用的一种方法是将第一个父级更改为:

<target name="parent-target" depends="child-target">
    <echo message="(From Parent) ${child-property}" />
</target>
Run Code Online (Sandbox Code Playgroud)

然后子目标将在父目标之前在父上下文中执行。

但是,您可能会发现在您不希望的父任务上下文中运行子任务会产生副作用。