【问题标题】:How to get properties from ant antfile task如何从 ant antfile 任务中获取属性
【发布时间】:2021-07-30 22:34:23
【问题描述】:

使用<ant antfile="..."> 调用任务时,如何从<ant ...> 调用中获取属性?我问是因为一个 Maven 插件 -- maven-antrun-plugin -- 使用这种表示法并声明建议使用具有这种表示法的外部文件。

要查看 Maven 项目中的代码,请单击此处:Retrieve value from external ant file for use in Maven project,或查看上游错误报告 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}

【问题讨论】:

    标签: java ant


    【解决方案1】:

    解决此限制的方法是使用以下命令读写属性文件,其中包含需要导出的属性:

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

    ...然后在使用中读回它:

    <property file="my.properties"/>
    

    这有一个额外步骤的缺点,但确实提供了一些属性封装,因此只有需要的属性才会暴露给父目标。

    工作示例:

    <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>
    

    【讨论】:

      猜你喜欢
      • 2010-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多