2013年7月4日 星期四

Ant Import 和 Include的差異

http://ant.apache.org/manual/Tasks/include.html

其實官方網頁寫得很清楚了,貼一下原文
The short version: Use import if you intend to override a target, otherwise use include.
When using import the imported targets are available by up to two names. Their "normal" name without any prefix and potentially with a prefixed name (the value of the as attribute or the imported project's name attribute, if any).
When using include the included targets are only available in the prefixed form.
When using import, the imported target's depends attribute remains unchanged, i.e. it uses "normal" names and allows you to override targets in the dependency list.
When using include, the included targets cannot be overridden and their depends attributes are rewritten so that prefixed names are used. This allows writers of the included file to control which target is invoked as part of the dependencies.
It is possible to include the same file more than once by using different prefixes, it is not possible to import the same file more than once.
意思就是說如果被導入的target需要被 override的話就用import,不然用include即可。


例如官方的例子, 下面是要被導入的nested.xml
<project>
  <target name="setUp">
    <property name="prop" value="in nested.xml"/>
  </target>

  <target name="echo" depends="setUp">
    <echo>prop has the value ${prop}</echo>
  </target>
</project>
如果用import
<project default="test">
  <target name="setUp">
    <property name="prop" value="in importing"/>
  </target>

  <import file="nested.xml" as="nested"/>

  <target name="test" depends="nested.echo"/>
</project>
結果會是
setUp: nested.echo: [echo] prop has the value in importing test:
如果用include
<project default="test">
  <target name="setUp">
    <property name="prop" value="in importing"/>
  </target>

  <include file="nested.xml" as="nested"/>

  <target name="test" depends="nested.echo"/>
</project>
結果會變成
nested.setUp: nested.echo: [echo] prop has the value in nested.xml test:
結論: 如果沒有要改動到導入的XML東西用include即可,反之則用import

沒有留言:

張貼留言

[京都] 瑠璃光院 永觀堂 東寺

      京都的紅葉大部分都是以寺廟為中心,來記錄一下這次跑的寺廟,第一間是 瑠璃光院,這沒甚麼好說的,就是熱門景點,由於剛好是不用預約的日期,所以最好要提早去排隊,交通也不是很方便會有巴士爆滿的情況,而且門票也不便宜,不過拍出來的照片層次豐富,要不要去被騙一次就看人抉擇了。 ...