Ant构建失败:“Target”build..xml“不存在”

时间:2009-07-06 11:00:23

标签: java ant build

当我以详细模式运行ant文件时,出现如下错误。有没有人有任何想法会出错?我是这个新手

antlib:org.apache.tools.ant] Could not load definitions from resource org/apach
e/tools/ant/antlib.xml. It could not be found.
Override ignored for property "java.home"
BUILD FAILED
Target "build.xml" does not exist in the project "MYPROJECT".
        at org.apache.tools.ant.Project.tsort(Project.java:1821)
        at org.apache.tools.ant.Project..topoSort(Project.java:1729)
        at org.apache.tools.ant.Project.topoSort(Project.java:1692)
        at org.apache.tools.ant.Project.executeTarget(Project.java:1298)
        at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
        at org.apache.tools.ant.Project.executeTargets(Project.java:1181)
        at org.apache.tools.ant.Main.runBuild(Main.java:698)
        at org.apache.tools.ant.Main.startAnt(Main.java:199)
        at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
        at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)
Total time: 1 second

这是我的build.xml

<project name="MYPROJECT" default="dev" basedir=".">
  <property name="build.home" value="build/jsp" />
  <property name="tomcat.home" location="../Tomcat 5.0/" />
  <path id="project.class.path">
    <fileset dir="./lib/">
      <include name="**/*.jar" />
    </fileset>
  </path>
  <target name="reltag">
    <tstamp>
      <format property="now" pattern="d MMM yyyy HH:mm" />
    </tstamp>
    <property name="reltag" value="Built date: ${now}" />
  </target>
  <target name="dev" depends="compile" description="Builds for development">
    <mkdir dir="jsp/WEB-INF/lib" />
    <mkdir dir="jsp/WEB-INF/classes" />
    <copy todir="jsp/WEB-INF/lib">
      <fileset dir="lib">
        <exclude name="**/CVS/*" />
      </fileset>
    </copy>
    <copy todir="jsp/WEB-INF/classes">
      <fileset dir="classes">
        <exclude name="**/CVS/*" />
      </fileset>
    </copy>
  </target>
  <target name="compile" depends="myproject-gen,reltag">
    <copy file="setup/Version.java.template" tofile="java/myproject/Version.java" overwrite="true">
      <filterset>
        <filter token="VERSION" value="${reltag}" />
      </filterset>
    </copy>
    <mkdir dir="classes" />
    <depend srcdir="java" destdir="classes" cache="depcache" />
    <javac srcdir="java" destdir="classes">
      <classpath refid="project.class.path" />
    </javac>
  </target>
  <target name="myproject-gen" description="Generates torque classes and SQL for myproject">
    <!-- generate the java files -->
    <ant dir="torque" target="om" />
    <antcall target="myproject-sql-mysql" />
  </target>
  <!-- generate the schema.sql script for MySQL -->
  <target name="myproject-sql-mysql" depends="myproject-sql-mysql-check" unless="mysql.schema.up.to.date">
    <echo message="Generating SQL file for MySQL" />
    <ant dir="torque" target="sql">
      <property name="torque.database" value="mysql" />
      <property name="torque.runOnlyOnSchemaChange" value="false" />
    </ant>
    <move file="sql/gen/myproject-schema.sql" tofile="sql/gen/myproject-schema-mysql.sql" overwrite="true" />
    <copy file="sql/gen/myproject-schema-mysql.sql" tofile="jsp/admin/database.sql" overwrite="true" />
    <copy file="sql/init.sql" tofile="jsp/admin/init.sql" overwrite="true" />
  </target>
  <!-- check if we need to generate the sql file -->
  <target name="myproject-sql-mysql-check">
    <uptodate property="mysql.schema.up.to.date" targetfile="sql/gen/myproject-schema-mysql.sql" srcfile="torque/myproject-schema.xml" />
  </target>
  <target name="myproject-war" depends="compile-jsp">
    <mkdir dir="build" />
    <jar destfile="build/myproject.war" basedir="build/jsp" />
  </target>
  <target name="prepare_for_war_file">
    <mkdir dir="build\jsp-classes\org\apache\jsp" />
    <!-- <move todir="${build.home}/WEB-INF/classes/org/apache/jsp"> <fileset dir="${build.home}"> <include name="*.jsp"/> </fileset> </move> -->
    <!-- copy everything which is not a jsp to admin directory -->
    <copy tofile="${build.home}/admin/database.sql">
      <fileset file="sql/gen/*-schema-mysql.sql" />
    </copy>
    <copy file="sql/init.sql" todir="${build.home}/admin" />
    <mkdir dir="${build.home}/pdb_files" />
    <!-- copy images directory -->
    <copy todir="${build.home}/images">
      <fileset dir="jsp/images" />
    </copy>
    <copy todir="${build.home}/hand">
      <fileset dir="jsp/hand" />
    </copy>
    <!-- copy javascript directory -->
    <copy todir="${build.home}/javascript">
      <fileset dir="jsp/javascript" />
    </copy>
    <!-- copy META-INF directory -->
    <copy todir="${build.home}/META-INF">
      <fileset dir="jsp/META-INF" />
    </copy>
    <!-- copy stylesheet directory -->
    <copy todir="${build.home}/stylesheet">
      <fileset dir="jsp/stylesheet" />
    </copy>
    <!-- copy WEB-INF directory -->
    <copy todir="${build.home}/WEB-INF/classes/">
      <fileset dir="classes/" includes="**" />
    </copy>
    <copy todir="${build.home}/WEB-INF/lib">
      <fileset dir="lib" includes="*.*" excludes="Cabwiz" />
    </copy>
    <!-- copy license and properties -->
    <!--<copy file="WebContent/myproject.license" todir="${build.home}"/> -->
    <copy file="jsp/myproject.properties" todir="${build.home}" />
    <!-- move jsp classes to the correct package structure so war file will work when deployed -->
    <move todir="${build.home}/WEB-INF/classes/org/apache/jsp">
      <fileset dir="${build.home}" includes="**/*.class" excludes="WEB-INF/**" />
    </move>
    <antcall target="create-web_XML"></antcall>
  </target>
  <target name="create-web_XML">
    <loadfile property="generated.web.xml" srcFile="jsp/WEB-INF/generated_web.xml" />
    <copy file="jsp/WEB-INF/web_template.xml" tofile="jsp/WEB-INF/web.xml.deployable" overwrite="true">
      <filterset begintoken="&lt;!--" endToken="--&gt;">
        <filter token="GENERATED_WEB_XML" value="${generated.web.xml}" />
      </filterset>
    </copy>
    <copy file="jsp/WEB-INF/web.xml.deployable" tofile="build/jsp/WEB-INF/web.xml" />
  </target>
  <target name="compile-jsp" depends="dev">
    <antcall target="prepare_for_war_file"></antcall>
    <property environment="env" />
    <taskdef classname="org.apache.jasper.JspC" name="jasper2">
      <classpath id="jspc.classpath">
        <pathelement location="${java.home}/../lib/tools.jar" />
        <fileset dir="${env.tomcat.home}/bin2">
          <include name="*.jar" />
        </fileset>
        <fileset dir="${env.tomcat.home}/server/lib">
          <include name="*.jar" />
        </fileset>
        <fileset dir="${env.tomcat.home}/common/lib">
          <include name="*.jar" />
        </fileset>
      </classpath>
    </taskdef>
    <mkdir dir="java/jsp-src" />
    <jasper2 validateXml="false" package="org.apache.jsp" uriroot="jsp" webXmlFragment="jsp/WEB-INF/generated_web.xml" outputDir="java/jsp-src" />
    <jspc srcdir="jsp" destdir="java/jsp-src">
      <include name="include/**/*.jsp" />
      <include name="**/*.jsp" />
    </jspc>
    <mkdir dir="build/jsp-classes" />
    <property environment="env" />
    <javac destdir="build/jsp/WEB-INF/classes" optimize="off" debug="on" failonerror="true" srcdir="java/jsp-src" excludes="**/*.smap">
      <classpath>
        <pathelement location="classes" />
        <fileset dir="lib">
          <include name="*.jar" />
        </fileset>
        <pathelement location="${env.tomcat.home}/common/classes" />
        <fileset dir="${env.tomcat.home}/common/lib">
          <include name="*.jar" />
        </fileset>
        <pathelement location="${env.tomcat.home}/shared/classes" />
        <fileset dir="${env.tomcat.home}/shared/lib">
          <include name="*.jar" />
        </fileset>
        <fileset dir="${env.tomcat.home}/bin">
          <include name="*.jar" />
        </fileset>
      </classpath>
      <include name="**" />
      <!--<exclude name="**/includes/*menu_jsp.java" /> -->
    </javac>
  </target>
  <target name="clean" description="Deletes the java/classes dir and torque base objects">
    <delete dir="classes" />
    <delete dir="${build.home}" />
    <delete>
      <fileset file="build/*" />
    </delete>
    <delete dir="java/myproject/torque/map" />
    <delete dir="java/jsp-src" />
    <delete dir="jsp/WEB-INF/classes" />
    <delete dir="jsp/WEB-INF/lib" />
    <delete>
      <fileset dir="java/myproject/torque" includes="Base*.java" />
    </delete>
    <touch file="torque/myproject-schema.xml" />
  </target>
