Maven build is successful, Eclipse detects cycle in dependencies - eclipse

I'm working on a large project that consists of many smaller projects (about 140) that are all managed by maven. There is a master pom and a pom for each individual project.
Now I have introduced 2 new small projects, A and B, with the dependency A -> B. Furthermore B depends on another existing project C, so B -> C. A is used by another project D which is (as far as I've seen) independent from C. I'm not sure because the structure is very complex and I don't want to sit there sifting through poms all day.
This means I have a dependency graph like this: D -> A -> B -> C
When I build the master pom in maven (clean install) it finishes successfully. So does the eclipse:eclipse goal that generates the Eclipse project files. When I refresh the projects in Eclipse it rebuilds everything and finishes with the error "A cycle was detected in the build path of project...". This error occurs in about 30 of the 140 projects. Ofcourse Eclipse doesn't tell me what the cycle looks like...
How can it be that maven does not detect a cycle in the dependencies but eclipse does? I thought that the Maven Eclipse plugin just mapped the dependencies from the pom to the Eclipse .project and .classpath files?
The fact that Maven doesn't complain about cycles tells me that there is no dependency C -> D.

I had the same symptoms but with no actual maven cycle. I dealt with this in eclipse Indigo in a workaround fashion: I closed and deleted the eclipse projects that reported this error (not deleting the files making up the project), then re-created the maven projects in eclipse by importing the source files into my workspace with "File->Import...->Maven->Existing Maven Projects". That took care of it.

You may have dependencies with the scope test or runtime. Maven only looks for cycles in the compile phase. Eclipse or m2e doesn't differ the dependencies in scopes.

You probably have previous Eclipse settings on your projects, try this steps:
Remove all projects from Eclipse.
Run 'mvn clean eclipse:clean;' from the console or delete all hidden files from all project folders: .project, .classpath, .wtpmodules, org.eclipse.core.resources.prefs, ...
Reimport the projects into Eclipse
This may fix the problem!

Related

Do dependencies need to be added through both the Eclipse GUI and the Gradle configuration files?

Two Gradle Git projects need to be created, within the same repository, using the Eclipse IDE. Project A depends upon a third-party library (a JAR, source code, Javadoc and natives) and project B depends upon project A.
Without Gradle, both projects would be created and Git would be enabled with Project Explorer > [Project Name] > Team > Share Project. Then, dependencies would be added via Project Explorer > [Project Name] > Build Path > Configure Build Path, followed by Projects > Add and Libraries > Add External JARs (which would also allow the specification of the locations of related source code, Javadoc and natives). This would enable automatic code completion of Project A from Project B as well as source code and Javadoc integration, within Eclipse.
With Gradle, should the settings.gradle or build.gradle files be manually edited, without adding dependencies through the Eclipse GUI, or must both be used simultaneously? Additionally,how does this effect which hidden files should be indexed by Git (.project, .classpath, .settings, .gradle)?
How is this setup through Eclipse and Gradle?
When you use a build-tool like Gradle (or maven), it is kind of assumed that the build-tool is in charge of configuring stuff like project dependencies and classpaths.
This kind of conflicts with the Eclipse UI which is kind of built to allow you to manage classpath / dependencies via its own ui.
But the Eclipse UI only controls what eclipse uses as the classpath when the Eclipse JDT compiler compiles your code... inside Eclipse.
So if you change things that way, then Gradle will not know about these dependencies and the build won't work.
Yep, this is definitely confusing :-)
The proper thing to do is manage your dependencies and project configuration entirely through gradle. So that means editing the build.gradle and settings.gradle.
The tools (BuildShip or STS Gradle Tools) provide a 'bridge' to try and configure Eclipse projects in conformance with your build.
E.g they may offer a 'Update Project' or 'Refresh Dependencies' command in project context menu.
Even if you don't use Gradle tooling, this kind of holds true. You then would use Gradle's 'ecplise' plugin and run a command like
gradle cleanEclipse eclipse
To generate the eclipse project config from the build config. Then import the project into Eclipse as configured by gradle. Also in this case, it would be a bad idea to use Eclipse UI to make changes to the buildpath since ultimately it has the same issue, changes you make to those generated files may make things compile inside Eclipse, but Gradle doesn't know you changed anything. And the next time you run gradle cleanEclipse eclipse your changes are also blown away.
As to your specific questions:
should the settings.gradle or build.gradle files be manually edited ...
Yes.
... without adding dependencies through the Eclipse GUI ...
Yes.
... or must both be used simultaneously?
No, only configure things in gradle. Then 'synch it up' to eclipse with some tool (BuildShip / STS Gradle / gradle cleanEclipse eclipse)
How does this effect which hidden files should be indexed by Git (.project, .classpath, .settings, .gradle)?
General rule of thumb. Only index the stuff that defines gradle's behaviour (there may be some exceptions, but in general try to minimize them only violate this rule if you have a good reason).
So specifically don't put in git these 'eclipse metadata'
.settings
.project
.classpath
Do put in git: the gradle wrapper and its properties file.
Gradle also has a .gradle folder. It belongs to gradle, not eclipse, buts is caches and things which are 'transient'. You don't want those in git either.

