当主代码引用测试代码时,我可以使用eclipse(使用maven,m2e)抛出错误/警告吗?

时间:2021-10-05 10:42:36

I am using Eclipse (4.2 Juno) with m2e with the default settings. Source code is in src/main/java and test code is in src/test/java.

我使用带有m2e的Eclipse(4.2 Juno)和默认设置。源代码在src / main / java中,测试代码在src / test / java中。

Sometimes I inadvertently import some stuff from test to a class in main. Eclipse does not complain about this (both src/main/java and src/test/java are just configured as source folders when using m2e), and tests all run properly because the test environment (JUnit) can access code from both main and test. However, when I try to run main code by itself later (after it has been ostensibly tested), I will inevitably get a bunch of ClassNotFoundExceptions when trying to use the code.

有时我无意中将一些东西从测试导入到主要的类中。 Eclipse不抱怨这(两个的src / main / java和SRC /测试/ java的只是配置为源文件夹使用M2E时),并测试所有的正常运行,因为测试环境(JUnit的)可以从主,测试接入代码。但是,当我稍后尝试运行主代码时(在表面测试之后),在尝试使用代码时,我将不可避免地得到一堆ClassNotFoundExceptions。

Is there a way to get Eclipse to show errors or warnings when this happens?

当发生这种情况时,有没有办法让Eclipse显示错误或警告?

2 个解决方案

#1


0  

If your code in src/main/java refers to code in the src/test/java, it will simply not compile with Maven. No need to write your own enforcer rule for that.

如果src / main / java中的代码引用了src / test / java中的代码,那么它将无法使用Maven进行编译。无需为此编写自己的强制执行规则。

#2


0  

Unfortunately there's no way to do this (that I know of). m2e gets the classpath wrong in other ways sometimes too, so you should get used to running the Maven build before you check in - irrespective of what Eclipse says about the code compiling.

不幸的是,没有办法做到这一点(我知道)。 m2e有时也会以其他方式使类路径错误,因此您应该在签入之前习惯运行Maven构建 - 无论Eclipse对代码编译的说法如何。

See this presentation http://www.eclipsecon.org/europe2012/sites/eclipsecon.org.europe2012/files/EclipseConEU2012-m2e-talk.pdf (under 'Dependency Scopes') for more background.

有关更多背景信息,请参阅此演示文稿http://www.eclipsecon.org/europe2012/sites/eclipsecon.org.europe2012/files/EclipseConEU2012-m2e-talk.pdf(在“依赖性范围”下)。

#1


0  

If your code in src/main/java refers to code in the src/test/java, it will simply not compile with Maven. No need to write your own enforcer rule for that.

如果src / main / java中的代码引用了src / test / java中的代码,那么它将无法使用Maven进行编译。无需为此编写自己的强制执行规则。

#2


0  

Unfortunately there's no way to do this (that I know of). m2e gets the classpath wrong in other ways sometimes too, so you should get used to running the Maven build before you check in - irrespective of what Eclipse says about the code compiling.

不幸的是,没有办法做到这一点(我知道)。 m2e有时也会以其他方式使类路径错误,因此您应该在签入之前习惯运行Maven构建 - 无论Eclipse对代码编译的说法如何。

See this presentation http://www.eclipsecon.org/europe2012/sites/eclipsecon.org.europe2012/files/EclipseConEU2012-m2e-talk.pdf (under 'Dependency Scopes') for more background.

有关更多背景信息,请参阅此演示文稿http://www.eclipsecon.org/europe2012/sites/eclipsecon.org.europe2012/files/EclipseConEU2012-m2e-talk.pdf(在“依赖性范围”下)。