maven dependencies to a parent project build path in eclipse - eclipse

I am using m2e. So I have two projects. When I right click on them both have maven enabled. So one of the projects is a parent project so it has many sub packages each sub package is correctly src\target, but not the parent itself. I have pom.xml for parent and all the children.
When I right click on these two projects then maven then update configuration. The maven repository gets added to the simple project but not to the one which is a parent of the rest. So what should I do in this case?

You have to perform a mvn install for the artifacts to get installed in the repository, the "update project configuration" is similar to the mvn eclipse:eclipse goal of the maven eclipse plugin.

Related

Eclipse project facets in Maven projects

I'm starting to work with Maven in Eclipse but when I create a Maven project with several children projects, let's say:
-Maven project
-Web project
-EJB project
-JSF project
Each of the children projects has a different set of facets in order for Eclipse enable the corresponding tooling.
The problem is that, when I import the Maven project, the children projects exists in the workspace (under the Maven project) as simple folders, so, no specific-tooling is available.
What is the correct way to work with Maven in Eclipse?
You need to "Import...>Existing Maven project". The import wizard will recursively discover all child modules (assuming they all have a pom.xml) and import them properly as separate Eclipse projects

Maven eclipse - mutli project dependencies not appearing

I have a bunch of maven projects which all are part of a parent project. When I import the parent project into eclipse, I was expecting the child projects source folders to be listed in my parent project, however they are not. They also don't appear in the "Maven Dependencies" section.
Is this the correct behavior or am I missing something. If it is the correct behavior, then what is the best practice around doing this?
Should I be importing each project into eclipse and then editing the build path for my parent project to include them all? I really don't like this approach because I want to do everything through maven.
Note: I am not using mvn eclipse:eclipse to generate my project files, I am using the maven plugin directly in eclipse IDE as: Import existing maven projects
Thanks!
Import your parent project then close it (right click on the project in the package explorer > close project). Select it and go to file > import > maven > existing maven projets here you should see the list of the child projects, select the wanted ones then finish.
Now in your package explorer you should have :
A project parent-project where you work on the pom-parent and eventually on the src-parent
One project per child-project where you work on the corresponding pom-child and src-child
If some dependencies are missing try to update the projects (right click on the projects in the package explorer maven > update project)
Normally you don't have to edit the build path in eclipse it must be handle by the maven nature of the project (the maven nature is often represent by a M on the icons of the projects)
Normally, if you don't import "dependencies" projects, Eclipse simply uses the jars presents in your local repo (.m2).
However, I don't get what you mean by "I have a bunch of maven projects which all are part of a parent project".
Projects are not part of each other. And the child projects sources should not be listed in the sources folders. You only uses the bytecode.
If you want to "see all your sources" :
However, you can have a complete "view" of the code if you export your sources as sources jars.
If you want to be able to debug across your projects :
You then have to import all your projects in Eclipse. If a project is closed, Eclipse will use the jar like if the project was not there. If the project is open, it will use the project (not sure exactly how, but it works smoothly).

Tell eclipse to use Source and not Jar

I have a maven project that depends on a jar (say A). When running the project, it uses the jar under Maven dependencies.
I have the source of A with me and its opened as a project in Eclipse. When I run the project I need to use the source and not the jar. How do I tell Eclipse to use the source and not the jar?
m2eclipse does this by default, if that is a full Maven project. See the context menu in the screen shot of http://www.sonatype.com/books/m2eclipse-book/reference/eclipse.html#eclipse-sect-resolving-dependencies, where it says "Disable workspace resolution".
If you don't find that context menu entry for your project, then you probably have a Java project with a POM file, which is not marked as Maven project type in Eclipse. Use the context menu Configure->Convert to Maven project in that case.
I had problems with the workspace resolution of m2eclipse as well. It would recognize the dependency (A) and show a different icon in the dependency view (which indicates that the dependency is resolved from the workspace) but the classes were not found.
Only when I ran a mvn install on project A all artifacts were generated and the resolution started to work. Maybe that helps.
Also I found a couple of hints (related to not working workspace resulotion) that suggested to (re-)import the project again using "Existing Maven Projects" wizard.