Eclipse can't resolve transitive dependency projects?

In a Java project in Eclipse, I am trying to debug project A.
Project A has a dependency on B.jar, normally B.jar comes from my .m2/repository.
but now I want to make some temp changes to B's code, and have it reflected in A,
so I directly edit the source code in B project in eclipse, and set B as a dependency project in A's build path. Additionally, both A and B refer to C.jar as their dependencies.
This builds A fine, but when it is run, A's classpath contains 2 copies of all the classes and resources in C.jar. this creates a problem for those hibernate hbm.xml mapping files in C.jar and I got errors saying duplicate mapping for...
This looks to be a defect in eclipse, in that it lacks the resolution ability as maven posseses. is there a way to work around this? (apart from building B and installing to .m2 instead of having it as a dependency project)
Thanks
You mentioned "[setting] B as a dependency project in A's build path" - do you mean editing eclipse's build path or editing the project's POM? I'm guessing the former, in which case make sure that you have the latest version of the m2eclipse plugin installed and that both projects A & B are Maven eclipse projects. If it is set up correctly then your projects should look have an 'M' on their project icons, and the icons of any workspace dependencies should look like folders.

Maven integration into eclipse, Problems with Dependencies vs. Referenced Libraries

he fellas, i need your help again.
Im trying to develop a new part for an existing software. The parts are all done using Maven. I have to work in several projects at once, which are all continously opened in Eclipse. In Eclipse i use M2E, but i tend to edit the poms directly in xml.
So, now I can build (clean install) the projects individually, works ok. But Eclipse now has problems with the online help: It shows me errors. Eclipse cant seem to see classes which are placed in the same project, but different packages. Note that it works fine when i compile it!
Also after I run the pom the dependencies dont show up as "Maven Dependencies" but as "Referenced Libraries".
This persists after I run mvn eclipse:eclipse from outside and refresh.
Thanks so much for your help!
"Do not ever do mvn eclipse:eclipse", only do mvn eclipse:clean from command line first and then import "as maven projects" using your eclipse M2E plugin.
M2E Plugin knows how to handle Maven Dependencies, whereas mvn eclipse:eclipse knows how to handle Referenced Libraries, and they are NOT compatible with each other. You can always do mvn update project in Eclipse after mvn eclipse:clean.
I had to delete the maven-project, too, after using eclipse:eclipse. It added the references as "Referenced Libraries".
Manually deleting the .project and .settings files in the project folder and importing the project again helped. I used the M2E import. Afterwards it displayed the libraries within the "Maven Dependencies". Subsequently I was able to run it on my local webserver.
I do not really know the reason for the behavior, but deleting the project and reimporting it solved it.
I agree with kisna. As an alternative to fix the project if already contains both "Referenced Libraries" and "Maven Dependencies", you can do the following:
https://docs.oracle.com/cd/E14545_01/help/oracle.eclipse.tools.weblogic.doc/html/j2eelib/operations/opRemoveLibRefFromClasspath.html
Right-click on the project and select Properties -> Java Build Path -> Libraries tab -> Find the entry in the list of libraries called Shared Library [] or/and all libraries that starts with 'M2_REPO/', and then select it/them and remove.

Eclipse build path with other projects

