Java 在项目xxx的构建路径中检测到一个循环-构建路径问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1084866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:24:55  来源:igfitidea点击:

A cycle was detected in the build path of project xxx - Build Path Problem

javaeclipsemaven-2osgibundle

提问by Mauli

I'm in the process of converting my projects to OSGI bundles using maven and eclipse. Maven builds the stuff just fine, only I get the above error now within Eclipse. How can I find out which project causes this? Is there a special view or something? How can this happen, I would expect that maven can detect cyclic dependencies as well?

我正在使用 maven 和 eclipse 将我的项目转换为 OSGI 包。Maven 构建的东西很好,只有我现在在 Eclipse 中收到上述错误。我怎样才能找出是哪个项目导致了这种情况?有什么特别的看法吗?这怎么会发生,我希望 maven 也可以检测循环依赖?

Update

更新

Ok, I found something hereand here

好的,我在这里这里找到了一些东西

Could this be caused by the felix maven-bundle-plugin which does for each export also an import?

这可能是由 felix maven-bundle-plugin 引起的,它对每个导出也是一个导入吗?

回答by djna

When I've had these problems it always has been a true cycle in the dependencies expressed in Manifest.mf

当我遇到这些问题时,它始终是 Manifest.mf 中表达的依赖项中的真正循环

So open the manifest of the project in question, on the Dependencies Tab, look at the "Required Plugins" entry. Then follow from there to the next project(s), and repeat eventually the cycle will become clear.

因此,打开相关项目的清单,在 Dependencies 选项卡上,查看“Required Plugins”条目。然后从那里到下一个项目,并重复最终循环将变得清晰。

You can simpify this task somewhat by using the Dependency Analysis links in the bottom right corner of the Dependencies Tab, this has cycle detection and easier navigation depdendencies.

您可以通过使用依赖项选项卡右下角的依赖项分析链接来稍微简化此任务,这具有循环检测和更容易的导航依赖项。

I also don't know why Maven is more tolerant,

我也不知道为什么Maven更宽容,

回答by Rich Seller

Maven willfail the build if it detects a cycle, as the dependencies must be a tree.

如果Maven检测到一个循环,它导致构建失败,因为依赖项必须是一棵树。

You may find that you have additional declarations in the manifest.mf over those defined in the pom.xml. any extra declaration could introduce a cycle that wouldn't be apparent to Maven.

您可能会发现在 manifest.mf 中除了 pom.xml 中定义的那些声明之外还有其他声明。任何额外的声明都可能引入一个对 Maven 来说并不明显的循环。

回答by SteveD

In simple terms, a cycle is when bundle A depends on bundle B which depends on bundle A. If this is source code, then there's no way of building the bundles seperately in one pass.

简单来说,一个循环就是当包 A 依赖于包 B 而包 B 又依赖于包 A。如果这是源代码,那么就没有办法一次性单独构建包。

Since the problem only shows in Eclipse, it may be a binary circular dependency as opposed to a source code circular dependency.

由于该问题仅在 Eclipse 中出现,因此它可能是二进制循环依赖而不是源代码循环依赖。

There is support for binary cycles in recent versions of Eclipse: Eclipsesource Blog

最新版本的 Eclipse 支持二进制循环:Eclipsesource 博客

If the cycle is in your code, then I suggest refactoring the code by breaking out some of the code to a 3rd bundle to remove the circular dependency.

如果循环在您的代码中,那么我建议通过将某些代码分解为第三个包来重构代码以消除循环依赖。

And watch out if you are using OSGi fragments (a common pattern for unit testing) as it is very easy to introduce cycles.

并注意您是否使用 OSGi 片段(单元测试的常见模式),因为它很容易引入循环。

Eclipse's manifest editor does have functionality on the "Dependencies" tab for looking for cycles (you need to click on "Dependency Analysis"), but I've never seen it show a cycle even when Eclipse has a big red X telling me there's a cycle!

