如何从 ant antfile 任务中获取属性 [英] How to get properties from ant antfile task

查看:36
本文介绍了如何从 ant antfile 任务中获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 调用任务时,如何从 中获取属性? 调用?我之所以这么问是因为 Maven 插件——maven-antrun-plugin——使用了这个符号,并声明建议使用带有这个符号的外部文件.

When calling a task using <ant antfile="...">, how do you get a property from within the <ant ...> call? I ask because a Maven plugin -- the maven-antrun-plugin -- uses this notation and states that using external files with this notation is recommended.

要查看 Maven 项目中的代码,请单击此处:Retrieve value from external ant file for use in Maven project,或在上游错误报告这里.

To see the code in a Maven project, click here: Retrieve value from external ant file for use in Maven project, or in an upstream bug report here.

这是蚂蚁代码:

<project default="run-test">

    <target name="run-test">
        <!-- Call using antfile notation -->
        <ant antfile="build.xml" target="antfile-task"/>
        <echo level="info">Outside antfile: my.val: ${my.val}</echo>
    </target>

    <target name="antfile-task">
        <property name="my.val" value="just some test value"/>
        <echo level="info">Inside antfile:  my.val: ${my.val}</echo>
    </target>

</project>

输出:

Buildfile: build.xml

run-test:

antfile-task:
     [echo] Inside antfile:  my.val: just some test value
     [echo] Outside antfile: my.val: ${my.val}

推荐答案

解决此限制的方法是使用需要导出的属性写入和读取属性文件:

A workaround to this limitation is to write-and-read a properties file with the properties requiring export using:

<propertyfile file="my.properties">
   <entry key="my.val" value="${my.val}"/>
</propertyfile>

...然后使用:

<property file="my.properties"/>

这有一个额外步骤的缺点,但确实提供了一些属性封装,以便仅将所需的属性公开给父目标.

This has the disadvantage of an extra step, but does provide a bit of property encapsulation so that only the properties needed are exposed to the parent target.

工作示例:

<project default="run-test">

    <target name="run-test">
        <!-- Call using antfile notation -->
        <ant antfile="build.xml" target="antfile-task"/>
        <!-- This will always be empty -->
        <echo level="info">Outside antfile:  my.val: ${my.val}</echo>

        <!-- Read the props file -->
        <property file="my.properties" description="read props from ant"/>
        <!-- Now we have the correct value -->
        <echo level="info">Props workaround: my.val: ${my.val}</echo>
    </target>

    <target name="antfile-task">
        <property name="my.val" value="just some test value"/>
        <echo level="info">Inside antfile:   my.val: ${my.val}</echo>

        <!-- Properties for export to maven -->
        <propertyfile file="my.properties">
            <entry key="my.val" value="${my.val}"/>
        </propertyfile>
    </target>

</project>

这篇关于如何从 ant antfile 任务中获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