I've got several projects in Eclipse (all are Maven projects) and one main project (also Maven project) which depends on the rest. I tried add this dependency by setting java build path (right click on project -> preferences -> java build path -> (tab) projects -> add). But there is a problem while executing maven install goal - this is compilation error: ... (class) ... "cannot be resolved" ... - this is definitely looks like maven does't see my resources from other projects. Eclipse is only warning me something like this: "Classpath entry /my-subproject1 will not be exported or published. Runtime ClassNotFoundExceptions may result." These warnings referenced to each subproject and occurs in main project. There is no error messages from Eclipse. In my main project, where I'm importing classes from subprojects, I can right-click on one of the import and choose "Open declaration" and there is valid reference to class from one of my subproject - so it looks like Eclipse sees my dependencies (there is no eclipse errors while building workspace - only these warnings mentioned above) from other projects, but maven doesn't see them while compilation.
Have you got any ideas how can I fix this?
Thanks for help.
You have to declare your dependencies in the pom.xml for Maven. Maven doesn't recognize any Eclipse specific configurations (like Build Path etc.): Maven Tutorial
If you use the m2eclipse plugin, it will configure your Eclipse build path according to your pom.xml configuration
Isn't this maven problem?
Maven needs jar file made from other project inside maven repository.
I don't know much about maven eclipse plugin but so far, in my observation, it seems like it works this way.
So that this case need to build other project so that create it's jar file.
But this takes so much time.

Maven project in eclipse - deployment assembly fails build

Sorry for being verbose...
I have some existing maven projects. I imported them into Eclipse using Maven -> Import existing Maven projects.
This gave me 6 individual projects and one project which lists all 6 under it. (The umbrella project has 6 modules defined in it which translated to 6 eclipse project - as I understand how maven works)
One of the project A requires two other project B and C (at compile time and run-time).
Build path -
On the build path of A I have one of the libraries as Maven dependencies. And it builds fine.
Deployment assembly -
On the deployment assembly of A however there is no entry for deploy to web-inf/lib. Because of that when tomcat starts it doesn't find any required jars or the project B and C.
So I added an entry to deployment assembly as : source - maven And dependencies deploy - web-inf/lib.
Tomcat comes up and my webapp for project A comes up.
This however breaks the eclipse build - upon build eclipse complains:
"Invalid classpath publish export Project entries not supported"
This I see is because, under build path, there is new entry added to web-inf/lib to publish/export under maven-dependencies. Removing that removes maven entry from the deployment assembly as well.
The only workaround is that in the deployment assembly I specify the web-inf/lib under target generated by maven but that causes stale copies of project B and C to be picked up.
Why I need this setting is because when I make changes to project B and C in Eclipse and build them in Eclipse I want to see the changes when I am debugging project A. I don't want to run maven builds on B and C again since its time consuming.
Any help is much appreciated.
I did try including the MAVEN2_CLASSPATH_CONTAINER but that is always empty and when tomcat runs it cannot find the necessary libraries. (BTW what vale is the MAVEN2_CLASSPATH_CONTATINER set to?)
This is on Eclipse Indigo Mac OSX.
You need to add the relevant project/s to your Deployment Assembly.
Right Click Project -> Properties -> Deployment Assembly -> Add
I solved just doing this:
right click on my eclipse project-> Maven -> Update Project Configuration...
Figured it out ..but not completely...
So I added maven dependency in the deployment assembly as I mentioned earlier.
Maven dependency -> WEB-INF/lib
AND additionally added project B and C as well :
B -> WEB-INF/lib and C->WEB-INF/lib
Somehow this way B and C are not added to the publish/export in the java build path. I don't know how eclipse knows to not export B and C from maven repo but to export it from project B and C itself.
Anyways thats for some other day..
for now i m happy :)
For anyone else that searches for this, it seems that you can also manually add the following to the .classpath file
<classpathentry combineaccessrules="false" kind="src" path="/B"/>
By including Maven Dependencies, you are including dependencies for project B and project C. It turns out, you also have to include project B and C manually.
The exact fix which works is.
project A -> properties -> Deployment Assembly
Click Add and then Project. Select B and C. Problem will get fixed after rebuilding.
I had this problem, but I believe none of these answers correctly fix the problem. Instead they workaround m2eclipse (which should manage classpaths and deployment assembly for you). For me, the problem that I was using m2eclipse without m2eclipse-wtp (a separate plugin).
See this SO answer for the plug-in details.
You can add the relevant projects / or maven java build path entry if using maven to your Deployment assembly (project -> properties -> deployment Assembly)
Then checks if theses relevant project are Facets compatible. (project -> Project Facets)
This error can occur if you have multiple entries of same jar in you class path. So you need to check your classpath and remove duplicate entries.