java 配置 ant 以运行单元测试。图书馆应该在哪里?应该如何配置类路径?避免 ZipException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/461417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Configuring ant to run unit tests. Where should libraries be? How should classpath be configured? avoiding ZipException
提问by chillitom
I'm trying to run my junit tests using ant. The tests are kicked off using a JUnit 4 test suite. If I run this direct from Eclipse the tests complete without error. However if I run it from ant then many of the tests fail with this error repeated over and over until the junit task crashes.
我正在尝试使用 ant 运行我的 junit 测试。测试使用 JUnit 4 测试套件开始。如果我直接从 Eclipse 运行此测试,则测试将完成而不会出错。但是,如果我从 ant 运行它,那么许多测试都会失败,并一遍又一遍地重复此错误,直到 junit 任务崩溃。
[junit] java.util.zip.ZipException: error in opening zip file
[junit] at java.util.zip.ZipFile.open(Native Method)
[junit] at java.util.zip.ZipFile.(ZipFile.java:114)
[junit] at java.util.zip.ZipFile.(ZipFile.java:131)
[junit] at org.apache.tools.ant.AntClassLoader.getResourceURL(AntClassLoader.java:1028)
[junit] at org.apache.tools.ant.AntClassLoader$ResourceEnumeration.findNextResource(AntClassLoader.java:147)
[junit] at org.apache.tools.ant.AntClassLoader$ResourceEnumeration.nextElement(AntClassLoader.java:130)
[junit] at org.apache.tools.ant.util.CollectionUtils$CompoundEnumeration.nextElement(CollectionUtils.java:198)
[junit] at sun.misc.CompoundEnumeration.nextElement(CompoundEnumeration.java:43)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.checkForkedPath(JUnitTask.java:1128)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeAsForked(JUnitTask.java:1013)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:834)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1785)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:785)
[junit] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
[junit] at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[junit] at java.lang.reflect.Method.invoke(Method.java:597)
[junit] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
[junit] at org.apache.tools.ant.Task.perform(Task.java:348)
[junit] at org.apache.tools.ant.Target.execute(Target.java:357)
[junit] at org.apache.tools.ant.Target.performTasks(Target.java:385)
[junit] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
[junit] at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
[junit] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
[junit] at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
[junit] at org.apache.tools.ant.Main.runBuild(Main.java:758)
[junit] at org.apache.tools.ant.Main.startAnt(Main.java:217)
[junit] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
[junit] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)
my test running task is as follows:
我的测试运行任务如下:
<target name="run-junit-tests" depends="compile-tests,clean-results">
<mkdir dir="${test.results.dir}"/>
<junit failureproperty="tests.failed" fork="true" showoutput="yes" includeantruntime="false">
<classpath refid="test.run.path" />
<formatter type="xml" />
<test name="project.AllTests" todir="${basedir}/test-results" />
</junit>
<fail if="tests.failed" message="Unit tests failed"/>
</target>
I've verified that the classpath contains the following as well as all of the program code and libraries:
我已经验证类路径包含以下以及所有程序代码和库:
ant-junit.jar ant-launcher.jar ant.jar easymock.jar easymockclassextension.jar junit-4.4.jar
I've tried debugging to find out which ZipFile it is trying to open with no luck, I've tried toggling includeantruntimeand forkand i've tried running ant with ant -lib test/libswhere test/libs contains the ant and junit libraries.
我试过调试以找出它试图打开哪个 ZipFile 没有运气,我试过切换includeantruntime和fork,我试过用ant -lib test/libs运行 ant,其中 test/libs 包含 ant 和 junit图书馆。
Any info about what causes this exception or how you've configured ant to successfully run unit tests is gratefully received.
感谢您收到有关导致此异常的原因或您如何配置 ant 以成功运行单元测试的任何信息。
ant 1.7.1 (ubuntu), java 1.6.0_10, junit 4.4
ant 1.7.1 (ubuntu)、java 1.6.0_10、junit 4.4
Thanks.
谢谢。
Update - FixedFound my problem. I had included my classes directory in my path using a fileset as opposed to a pathelement this was causing .class files to be opened as ZipFiles which of course threw an exception.
更新 - 已修复发现我的问题。我使用文件集而不是路径元素将我的类目录包含在我的路径中,这导致 .class 文件作为 ZipFiles 打开,这当然会引发异常。
采纳答案by chillitom
Found my problem. I had included my classes directory in my path using a filesetas opposed to a pathelementthis was causing .class files to be opened as ZipFiles which of course threw an exception.
发现我的问题。我使用文件集而不是路径元素将我的类目录包含在我的路径中,这导致 .class 文件作为ZipFiles打开,这当然会引发异常。
回答by parris
This error is caused specifically because the class path contains explicit references to one or more [files] that are not JAR's. The reference to "error in opening zip file" is of course that a JAR is in effect a ZIP file where as other files [JUNIT] has found like class files are notand as such do not have a zip format. So the class path should contain only explicit references to JAR [files] and/or the names of the [directories] where other resources like class files are to be found.
这个错误是特别引起的,因为类路径包含对一个或多个不是 JAR 的 [文件] 的显式引用。对“打开 zip 文件时出错”的引用当然是 JAR 实际上是一个 ZIP 文件,而其他文件 [JUNIT] 发现类文件不是,因此没有 zip 格式。因此,类路径应该只包含对 JAR [文件] 的显式引用和/或 [目录] 的名称,在其中可以找到其他资源(如类文件)。
So when building up your class path (in ANT) use:
因此,在构建类路径时(在 ANT 中)使用:
<path id="proj.class.path">
<pathelement location="c:/my/project/root" /> :one for each of the [directories] where class files, log4j property files and other resources etc are to be found
<fileset refid="my.file.set"> :to describe all the explicit JAR [files] that need to be on the class path.
</path>
where
在哪里
<fileset id="my.file.set" dir="c:/where/i/keep/my/jars">
<filename name="myjar1.jar" />
<filename name="myjar2.jar" />
<filename name="myjar3.jar" />
</fileset>
or
或者
NOTE: When using wild cards like [**/*]in you need to make sure the wild card is not matching files that are not JAR files
注意:当使用像[**/*]in一样的通配符时,你需要确保通配符不匹配不是 JAR 文件的文件
<fileset id="my.file.set" dir="c:/where/i/keep/my/jars">
<include name="**/*.jar" />
</fileset>
回答by guerda
It sounds like there is an issue with paths.
听起来路径有问题。
Check following error source:
检查以下错误源:
- classpath: print out the classpath variable in a junit test, run it from eclipse and ant, so you can compare them
- Check your project for absolute paths. Probably, ant uses other path prefixes than eclipse.
- classpath:打印出junit测试中的classpath变量,从eclipse和ant运行,这样你就可以比较它们
- 检查您的项目的绝对路径。可能 ant 使用了 eclipse 以外的其他路径前缀。
Some more information would help to help :)
更多信息将有助于帮助:)
Good luck!
祝你好运!
回答by Alexei
Thanks guys for this information. I just want to add some tip from my experience. I have the same problem with junit as you when junit tryes to open license*.txt files in lib folder where *.jar resides.(Ivy resolve process puts them here) So
谢谢你们提供这些信息。我只想从我的经验中添加一些提示。当junit尝试在*.jar所在的lib文件夹中打开license*.txt文件时,我和你有同样的问题。(常春藤解析过程把它们放在这里)所以
<path id="lib.path.id">
<fileset dir="${lib.dir}" includes="**.jar"/>
</path>
helps too.
也有帮助。
回答by Nowaker
If you are using Ubuntu or Debian, this will make JUnit (and some other libs) always available for Ant:
如果您使用的是 Ubuntu 或 Debian,这将使 JUnit(和其他一些库)始终可用于 Ant:
sudo apt-get install ant-optional