Eclipse 的清单编辑器在“依赖关系”选项卡上确实具有用于查找周期的功能(您需要单击“依赖关系分析”),但我从未见过它显示周期,即使 Eclipse 有一个大红色 X 告诉我有一个循环!

回答by AlBlue

As well as the Require-Bundle form of dependency management (most similar to Maven's pom dependencies), it's also possible to have Import-Package dependencies. It's much easier to introduce circular dependencies with Import-Package than Require-Bundle, but YMMV.

除了依赖管理的 Require-Bundle 形式(最类似于 Maven 的 pom 依赖),还可以有 Import-Package 依赖。使用 Import-Package 引入循环依赖比使用 Require-Bundle 容易得多,但是 YMMV。

Also, Eclipse projects have a 'project references' which says which other projects it depends on. Eclipse uses this at a high level to decide what projects to build, and in which order, so it's quite possible that your Manifest.MF lists everything correctly but the project references are out of whack. Right click on a project and then go to properties - you'll see which projects you depend on. If you're a text kind of person, open up the .project files and see which ones you depend on there - it's probable that a project cyclic link is being defined at that level instead (often caused when you have an A-B dependency and then flipped from B-A but without updating the .project references).

此外,Eclipse 项目有一个“项目引用”,说明它依赖于哪些其他项目。Eclipse 在较高级别使用它来决定要构建的项目以及构建顺序,因此很可能您的 Manifest.MF 正确列出了所有内容,但项目引用不正常。右键单击一个项目,然后转到属性 - 您将看到您所依赖的项目。如果您是文本类型的人,请打开 .project 文件并查看您所依赖的文件 - 很可能是在该级别定义了项目循环链接(通常在您具有 AB 依赖项时导致,然后从 BA 翻转但没有更新 .project 引用)。

回答by Petr Gladkikh

I faced similar problem a while ago and decided to write Eclipse plug-in that shows complete build path dependency tree of a Java project (although not in graphic mode - result is written into file). The plug-in's sources are here http://github.com/PetrGlad/dependency-tree

不久前我遇到了类似的问题,并决定编写 Eclipse 插件来显示 Java 项目的完整构建路径依赖树(虽然不是在图形模式下 - 结果写入文件)。插件的来源在这里http://github.com/PetrGlad/dependency-tree

回答by Hyman

When we have multiple projects in workspace, we have to set the references between the projects, not among the projects. If P1 references P2, P2 references P3, and P3 reference back to P1. That will cause a cycle.

当我们的工作空间中有多个项目时,我们必须设置项目之间的引用,而不是项目之间的引用。如果 P1 引用 P2,P2 引用 P3,而 P3 又引用回 P1。那会造成一个循环。

The Solution is to draw a Diagram of the reference between projects in workspace. Check the Java Build Path of each of the projects to see the Tab of the Projects window. Take out the Project that are refering back to the main project, e.g. P3 references P1, in this example above.

解决方案是在工作区中绘制项目之间的参考图。检查每个项目的 Java 构建路径以查看“项目”窗口的选项卡。取出引用回主项目的项目,例如,在上面的示例中,P3 引用 P1。

Detailed operation is to select P3 project in RAD OR eclipse, right click on the project and choose the properties option, it brings up a new window for properties of P3. Click on the "Java Build Path" section, Choose the "Projects" option Tab. You can see the P3 has referenced P1 in the field. Select the P1 reference, click "Remove" button on the right side of the window. Then, click okay. The IDE will start to reset the path automatically.

具体操作是在RAD OR eclipse中选择P3项目,在项目上右击选择属性选项,弹出P3属性的新窗口。单击“Java Build Path”部分,选择“Projects”选项选项卡。您可以看到 P3 在字段中引用了 P1。选择 P1 引用,单击窗口右侧的“删除”按钮。然后点击确定。IDE 将开始自动重置路径。

Done.

完毕。

Keep find all of the mis-referenced reference in every each projects until you have the right references to each of the projects in your Diagram. Good Luck!

继续查找每个项目中所有错误引用的引用,直到您对图表中的每个项目都有正确的引用。祝你好运!

回答by garrett.patterson

I had this due to one project referencing another.

我有这个是因为一个项目引用了另一个项目。

  1. Remove reference from project A to project B
  2. Try running stuff, it will break
  3. Re-add reference
  4. Clean/Clean and build
  5. Back in business
  1. 删除从项目 A 到项目 B 的引用
  2. 尝试运行东西,它会坏掉
  3. 重新添加参考
  4. 清洁/清洁和构建
  5. 回到工作

回答by Ryan Zoerner

Problem

问题

I have an old project that tests two different implementations of a Dictionary interface. One is an unsorted ArrayList and the other is a HashTable. The two implementations are timed, so that a comparison can be made. You can choose which data structure from the command line args. Now.. I have another data structure which is a tree structure. I want to test the time of it, in order to compare it to the HashTable. So.. In the new dataStructure project, I need to implement the Dictionary interface. In the Dictionary project, I need to be able to add code specific to my new dataStructure project. There is a circular dependency. This means that when Eclipse goes to find out which projects are dependent on project A, then it finds project B. When it needs to find out the sub-dependencies of the dependent projects, it finds A, which is again, dependent on B. There is no tree, rather a graph with a cycle.

我有一个旧项目,用于测试 Dictionary 接口的两种不同实现。一个是未排序的 ArrayList,另一个是 HashTable。这两个实现是定时的,因此可以进行比较。您可以从命令行参数中选择哪种数据结构。现在.. 我有另一个数据结构,它是一个树结构。我想测试它的时间,以便将它与HashTable进行比较。所以..在新的dataStructure项目中,我需要实现Dictionary接口。在 Dictionary 项目中,我需要能够添加特定于我的新 dataStructure 项目的代码。存在循环依赖。这意味着当Eclipse去查找哪些项目依赖于项目A时,它会找到项目B。当它需要查找依赖项目的子依赖时,它会找到A,这又是,

Solution

解决方案

When you go to configure your build path, instead of entering dependent projects ('projects' tab), go to the Libraries tab. Click the 'Add Class Folder...' button (assuming that your referenced projects are in your workspace), and select the class folder. Mine is \target. Select this as a library folder. Do this in project A, to reference project B. Do this in project B to reference project A. Make sure that you don't reference \target\projectNameFolder or you won't have a matching import. Now you won't have to remove a dependency and then reset it, in order to force a rebuild.

当您去配置构建路径时,不要输入依赖项目(“项目”选项卡),而是转到“库”选项卡。单击“添加类文件夹...”按钮(假设您引用的项目在您的工作区中),然后选择类文件夹。我的是\目标。选择它作为库文件夹。在项目 A 中执行此操作以引用项目 B。在项目 B 中执行此操作以引用项目 A。确保您不引用 \target\projectNameFolder,否则您将不会有匹配的导入。现在您不必删除依赖项然后重新设置它,以强制重建。

Use class libraries instead of project references.

使用类库而不是项目引用。

回答by user2582855

Mark circular dependencies as "Warning" in Eclipse tool to avoid "A CYCLE WAS DETECTED IN THE BUILD PATH" error.

在 Eclipse 工具中将循环依赖标记为“警告”以避免“在构建路径中检测到循环”错误。

In Eclipse go to:

在 Eclipse 中转到:

Windows -> Preferences -> Java-> Compiler -> Building -> Circular Dependencies

Windows -> Preferences -> Java-> Compiler -> Building -> Circular Dependencies

回答by sjchoudary

Try to delete references and add it back, some times eclipse behave weird because until and unless you fix that error it wont allow you refresh. so try to delete all dependencies project and add it back Clean and build

尝试删除引用并将其添加回来,有时 eclipse 的行为很奇怪,因为除非您修复该错误,否则它不会允许您刷新。所以尝试删除所有依赖项目并将其添加回来 Clean and build