简体   繁体   English

如何在Eclipse中解决循环项目依赖关系。 有什么办法只能使其中一个依赖项运行时吗?

[英]How to solve a cyclic project dependency in Eclipse. Any way to make one of the dependencies runtime only?

I have a project that basically consists of 2 modules. 我有一个基本上由2个模块组成的项目。 I have a Eclipse workspace and 2 java projects (maven) in it which represent those two modules. 我有一个Eclipse工作区和2个Java项目(Maven),它们代表了这两个模块。

To go a bit more in depth: 要深入一点:

  • Project 1 : Core Module (Has Core Implementation and API Interfaces) 项目1 :核心模块(具有核心实现和API接口)
  • Project 2 : Auxiliary Module (A Server) 项目2 :辅助模块(服务器)

Project 2 requires to have Project 1 as Dependency in order to access the API Interfaces during compile time. 为了在编译期间访问API接口, Project 2需要具有Project 1作为依赖项。 But the problem is that Project 1 requires Project 2 in order to work properly (This is only a runtime requirement though). 但是问题在于项目1需要项目2才能正常工作(尽管这只是运行时要求)。

So when I add in each project the other project as dependency a cyclic dependency error is shown, as Eclipse doesn't knows in which order to compile the classes. 因此,当我在每个项目中添加另一个项目作为依赖项时,会显示一个周期性的依赖项错误,因为Eclipse不知道以何种顺序编译类。

The way I managed to make this work is that I added the "bin" folder from Project 2 as dependency to Project 1 as also all of Project 2 dependencies (Which are maven based). 我设法完成这项工作的方法是,我将Project 2的“ bin”文件夹作为对Project 1的依赖项以及所有Project 2的依赖项(这都是基于Maven的)添加的。 This though makes the whole project completely bound to my system, as all paths are adjusted to my computer setup. 但是,这使得整个项目完全绑定到我的系统上,因为所有路径都根据我的计算机设置进行了调整。

Is there any proper way to specify another project to be dependency only on "runtime", and not on "compile" and avoid the cyclic redundancy in Eclipse? 是否有任何适当的方法将另一个项目指定为仅依赖于“运行时”而不依赖于“编译”,并避免Eclipse中的循环冗余?


To explain in a more visual way. 以更直观的方式进行解释。 What I need is: 我需要的是:

  • Project 2 --- During compilation depends on ---> Project 1 项目2 ---在编译过程中取决于---> 项目1

  • Project 1 --- During runtime depends on ---> Project 2 项目1 ---在运行期间取决于-> 项目2

Eclipse can handle circular project dependencies but by default circular dependencies will be reported as an error for good reasons (splitting code into projects with circular dependencies makes no sense). Eclipse可以处理循环项目依赖关系,但是默认情况下,出于良好的原因,循环依赖关系将被报告为错误(将代码拆分为具有循环依赖关系的项目是没有意义的)。

You can decrease the circular dependencies project problem severity from Error to Warning for both projects as follows (not recommended): 您可以如下将两个项目的循环依赖项目问题严重性从“ 错误”降低为“ 警告 ”(不建议):

  1. In Project > Properties: Java Compiler > Building 在“ 项目”>“属性:Java编译器”>“构建”中
    1. check the checkbox Enable project specific settings and 选中启用项目特定设置复选框,然后
    2. in the section Build path problems choose for Circular dependencies Warning instead of Error 在“ 构建路径问题 ”部分中,为循环依赖项选择 警告”而不是“ 错误”

In Maven, dependencies have a scope. 在Maven中,依赖项具有作用域。 They can be declared as "compile", "runtime", "test" etc. 可以将它们声明为“编译”,“运行时”,“测试”等。

You can declare a compile dependency on Project1 in Project2 and a runtime dependency in Project1 on Project2. 您可以在Project2中声明对Project1的编译依赖性,并在Project2中声明Project1的运行时依赖性。 I have never tried to define dependencies in such a way, but it will probably work. 我从未尝试过以这种方式定义依赖项,但它可能会起作用。

But, much more important: If Project1 cannot be used without Project2 and vice versa, why are they different projects? 但是,更为重要的是:如果没有Project2就不能使用Project1,反之亦然,为什么它们是不同的项目?

So based on @JF-Meier's comments, I found another way how to make this work. 因此,基于@ JF-Meier的评论,我找到了另一种方法来实现此目的。 The whole idea was basically to have Project 2 loosely coupled to Project 1 , to allow Project 2 to be swapped with other implementations, while still being a vital part to Project 1 to work. 整个想法基本上是使Project 2Project 1松散耦合,以允许Project 2与其他实现互换,同时仍然是Project 1工作的重要组成部分。

To solve the circular dependency issue, I've just created another project that sets Project 1 and Project 2 as it's dependency, and from there I launch Project 1 . 为了解决循环依赖问题,我刚刚创建了另一个项目,将Project 1Project 2设置为依赖项,然后从那里启动Project 1 This solved the circular dependency issue for me and everything works. 这为我解决了循环依赖问题,一切正常。

The result: 结果:

  • Project 0 : Launcher to start Project 1 项目0 :启动器启动项目1
  • Project 1 : Core Module (Has Core Implementation and API Interfaces) 项目1 :核心模块(具有核心实现和API接口)
  • Project 2 : Auxiliary Module (A Server) 项目2 :辅助模块(服务器)

And the dependencies: 和依赖项:

  • Project 1 --- Depends on nothing 项目1 ---一无所获

  • Project 2 --- Depends on ---> Project 1 项目2 ---取决于-> 项目1

  • Project 0 --- Depends on ---> Project 1 and Project 2 项目0 ---取决于---> 项目1项目2

And like this I can launch Project 1 from Project 0 while having Project 2 on the classpath (So Project 1 can find Project 2 as required). 像这样,我可以从项目0启动项目1 ,同时在类路径上具有项目2 (因此项目1可以根据需要找到项目2 )。

I marked the response from @howlger as accepted as it also fixed my issue. 我将@howlger的回复标记为已接受,因为它也解决了我的问题。 But in case my question was not well understantable, I'm putting here how I finally fixed it, in case it may be helpfull for somebody else trying to achieve the same like me. 但是,如果我的问题不是很清楚,那么我将在这里说明如何最终解决它,以防其他人尝试像我一样实现相同目标。

Thanks for all the help as always here! 感谢您一如既往的所有帮助!

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

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