简体   繁体   English

蚂蚁:在一个文件中设置属性,在另一个文件中读取它?

[英]Ant: set property in 1 file, read it in another?

I have the following Ant buildfile importer.xml : 我有以下Ant构建文件importer.xml

<project name="importer" basedir=".." default="build">
    <import file="imported.xml"/>

    <target name="build">
        <!-- Do some stuff... -->

        <property name="isRunningFromImporter" value="true"/>
        <antcall target="run-now"/>
    </target>
</project>

And another buildfile imported.xml that uses the ant-contrib tasks: 而另一构建文件imported.xml使用蚂蚁的contrib任务:

<project name="importer" basedir=".." default="build">
    <!-- Most of file omitted for brevity -->

    <target name="run-now">
        <if>
            <not-equals arg1="${isRunningFromImporter}" arg2="true"/>
            <then>
                <!--
                    This should only execute when the
                    isRunningFromImporter property is not true.
                -->
            </then>
        </if>
    </target>
</project>

The imported#run-now target can be ran as a standalone Ant task, for example: imported#run-now目标可以作为独立的Ant任务运行,例如:

ant -buildfile imported.xml run-now ant -buildfileimported.xml立即运行

In this case I do not want the <then> clause/task executed. 在这种情况下,我不想执行<then>子句/任务。 However, if you run the same task as it is imported into importer.xml : 但是,如果运行与导入importer.xml相同的任务,则:

ant -buildfile importer.xml build ant -buildfile importer.xml构建

Then I want the <then> clause/task to execute, however, Ant does no allow me to see a property in one file and read it in another. 然后,我想执行<then>子句/任务,但是,Ant不允许我在一个文件中查看属性,而在另一个文件中读取它。 Any ideas? 有任何想法吗? Thanks in advance! 提前致谢!

It does what you want by default. 默认情况下,它会执行您想要的操作。 Antcall's "inheritAll" attribute is set to true. Antcall的“ inheritAll”属性设置为true。

Running the following code echo's "true" showing the property is in fact set. 运行以下代码,显示该属性实际上已设置为echo“ true”。

<project name="importer" basedir=".." default="build">
    <import file="imported.xml"/>

    <target name="build">
        <!-- Do some stuff... -->

        <property name="isRunningFromImporter" value="true"/>
        <antcall target="run-now"/>
    </target>
</project>

<project name="importer" basedir="..">
    <!-- Most of file omitted for brevity -->

    <target name="run-now">

        <echo>${isRunningFromImporter}</echo>
    </target>
</project>

I'm not familiar with <not-equals arg1="${isRunningFromImporter}" arg2="true"/> . 我不熟悉<not-equals arg1="${isRunningFromImporter}" arg2="true"/> I've always used <not><equals ...> instead. 我一直使用<not><equals ...>代替。 Where does not-equals come from? 不等于从何而来? Are you sure the problem isn't in that line? 您确定问题不在那吗?

You can use the following idiom to determine automatically whether a particular build file is the main file called by the user or whether it has been imported: 您可以使用以下惯用法来自动确定特定的构建文件是用户调用的主文件还是已导入:

<project name="projectA">
  <!-- set a property if this file is standalone, don't set it if imported -->
  <condition property="projectA.standalone">
    <equals arg1="${ant.file}" arg2="${ant.file.projectA}" />
  </condition>

  <target name="standalone-only" if="projectA.standalone">
    <echo>I am standalone</echo>
  </target>

  <target name="imported-only" unless="projectA.standalone">
    <echo>I have been imported</echo>
  </target>
</project>

The trick here is that the implicit property ant.file is set to the path of the main build file that was specified on the command line (or used implicitly if it's called build.xml ), but in addition Ant sets properties ant.file.PROJECTNAME to the path of the relevant build file for every (main or imported) <project name="PROJECTNAME"> . 这里的窍门是,将隐式属性ant.file设置为在命令行上指定的构建文件的路径(如果称为build.xml ,则隐式使用),但此外Ant设置了属性ant.file.PROJECTNAME到每个(主要或导入的) <project name="PROJECTNAME">的相关构建文件的路径。 Thus ${ant.file} == ${ant.file.PROJECTNAME} if and only if PROJECTNAME is the top-level build file. 因此,当且仅当PROJECTNAME是顶级构建文件时, ${ant.file} == ${ant.file.PROJECTNAME}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM