55

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?

Update

Ok, I found something here and here

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

Mauli
  • 16,863
  • 27
  • 87
  • 114
  • 1
    Important safety tip. Maven command line detects cycles within one reactor. If the cycle would only be exposed by activating a profile then your command line build will not report an error while your eclipse build may. If you try to debug this be sure to activate all the profiles to include all your modules (e.g., mvn -Pcore,buildtools dependency:tree) otherwise you may miss the problem. – Peter Kahn Jul 03 '12 at 18:01
  • Links are broken (and they were both the same anyway). – Noumenon Jan 04 '20 at 23:53

17 Answers17

104

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

In Eclipse go to:

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

alex
  • 479,566
  • 201
  • 878
  • 984
user2582855
  • 1,041
  • 1
  • 7
  • 2
  • 6
    this answer is correct since open source projects using gradle have circular dependency and they build ok on gradle. eclipse should not cry about this as a error – Siddharth Oct 12 '15 at 04:49
  • The errors went up from 6 to 2000+ when I did this. – Arvindh Mani Nov 15 '16 at 23:23
  • How to permanently remove this warning. It causes some of my problems like if there is a unused import. –  Jan 30 '18 at 02:10
  • 1
    In my case too, errors went from 15 to 2000+ when I did this change. Any other solution? – Sanchit Khera Feb 23 '18 at 06:58
  • 2
    I think this is not an appropriate solution as we should always resolve the cyclic dependencies. – Manish Jul 24 '19 at 17:54
15

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.

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.

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.

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!

The Student
  • 27,520
  • 68
  • 161
  • 264
Jack
  • 151
  • 1
  • 3
  • 4
    But if there is a case that P1 references,P2 references P1. For eg. P1 contains DAO layer code and P2 contains utilities. Utilities sometimes use DAO layer to get configuration properties from DB table and DAO layer uses the utilities. So how to resolve this problem? – Sanjeev Kumar Dangi Jun 20 '11 at 10:06
  • me too have the exactly same problem @Sanjeev. Have you got any solution, please share... – Arundas K V May 23 '14 at 09:54
  • @SanjeevKumarDangi maybe put the utilities specific to the DAO in the DAO project. – The Student Apr 11 '16 at 13:53
8

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
6

Sometimes marking as Warning

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

doesn't solve the problem because eclipse don't compile the projects that have another project in the dependencies that isn't compiled.

So to solve this problem you can try forcing Eclipse to compile every class that it be able to.

To make this just:

  1. Deselect

Windows -> Preferences -> Java-> Compiler -> Building -> Abort build when build path error occur

  1. Clean and rebuild all project

Project -> Clean...

  1. Reselect:

Windows -> Preferences -> Java-> Compiler -> Building -> Abort build when build path error occur

If you have the Automatic Build selected then you will not need to do this every time that you change the code

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sambuccid
  • 166
  • 2
  • 4
4

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.

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

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

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.

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!

SteveD
  • 5,396
  • 24
  • 33
  • Thanks for the tip about Eclipse's manifest editor "Dependency Analysis > Look for Cycles" feature. It did actually show the cycle (but only in one of the packages in the cycle). – wisbucky Oct 09 '18 at 00:01
3

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.

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.

Use class libraries instead of project references.

ataulm
  • 15,195
  • 7
  • 50
  • 92
Ryan Zoerner
  • 63
  • 1
  • 10
  • 1
    I would highly prefer Project References since it allows me to jump between source codes of different projects (Using Class Libraries had me end up in binaries, and then I had to manually go looking for the corresponding source files - which was a headache). However, in this situation of a cycle dependency, I could only solve it by Having A refer to B as Project Reference, and B refer back to A as Class libraries. A little sacrifice I had to do to solve the cycle dependency problem. – ADTC Oct 28 '13 at 07:23
3

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

Petr Gladkikh
  • 1,906
  • 2
  • 18
  • 31
2

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

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.

Rich Seller
  • 83,208
  • 23
  • 172
  • 177
1

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.

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).

AlBlue
  • 23,254
  • 14
  • 71
  • 91
  • +1 for mentioning the Eclipse problems with .project files.. Sometimes the only solution is throwing away everything related to Eclipse and let it figure it out from the pom again.. – Tim Jul 27 '09 at 21:53
1

Although "Mark Circular Dependencies" enables you to compile the code, it may lead to a slower environment and future issues.

That's happening because at some point Eclipse has lost it's directions on your build path.

1 - Remove the project and it's references from the workspace. 2 - Import it back again. 3 - Check the references.

It is the best solution.

Machado
  • 14,105
  • 13
  • 56
  • 97
0

When I've had these problems it always has been a true cycle in the dependencies expressed in 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.

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,

djna
  • 54,992
  • 14
  • 74
  • 117
0

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

0

Just restarting Eclipse fixed the issue in my project

0

I faced this same problem today. The error was apt. By mistake, I added cyclic dependency. A was dependent on B. In my case, by mistake, apart from adding B as dependent to A. I added A as dependent to B too. It was a foolish mistake.

MansoorShaikh
  • 913
  • 1
  • 6
  • 19
0

I have this problem,too.I just disable Workspace resolution,and then all was right.enter image description here

Rain
  • 1
  • 1
  • 2
    This is not a solution to the problem. Maven is complaining about circular dependency of the artifacts. It will create the problems. – kk. Jun 06 '17 at 10:54
0

This could happen when you have several projects that include each other in JAR form. What I did was remove all libraries and project dependencies on buildpath, for all projects. Then, one at a time, I added the project dependencies on the Project Tab, but only the ones needed. This is because you can add a project which in turn has itself referenced or another project which is referencing some other project with this self-referencing issue.

This resolved my issue.

Camilo
  • 439
  • 5
  • 13
0

Eclipse had a bug which reported more cycles than necessary. This has been fixed with the 2019-12 release. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=551105

vogella
  • 24,574
  • 4
  • 29
  • 26