【问题标题】:How can I get the value of the current target ant?如何获取当前目标蚂蚁的值?
【发布时间】:2009-09-15 15:03:56
【问题描述】:

如何获取当前目标蚂蚁的值?

它是否存在一个名为 TARGET 的特殊变量?

【问题讨论】:

  • 当前运行目标的名称?

标签: ant target


【解决方案1】:

基于the issue,你必须修补ant或使用过的javascript:

<target name="test">
  <script language="javascript">
    project.setNewProperty("current_target", self.getOwningTarget());
  </script>
  <echo>${current_target}</echo>
</target>

【讨论】:

  • 使用setNewProperty() 会将${current_target} 视为不可变的(Ant 通常是这样工作的)。如果要覆盖${current_target} 的值,请改用setProperty()
【解决方案2】:

在 ant 1.8.2 中,您可以使用 ${ant.project.invoked-targets}

但是,查看提交日志 http://svn.apache.org/viewvc?view=revision&revision=663061 我猜它从 1.7.1 开始就可以使用了

【讨论】:

  • 这仅显示最初启动的目标,而不是您当前所在的目标。
【解决方案3】:

我的回答,使用 antcontrib

  <macrodef name="showtargetname">
    <attribute name="property"/>
    <sequential>

      <!-- make temporary variable -->
      <propertycopy name="__tempvar__" from="@{property}"/>

      <!-- Using Javascript functions to convert the string -->
      <script language="javascript"> <![CDATA[
        currValue = [project-name].getThreadTask(java.lang.Thread.currentThread()).getTask().getOwningTarget().getName();
        [project-name].setProperty("__tempvar__", currValue);
      ]]>
      </script>

      <!-- copy result -->
      <var name="@{property}" value="${__tempvar__}"/>

      <!-- remove temp var -->
      <var name="__tempvar__" unset="true"/>

    </sequential>
  </macrodef>

用法:

<showtargetname property="mycurrenttarget"/>

【讨论】:

    【解决方案4】:

    我认为你不能,除非你花一些时间编写自己的自定义任务 (http://ant.apache.org/manual/tutorial-writing-tasks.html)

    可以显示的内置属性有:basedir、ant.file、ant.version、ant.project.name、ant.java.version

    【讨论】:

      【解决方案5】:

      如果您使用 -projecthelp arg 运行 ant:

      ant -projecthelp
      

      您将获得 build.xml(或命令行中声明的其他构建文件)中指定的主要目标的列表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-07
        • 2012-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-30
        相关资源
        最近更新 更多