问题描述
该问题是对以下问题的回答的结果:自动布线场的注入失败在多模块Maven项目中-NoSuchBeanDefinitionException
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模块的以下结构:
According to the answer from the aforementioned question, I configured the following structure of my Maven modules:
在这种情况下,基本模块中的Foo类可以在模块A中的测试中使用(例如,使用IFoo接口自动装配时),但不能在生产代码中使用(包含在src中) /main/java).
In this case, the class Foo from the base module can be used in tests in the module A (e.g. when it's autowired using the IFoo interface), but can't be used in the production code (which is contained in src/main/java).
当有人在生产代码(在src/main/java下)中的模块A中的类中添加对Foo类的引用时,Maven构建失败.但是,在这种情况下Eclipse中的自动构建不会失败,因为我使用了启用了工作区分辨率的m2eclipse插件.我必须运行maven buiild(mvn全新安装)才能看到编译失败.之后,该错误仍然不会立即在Eclipse的受影响类中可见.我必须在受影响的Java类中进行更改(例如,添加空格并保存文件),然后才能在此类中看到错误.但是,更令人误解的事实是,Eclipse中显示的编译错误与实际的编译错误无关. -错误行之后的下一行显示编译错误.
我不想在m2eclipse插件中禁用工作区分辨率",以便始终将最新的源用于Eclipse的编译.但是,很高兴能立即在Eclipse中看到这样的编译错误,而不必执行Maven构建就可以在Eclipse中看到该编译错误.
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). However, the automatic build in Eclipse doesn't fail in such case, because I use the m2eclipse plugin which has the Workspace Resolution enabled. I have to run the maven buiild (mvn clean install) to see the compilation failure. Afterwards, the error is still not immediately visible in the affected class in Eclipse. I have to make a change in the affected java class (e.g. add a space and save the file) and then I can see an error in this class. 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. - 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. 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编译后立即看到编译错误?
Is it possible to see the compile error immediately after the Eclipse's compile?
推荐答案
我不确定为什么在Maven构建中会遇到编译错误.请在Github上查看示例项目,该示例项目演示了多模块Maven集上面您的帖子中显示的模块关系.
I am not sure why you are facing a compilation error on Maven build. 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.
-
interface
-该模块是其他每个模块所依赖的基础,类似于您帖子中的base-api
模块.它包含一个名为IGreeter
的界面,本质上与您帖子中的IFoo
界面相似; -
implementation
-此模块依赖于interface
,并包含实现IGreeter
的单个类Greeter
.它类似于您帖子中的base
模块.请注意,此模块中没有其他代码,甚至没有单元测试; -
service
-这提供了虚构的服务层,并且取决于interface
.此外,它在implementation
上具有test
范围内的依赖关系.它类似于您帖子中的module A
.它包含一个名为GreetingService
的类及其关联的集成测试类GreetingServiceTest
.该测试可以从命令行或从Eclipse运行; -
web
-这提供了虚构的应用程序层,并且依赖于所有其他模块.它使用GreetingService
向用户显示问候消息.以mvn clean package tomcat7:run
身份运行.
interface
- This module is the base every other module depends upon and is analogous tobase-api
module in your post. It contains a single interface calledIGreeter
, similar in essence to theIFoo
interface in your post;implementation
- This module depends oninterface
and contains a single classGreeter
that implementsIGreeter
. It is analogous tobase
module in your post. Note that there is no other code in this module, not even unit tests;service
- This provides a fictional service layer and depends oninterface
. Additionally, it has atest
scoped dependency onimplementation
. It is analogous tomodule A
in your post. It contains a class calledGreetingService
and its associated integration test classGreetingServiceTest
. The test can be run from the command line or from Eclipse;web
- This provides a fictional application layer and depends on all the other modules. It usesGreetingService
to display a greeting message to the user. Run it asmvn clean package tomcat7:run
.
此样本显示了如何仅依靠某些接口(service
模块)来编写业务逻辑.它显示了如何在测试过程中选择特定的接口实现以证明集成有效.
This sample shows how you can write business logic by depending only on certain interfaces (service
module). It shows how a specific interface implementation can be chosen during testing to prove that integration works.
此示例还显示了如何在运行时(web
模块)选择接口的特定实现.如果您有IGreeter
的多种实现,并且想在运行时使用特定的实现,则可以将其作为对web
模块的依赖项,而不是添加implementation
模块.
This sample also shows how you can choose a specific implementation of an interface at runtime (web
module). 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.
这篇关于具有范围“测试"的Maven依赖关系被指定为“测试" &在m2eclipse中启用了工作区分辨率-仅在Maven构建之后才显示编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!