【发布时间】:2015-08-29 03:28:03
【问题描述】:
如何防止 AntClassLoader 和 URLClassLoader 导致链接错误?
由于项目较旧,将 junit 和 hamcrest.core 放入 ANT_HOME 中,我们的构建使用 includeantruntime=true 来运行单元测试。
我现在正在处理一个用 ant 构建的项目,我不想依赖 ANT_HOME 中的任何 jar 文件。我虔诚地在所有 javac 节中使用了 includeantruntime=false。
但是,现在我使用 hamcrest 进行测试,我遇到了运行时链接错误。
java.lang.LinkageError: loader constraint violation: when resolving method
"org.junit.Assert.assertThat(Ljava/lang/Object;Lorg/hamcrest/Matcher;)V"
the class loader (instance of org/apache/tools/ant/loader/AntClassLoader5)
of the current class, myclass, and the class loader (instance of
java/net/URLClassLoader) for resolved class, org/junit/Assert, have different
Class objects for the type assertThat used in the signature
junit 运行测试时出现运行时链接错误。
我似乎有两个选择,我都不喜欢:
- 从 ANT_HOME 中删除 junit 和 hamcrest.core。这打破了另一个 项目。
- 从项目中移除 junit 和 hamcrest.core 并依赖 ANT_HOME 中的设置。这是我想要避免的。
我原以为 includeantruntime 会有所帮助。
我的目标是让 junit 和 hamcrest.core 保留在 ANT_HOME 中,同时将它们包含在项目中。也就是说,ANT_HOME 的内容应该无关紧要。
这个问题有解决办法吗?
【问题讨论】: