简体   繁体   English

在m2eclipse中启用了作用域“测试”和工作区解析的Maven依赖关系-仅在Maven构建之后才显示编译错误

[英]Maven dependency with scope “test” & Workspace Resolution enabled in m2eclipse - compile error shown only after the Maven build

This question is a result of an answer to the following question: Injection of an autowired field failed in a multi-module Maven project - NoSuchBeanDefinitionException 该问题是以下问题的答案: 在多模块Maven项目中,自动接线字段的注入失败-NoSuchBeanDefinitionException

According to the answer from the aforementioned question, I configured the following structure of my Maven modules: 根据上述问题的答案,我配置了Maven模块的以下结构:

Maven模块

In this case, the class Foo from the base module can be used in tests in the module A (eg when it's autowired using the IFoo interface), but can't be used in the production code (which is contained in src/main/java). 在这种情况下,基本模块中的类Foo可以在模块A中的测试中使用(例如,使用IFoo接口自动装配时),但不能在生产代码中使用(包含在src / main /中) Java)。

The Maven build fails when someone adds a reference to the Foo class in a class in the module A in the production code (under src/main/java). 当有人在生产代码(在src / main / java下)中的模块A中的类中添加对Foo类的引用时,Maven构建失败。 However, the automatic build in Eclipse doesn't fail in such case, because I use the m2eclipse plugin which has the Workspace Resolution enabled. 但是,在这种情况下Eclipse中的自动构建不会失败,因为我使用了启用了工作区分辨率的m2eclipse插件。 I have to run the maven buiild (mvn clean install) to see the compilation failure. 我必须运行maven buiild(mvn全新安装)才能看到编译失败。 Afterwards, the error is still not immediately visible in the affected class in Eclipse. 之后,该错误仍然不会立即在Eclipse中的受影响类中可见。 I have to make a change in the affected java class (eg add a space and save the file) and then I can see an error in this class. 我必须在受影响的Java类中进行更改(例如,添加一个空格并保存文件),然后才能在此类中看到错误。 However, what is even more misleading, is the fact, that the compilation error shown in Eclipse has nothing to do with the real copilation error. 但是,更令人误解的是,事实是Eclipse中显示的编译错误与实际的编译错误无关。 - The compilation error is shown in the next line after the erroneous line. -错误行之后的下一行将显示编译错误。
I don't want to disable the Workspace Resolution in the m2eclipse plugin, so that the newest sources are always used for the Eclipse's compile. 我不想在m2eclipse插件中禁用“工作空间分辨率”,以便始终将最新的源用于Eclipse的编译。 However, it would be nice to see such compile error immediately in Eclipse and not have to perform a Maven build in order to see that compile error in Eclipse. 但是,很高兴能立即在Eclipse中看到这样的编译错误,而不必执行Maven构建就可以在Eclipse中看到该编译错误。

Is it possible to see the compile error immediately after the Eclipse's compile? Eclipse编译后是否可以立即看到编译错误?

I am not sure why you are facing a compilation error on Maven build. 我不确定为什么您在Maven构建中会遇到编译错误。 Please see a sample project on Github that demonstrates a multi-module Maven set up with the module relationships as shown in your post above. 在Github上查看一个示例项目,示例项目演示了一个多模块Maven,它具有模块关系,如上面的文章中所示。

  • interface - This module is the base every other module depends upon and is analogous to base-api module in your post. interface -该模块是其他所有模块所依赖的基础,类似于您帖子中的base-api模块。 It contains a single interface called IGreeter , similar in essence to the IFoo interface in your post; 它包含一个名为IGreeter接口,从本质上类似于您帖子中的IFoo接口。
  • implementation - This module depends on interface and contains a single class Greeter that implements IGreeter . implementation -该模块取决于interface并且包含实现IGreeter的单个Greeter类。 It is analogous to base module in your post. 它类似于您帖子中的base模块。 Note that there is no other code in this module, not even unit tests; 请注意,该模块中没有其他代码,甚至没有单元测试。
  • service - This provides a fictional service layer and depends on interface . service提供虚构的服务层,并取决于interface Additionally, it has a test scoped dependency on implementation . 此外,它具有对test implementation依赖范围。 It is analogous to module A in your post. 它类似于您帖子中的module A It contains a class called GreetingService and its associated integration test class GreetingServiceTest . 它包含一个名为GreetingService的类及其关联的集成测试类GreetingServiceTest The test can be run from the command line or from Eclipse; 可以从命令行或从Eclipse运行测试。
  • web - This provides a fictional application layer and depends on all the other modules. web -这提供了虚构的应用程序层,并且依赖于所有其他模块。 It uses GreetingService to display a greeting message to the user. 它使用GreetingService向用户显示问候消息。 Run it as mvn clean package tomcat7:run . 作为mvn clean package tomcat7:run

This sample shows how you can write business logic by depending only on certain interfaces ( service module). 此样本说明如何仅通过某些接口( service模块)来编写业务逻辑。 It shows how a specific interface implementation can be chosen during testing to prove that integration works. 它显示了如何在测试过程中选择特定的接口实现来证明集成有效。

This sample also shows how you can choose a specific implementation of an interface at runtime ( web module). 此样本还显示了如何在运行时( web模块)选择接口的特定实现。 If you had multiple implementations of IGreeter and wanted to use a specific one at runtime, you would add that as a dependency to the web module instead of adding the implementation module. 如果您有IGreeter多个实现,并且想在运行时使用特定的实现,则可以将它作为对web模块的依赖项添加,而不是添加implementation模块。

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

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