如何配置用于 junit 测试的 eclipse 类路径?
我有一个 Eclipse 项目,其中每个源文件夹都有其自己关联的输出文件夹。它不是 /classes,而是 /eclipse-classes。
所以如果我有一个文件夹: src/main/java (典型的 Maven 东西) 目标文件夹是: target/eclipse-classes
对于资源等也是如此。
这似乎有效(即 eclipse 生成放入这些文件夹中的 .class 文件),但运行任何 Junit 测试都会引发异常,指出“未找到类”。我正在使用内置的 Eclipse 测试运行程序运行 JUnit(即右键单击该类,“运行为”,“Junit 测试”)。
将 /eclipse-classes 文件夹复制到 /classes 使它们成功,这意味着 eclipse 正在使用 /classes,但我找不到任何配置选项来更改它。有没有办法找出 Eclipse 仍在使用 /classes 文件夹的位置和原因?
(也许相关,我也在使用 m2eclipse 插件)
受 Rich Seller 的回答启发的一些附加信息: Maven 配置为在资源更改时运行以下命令:
process-resources resources:testResources
虽然这不会做任何有用的事情(复制到错误的目录),但资源不会有问题,因为它们最终位于正确的位置。
.classpath 条目看起来不错。有一堆 Maven 子项目,因此嵌套比 Rich 发布的更深一些,但除此之外,其他方面完全相同:
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
我认为我们可能不需要那个子项目,但它不会伤害任何 atm。
edit2:进一步测试表明 eclipse 在 /eclipse-classes 文件夹和 /classes 文件夹中生成类文件。似乎 m2eclipse 在自动构建时在后台运行 mvn build ,但我似乎找不到禁用此功能的方法。如果这里没有人有任何其他想法,我会尝试与 m2eclipse 开发人员联系。
I have an eclipse project where every source folder has its own associated output folder. Instead of /classes it's called /eclipse-classes.
So if I have a folder:
src/main/java (typical maven thing)
the target folder is:
target/eclipse-classes
And likewise for resources etc.
This seems to work (i.e. eclipse generates .class files that are put inside these folders) but running any Junit tests throws an exception stating "class not found". I'm running JUnit using the built-in eclipse test runner (i.e. right click the class, "run as", "Junit test").
Copying the /eclipse-classes folder to /classes makes them succeed, meaning eclipse is using /classes, but I can't find any configuration options to change it. Is there any way to find out where and why eclipse is still using the /classes folder?
(perhaps relevant, I'm also using the m2eclipse plugin)
Some additional information inspired by Rich Seller's answer:
Maven is configured to run the following on resource changes:
process-resources resources:testResources
While this won't do anything useful (copies to the wrong directory) the resources are not problematic atm since they end up in the correct location.
The .classpath entries look okay. There's a bunch of maven subprojects so the nesting goes a bit deeper than what Rich posted, but otherwise it's exactly the same except for this line:
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
I think we might not need that one but it's not hurting anything atm.
edit2: Further testing reveals that eclipse is generating class files in both the /eclipse-classes folders and the /classes folder. It seems that m2eclipse is running mvn build in the background when building automatically, but I can't seem to find a way to disable this. I'll try to get in touch with the m2eclipse developers if nobody here has any other ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您使用m2eclipse,那么Eclipse项目中的配置将被插件覆盖。请参阅本文了解一个解决方案。
原因是某些 Maven 插件无法处理
target/
之外的目录,因此 m2eclipse 开发人员强制编译类的文件夹为target/classes
code> 和target/test-classes
,无论您在 Eclipse 中配置什么。通过使用配置文件,您可以为 Eclipse 使用不同的文件夹。尽管如此,更改 Maven(及其插件)的输出文件夹并不明智。
If you use m2eclipse, then the config in the Eclipse project is overwritten by the plugin. See this article for a solution.
The reason for this is that some maven plugins can't cope with a directory that is outside of
target/
, so the m2eclipse devs force the folders for compiled classes to betarget/classes
andtarget/test-classes
, no matter what you configure in Eclipse.By using a profile, you can use different folders for Eclipse. Still, it's not wise to change the output folders for maven (and its plugins).
Eclipse JUnit 集成没有特殊的类路径配置,它将处理类路径中定义的输出文件夹,并且应该找到编译到这些文件夹的所有类。可能是您的 .classpath 文件中有一些可疑的东西,从而使 JUnit 感到困惑(默认情况下 .classpath 文件是隐藏的,它位于项目的根目录中)。
根据您的描述,我希望看到类似下面的条目(注意默认输出文件夹以及 src/main/java 和 src/main/resources 的覆盖)。您的类路径看起来是否明显不同,如果是这样,这可能是问题所在
这是一个远景,但也可能是在您的项目上配置了 Maven clean,如果是这种情况,则 target/eclipse-classes 的内容将是每当运行 clean 目标时就会删除,因此您的测试将在运行测试之前从文件系统中删除。您可以通过打开项目属性 (alt-enter) 并选择 Maven 项来查看 Maven 运行的目标。
这部分不会直接回答您的问题,但您可能会发现有用。我倾向于让 Eclipse 输出目录与 Maven 相同,并且 Eclipse 内部没有任何问题(我将 Maven 构建器修改为仅运行进程资源,因此它不会尝试编译)。
如果我进行 Maven 构建,Maven 编译器将构建任何已更改的类(如果包含 clean,则将是所有这些类)。 Eclipse 中的后续修改由增量编译器检测并处理,一切正常。我确实关闭了自动构建,但这只是因为它让我烦恼,如果你把它们都关闭的话,Maven 和 Eclipse 可能会发生冲突。
The Eclipse JUnit integration has no special classpath configuration, it will work off the output folders defined in your classpath and should find all classes compiled to those folders. It may be that there is something dodgy in your .classpath file, so that JUnit is confused (by default the .classpath file is hidden from view, it is located in the root of the project).
Based on your description I would expect to see entries something like below (note the default output folder and the override for src/main/java and src/main/resources). Does your classpath look markedly different, if so that may be the issue
This is a long shot, but it may also be that a Maven clean is configured on your project, if that is the case the contents of target/eclipse-classes would be dropped whenever the clean goal is run, so your tests would be deleted from the file system before the tests are run. You can see what goals are run by Maven by opening the project properties (alt-enter) and selecting the Maven item.
This part doesn't directly answer your question, but you may find useful anyway. I tend to have my Eclipse output directories the same as for Maven and have no issues inside Eclipse (I modify the Maven builder to only run process-resources so it doesn't attempt to compile).
If I do a Maven build the Maven compiler will build any changed classes (that would be all of them if a clean is included). A subsequent modification in Eclipse is detected by the incremental compiler and processed, all is fine. I do turn off Build Automatically, but that's just because it annoys me, it may be that Maven and Eclipse fght if you have them both turned off.
以防万一您愿意为此尝试不同的插件:我使用 maven-eclipse-plugin 来生成我的 Eclipse 项目设置。我配置插件来配置我的 Eclipse 项目,以使用完全独立的类输出目录(见下文)。它与项目根目录相关,因此位于目标之外。
这对我来说非常有用,包括能够通过 Maven 和 Eclipse 直接运行测试。
Just in case you're open to trying a different plugin for this: I use the maven-eclipse-plugin to generate my Eclipse project settings. I configure the plugin to configure my Eclipse project to use a completely separate output directory for classes (see below). It's relative to the project root, so it sits outside of target.
This works great for me, including being able to run tests right out of the box, both via Maven and Eclipse.
尝试运行
mvn eclipse:eclipse
。这为我解决了这个问题try running
mvn eclipse:eclipse
. This fixed the issue for me我正在使用 gradle。刷新 gradle 项目将覆盖 .classpath 文件。因此,我必须在 build.gradle 文件末尾添加以下内容,以便让它维护 JUNIT5 引用。
I am using gradle. Refreshing gradle project will overwrite .classpath file. So I have to add the following at the end of the build.gradle file in order to let it maintain a JUNIT5 reference.