【问题标题】:Unable to add if-block in build.xml无法在 build.xml 中添加 if-block
【发布时间】:2014-07-11 08:04:49
【问题描述】:

我无法执行if 任务。 我的代码是:

<?xml version="1.0" ?>
<project default="main">

    <property name="buildsequence.property.file.fullpath" value="D:\testant\AntExample" />

    <target name="main" depends="compile, compress" description="Main target">
        <echo>
            Building the .jar file.
        </echo>
    </target>

    <target name="compile" description="Compilation target">
        <javac srcdir="src/org" destdir="src/org" fork="yes" executable="C:\Program Files\Java\jdk1.7.0_60\bin\javac"/>
    </target>

  <target name="compress" description="Compression target">
        <jar jarfile="Project.jar" basedir="src/org" includes="*.class" />

        <if>
            <available file="${buildsequence.property.file.fullpath}" />
            <then>              
                <echo message="File exist"/>
            </then>
            <else>
                <echo message="File do not exist" />                
            </else>
        </if>

  </target>

</project>

错误:

Buildfile: D:\projects\Self\AntExample\build.xml
compile:
    [javac] Compiling 1 source file to D:\projects\Self\AntExample\src\org
compress:

BUILD FAILED
D:\projects\Self\AntExample\build.xml:19: Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.

注意:文件存在于D:\testant\AntExample

【问题讨论】:

  • 使用 if 任务不是蚂蚁的方式。蚂蚁的方式是使目标验证和其他目标取决于此目标,并且仅在“如果”或“除非”属性设置为 true 时触发它们。见ant.apache.org/manual/targets.html

标签: ant build.xml ant-contrib


【解决方案1】:

ifant-contrib 的一部分,它必须存在于您的类路径中。下载后,您可以将其放入您的 Ant lib 文件夹 (anthome/lib),然后您需要通过在构建文件的开头添加以下行来导入任务:

<taskdef resource="net/sf/antcontrib/antlib.xml" />

【讨论】:

    【解决方案2】:

    正如@manouti 所解释的,if 任务是一个外部扩展,而不是核心 ANT 的一部分。

    以下示例下载丢失的 jar 并调用必要的 taskdef 语句:

    <project default="runif">
    
      <target name="init" description="Download dependencies and setup tasks">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/ant-contrib.jar" src="http://search.maven.org/remotecontent?filepath=ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar"/>
    
        <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
      </target>
    
      <target name="runif" depends="init" description="Example running the ant-contrib if statement">
        <if>
          <available file="file.txt" />
          <then>              
            <echo message="File exist"/>
          </then>
          <else>
            <echo message="File do not exist" />                
          </else>
        </if>
      </target>
    
    </project>
    

    ant-contrib 扩展还支持更现代的antlib 机制。以下示例演示了它如何使用命名空间并且不需要 taskdef。

    <project default="runif" xmlns:contrib="antlib:net.sf.antcontrib">
    
      <available classname="net.sf.antcontrib.logic.IfTask" property="if.task.exists"/>
    
      <target name="init" description="Download missing dependencies" unless="if.task.exists">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/ant-contrib.jar" src="http://search.maven.org/remotecontent?filepath=ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar"/>
    
        <fail message="ant-contrib installed run the build again"/>
      </target>
    
      <target name="runif" depends="init" description="Example running the ant-contrib if statement">
        <contrib:if>
          <available file="file.txt" />
          <then>              
            <echo message="File exist"/>
          </then>
          <else>
            <echo message="File do not exist" />                
          </else>
        </contrib:if>
      </target>
    
    </project>
    

    【讨论】:

    • 这段代码表示无法加载 [taskdef] 无法从资源 net/sf/antcontrib/antlib.xml 加载定义。找不到。 构建 .jar 文件。
    • @user3616128 我不是 ant-contrib 扩展的粉丝。如果您真的想使用它,我强烈建议您使用更现代的版本。 V0.6 于 10 年前发布......(2004 年)。我怀疑这可能是你的问题。
    • @user3616128 在我的示例中,我提供了 ANT 构建如何下载和安装最新版本的 ant-contrib 的示例。我发现它使构建更便携。
    猜你喜欢
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    相关资源
    最近更新 更多