</project>

6 个答案:

答案 0 :(得分:20)

因为您的ant文件的名称是build.xml,所以您应该只输入ant而不使用ant build.xml。 即:> ant [输入]

答案 1 :(得分:10)

  1. 可能您没有正确设置环境变量ANT_HOME
  2. 您似乎正在调用Ant:“ant build..xml”。如果您的ant脚本具有名称build.xml,则需要在命令行中仅指定目标。例如:“ant target1”。

答案 2 :(得分:8)

看起来你称之为'ant build..xml'。 ant会自动选择当前目录中的文件build.xml,因此只需调用'ant'(如果定义了default-target)或'ant target'(将调用目标命名目标)。

通过调用'ant -p',您将获得build.xml中定义的目标列表。

编辑:在评论中显示调用'ant -verbose build.xml'。为了正确,这必须被称为'ant -verbose'。将自动使用当前目录中的文件build.xml。如果需要显式指定构建文件(因为它的名称不是build.xml),则必须使用'-f'选项指定构建文件:'ant -verbose -f build.xml'。我希望这会有所帮助。

答案 3 :(得分:3)

在build.xml所在的文件夹中仅运行命令 -

  

而不是命令 - “

  

ant build.xml

`

。如果您使用ant文件作为构建xml,则以下步骤可帮助您执行以下步骤: 打开cmd提示&gt;&gt;切换到项目位置&gt;&gt;键入ant并单击Enter key

答案 4 :(得分:0)

运行ant -d or ant -verbose, but works other times时,这是 的问题吗?

我注意到了这条错误消息行:

Could not load definitions from resource org/apache/tools/ant/antlib.xml. It could not be found.

org/apache/tools/ant/antlib.xml 文件嵌入在ant.jar中。 ant命令实际上是名为ant的shell脚本或名为ant.bat的批处理脚本。如果未设置环境变量ANT_HOME,它将通过查看ant命令本身所在的位置来确定它所在的位置。

有时我会看到有人会移动ant shell / batch脚本将其放入路径,并且ANT_HOME未设置或设置错误。

你在哪个平台上?这是Windows还是Unix / Linux / MacOS?如果您使用的是Windows,请检查是否设置了%ANT_HOME%。如果是,请确保它是正确的目录。确保已将PATH设置为包含%ANT_HOME%/bin

如果您使用的是Unix,请不要将ant shell脚本复制到可执行目录中。相反,建立一个符号链接。我有一个名为/usr/local/bin的目录,我在其中放置了我想要覆盖/bin/usr/bin中的命令的命令。我的Ant已安装在/opt/ant-1.9.2中,我有一个从/opt/ant-1.9.2/opt/ant的符号链接。然后,我有/opt/ant/bin/usr/local/bin中所有命令的符号链接。 Ant shell脚本可以检测符号链接并找到正确的Ant HOME位置。

接下来,转到Ant安装目录并查看lib目录以确保ant.jar存在,并且它包含org/apache/tools/ant/antlib.xml。您可以使用jar tvf ant.jar命令。我唯一可以强调的是你确实正确设置了所有东西。您在PATH中有Ant shell脚本,因为您在类路径中包含了Ant的HOME目录的bin目录,或者(如果您在Unix / Linux / MacOS上),您可以象征性地拥有该文件链接到PATH中的目录。

确保您的JAVA_HOME设置正确(在Unix上,您可以使用符号链接技巧让java命令为您设置),并且您正在使用Java 1.5或更高版本。 Java 1.4将不再适用于较新版本的Ant。

同时运行ant -version,看看你得到了什么。您可能会遇到与以前相同的错误,这会让我认为您有错误。

让我知道你找到了什么,以及你的操作系统,我可以给你关于重新安装Ant的指示。

答案 5 :(得分:0)

我可能迟到但这对我有用:


  1. 打开位于项目目录中的build.xml文件。
  2. 将以下代码复制并粘贴到主项目标记中: <target name="build" />