Is any method to using "Workspace project" maven repository?

We have: Eclipse 3.6, Maven (M2Eclipse), Two Project (ProjectA and ProjectB) with pom.xml.
ProjectB depends of ProjectA (using maven).
If we install ProjectA to local repository (maven install), ProjectB resolve dependencies OK.
If we not install ProjectA to local repository. In Maven POM Editor we add ProjectA as dependency of ProjectB (M2Eclipse using "workspace project" repository to resolve this dependency).
OK, added. But if we do "maven package" or others plugins goals... dependency not resolved.
And we have somethings like that:
Downloading: http://repo1.maven.org/maven2/Artifact/ProjectA/maven-metadata.xml
and of course it cannot be resolved.
Now is the Question. How I can use "Workspace project" when build ProjectB?
P.S. Why I'm not using "maven install"? OK, it is 100% method, but when I do many small changes in several it is too long do excess operations.
P.S.S. "Workspace project" we can see Window -> Show View -> Maven Repositories.
I think the idea of running the 'Maven package' or similar goals from Eclipse is to do whatever maven would have done if run from the commandline for example. The scenario that you are giving wouldn't have worked on the commandline either.
If project B depends on project A (via pom.xml) that means that project A should be available in a repository. Whether that's local (via mvn install) or remote via (mvn deploy) doesn't matter, but mvn package isn't enough. That will just put a package in the /target folder.
The only exception would be if both projects reside in the same reactor I suppose, that is both projects are modules of some other 'parent' project. In that case triggering maven targets on one of the submodules might be able to resolve the dependencies okay. Not 100% sure though.
Back to your question: "Now is the Question. How I can use "Workspace project" when build ProjectB?". No, I don't think so.
Workspace dependency should be resolved. Try this,
run > run configurations > Maven Build > new > Select Project B > check "resolve Workspace Artifact" > set goals as package
select offline check-box as well.
Perhaps, you need to provide exact version of projectA in projectB dependency tag. Else, it may look to Maven public repository to pull the latest.
Hope this helps.
Try this (in ProjectB):
Maven > Enable Workspace Resolution
A similar issue was solved here in that way.

Problem with Eclipse and a Maven multi-module project

I have created a Maven project with the following structure:
+ root-project
pom.xml (pom)
+ sub-projectA (jar)
+ sub-projectB (jar)
I have done the following steps:
mvn archetype:create –DgroupId=my.group.id –DartifactId=root-project
mvn archetype:create –DgroupId=my.group.id –DartifactId=sub-projectA
mvn archetype:create –DgroupId=my.group.id –DartifactId=sub-projectB
So I have, obviously, in the top-level pom.xml the following elements:
<modules>
<module>sub-projectA</module>
<module>sub-projectB</module>
</modules>
The last step was:
mvn eclipse:clean eclipse:eclipse
Now if I import the root-project in Eclipse, it seems to look at my projects as resources and not like java projects.
However if I import each of child projects sub-projectA and sub-projectB, it looks them like java projects.
This is a big problem for me because I have a deeper hierarchy. Any help would be appreciated!
The maven eclipse plugin does NOT support nested projects so you can't import the root-project and the nested modules (unless you move to a flat layout).
The alternative is to use m2eclipse (and you won't have any problems with a maven-typical nested tree). This is the preferred approach nowadays.
Please follow these simple step's to configure project
Download project from maven project
Inside ch-multi-spring->simple-parent
convert simple-parent to eclipse project using this command
mvn eclipse:eclipse
Import sample-parent in eclipse.
For runing this project
right click on simple-webapp->properties->deployment assemblly->project->
Then add all the dependent project.
Now you can run this project.
I had a similar issue, and the following is what I did to solve it...
I had the parent project open as well as the child projects. The child projects would build fine and not show any problems, however the parent showed the child projects as "resource folders" and they showed as having problems.
Solution was to right click on each of the child projects in the parent project -> Properties -> Resource -> Check "derived"
After you do this, clean/rebuild and you should be good.