简体   繁体   English

编译Java文件时遇到问题(与ANT和XML有关)

[英]Having problems compiling java file (ANT and XML related)

Basically, when I execute the command ant -f mybuild.xml clean junit-TestRunner , I get lots of errors in my java file, it's giving errors on junit imports like: 基本上,当我执行命令ant -f mybuild.xml clean junit-TestRunner ,我的java文件中出现很多错误,这给junit导入带来了错误,例如:

    java:6: error: package org.junit does not exist

Here's a section of my xml file where it gives me errors on test-compile: 这是我的xml文件的一部分,它在测试编译时给了我错误:

<path id="test.classpath">
   <pathelement location="${build.test.dir}"/>
</path>

<target name="test-compile" depends="test-init">
      <javac srcdir="test" 
      destdir="build\test"
      includeAntRuntime="No"/>
</target>

<target name="junit-TestRunner" depends="test-compile">
  <java classname="org.junit.runner.JUnitCore" 
        classpathref="C:\junit-4.10">
    <arg value="org.example.antbook.junit.SimpleTest"/>
  </java>

  <java classname="org.junit.runner.JUnitCore" 
        classpathref="test.classpath">
    <arg value="org.eclipseguide.persistence.FilePersistenceServicesTest"/>
  </java>
</target>

I assume it cannot find the the directory where my junit-4.10.jar file is located. 我假设它找不到我的junit-4.10.jar文件所在的目录。 I've changed that in the 'classpathref', but still give me same errors. 我已经在“ classpathref”中进行了更改,但仍然给了我同样的错误。

Any suggestions? 有什么建议么?

Don't put junit.jar into your $ANT_HOME . 不要将junit.jar放入$ANT_HOME If you do that, you will cause problems when you install a new version of Ant, or if someone else checks out the file. 如果这样做,则在安装新版本的Ant或其他人签出该文件时会导致问题。 Here's your problem: 这是您的问题:

<target name="junit-TestRunner" depends="test-compile">
   <java classname="org.junit.runner.JUnitCore" 
       classpathref="C:\junit-4.10">
       <arg value="org.example.antbook.junit.SimpleTest"/>
   </java>

A classpathref is a reference to a classpath. classpathref classpathref引用 It is defined separately, and it is not a jar or directory. 它是单独定义的,不是jar或目录。

For example, before I call my <java> task, I first define my classpath reference: 例如,在调用<java>任务之前,我首先定义我的类路径引用:

<classpath id="test.classpath">
    <pathelement path="C:/junit-4.10"/>
</classpath>

This defines a reference to this classpath called test.classpath . 这定义了对该类路径的引用 ,称为test.classpath You can now use it in your <java> task: 现在,您可以在<java>任务中使用它:

<target name="junit-TestRunner" depends="test-compile">
    <classpath id="test.classpath">
        <pathelement path="C:/junit-4.10"/>
    </classpath>

   <java classname="org.junit.runner.JUnitCore" 
       classpathref="test.classpath">
       <arg value="org.example.antbook.junit.SimpleTest"/>
   </java>

Another thing you could do is simply include the classpath , so you don't have to define a reference to it in the first place: 您可以做的另一件事就是仅包含classpath ,因此您不必首先定义对其的引用:

<target name="junit-TestRunner" depends="test-compile">
   <java classname="org.junit.runner.JUnitCore">
       <arg value="org.example.antbook.junit.SimpleTest"/>
       <classpath>
           <pathelement path="C:/junit-4.10"/>
       </classpath>
   </java>

By the way, avoid the backslash. 顺便说一下,请避免反斜杠。 It can cause problems because it can sometimes be used as a quoting character . 它可能会引起问题,因为有时可以将其用作引号 Instead, use a forward slash like I did in the above. 相反,请像上面一样使用正斜杠。

I suspect this: 我怀疑这:

classpathref="C:\junit-4.10"

should point to a jar file or a classes subdir under the above. 应该指向上面的jar文件或classes子目录。

Have you checked the second "note" on this page ? 您是否已查看此页面上的第二个“注释”? I think the easiest way is #1, to put those JARs in ${ANT_HOME}/lib folder. 我认为最简单的方法是#1,将这些JAR放在${ANT_HOME}/lib文件夹中。 That way you don't have to specify any classpaths. 这样,您不必指定任何类路径。

Add the following target to your build. 将以下目标添加到您的构建中。

<target name="bootstrap" description="Install build dependencies">
    <mkdir dir="${user.home}/.ant/lib"/>
    <get dest="${user.home}/.ant/lib/junit.jar" src="http://search.maven.org/remotecontent?filepath=junit/junit/4.11/junit-4.11.jar"/>
</target>

This will install the junit jar into one of ANT standard locations for 3rd party dependencies. 这会将junit jar安装到ANT标准位置之一,以用于第三方依赖关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM