Multi-module Maven with Profile in Eclipse Doesn't Resolve Dependencies on Children - eclipse

For my setup, I have profiles defined in my ~/.m2/settings.xml, where the profiles define some project-specific repositories. From the command line, I can successfully build the projects using -PprofileHere. However, the profile isn't enabled automatically (i.e. activeByDefault is not set to true for the profile).
In Eclipse 4.5.1 (Mars 1) with m2e 1.6.2.20150902-0002, I import the projects using Import → Maven → Existing Maven Projects. I select the parent project and Eclipse imports them. The children projects are imported, but as expected, their dependencies aren't correctly resolved. To attempt to fix this, I select the parent project, right-click Maven → Select Maven Profiles... and select the correct profile for the parent. I then do the m2e tango by trying various combinations of the following:
Cleaning and rebuilding the project (Project → Clean...)
Updating the parent project (Right-click, Maven → Update Project...)
Updating the children projects (ditto)
Restarting Eclipse
Eclipse continuously shows the dependencies unresolved on the children projects when viewing the children pom.xml's or opening a class in a child module with unresolved dependencies. The only workaround I've found is by enabling the profile automatically in my ~/.m2/settings.xml. Then and only then does Eclipse properly resolve the Maven dependencies.
What am I doing wrong when originally importing / configuring projects in Eclipse?

How it worked for me in a sample project providing the same scenario: make Eclipse use a profile by default for a Maven build.
Right click on the concerned project > Properties > Maven
Type your profile there, it will be used by Eclipse during the build
I never used this option, but apparently its purpose should suit your need.

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.

Maven dependency issue in eclipse

I have multiple related projects open in a workspace in eclipse. One of the projects has a maven dependency on the output jar from another project. In this project, any references to the classes in the dependency always show up as red (unidentified) and I get no code completion or javadoc showing up for them. If I manually add the jar to the classpath, then I no longer have this problem. The thing is, I don't want to have to manually add the jar every time our revision changes, that's what Maven is for. Any ideas on how I can resolve this? it seems like a bug to me, but I'm not sure...
The correct way to do this is the following:
Make sure that m2e or m2eclipse (depending on your version of Eclipse) is installed: http://www.eclipse.org/m2e/
Use the dependencies section in the POM file exclusively, don't fiddle with the Eclipse project references. Right-click the project, then select Maven > Update Project Configuration to reset the project to the Maven default settings. This way, m2e has ownership of the dependencies.
If you don't have that option, you might need to enable the project for Maven usage. Right-click the project, then Maven > Enable Workspace Resolution or Maven > Enable Dependency Management.
Make sure all referenced projects are open in Eclipse and have the Maven nature enabled.
Check the Maven settings for each project, make sure that groupId, artifactId and version match with the projects you have open in Eclipse. So if the project you depend on has version 1.0.0-SNAPSHOT in Eclipse, make sure that the depending project's POM file references version 1.0.0-SNAPSHOT in the dependencies section.
Enable Workspace Resolution for each of the projects. Right-click the project, then Maven > Enable Workspace Resolution.
Finally, if the projects are still not resolved, right-click the project again, then Maven > Update Dependencies
This should solve your problem.
I had the same issue and resolved the problem by right-clicking on the project and selecting Maven -> Disable Workspace Resolution from the context menu. I had already tried updating the project from the POM file as described above.
In order to fix this typically you can copy the Eclipse .classpath file from a working project since there isn’t anything project specific here assuming you’re structured as a standard Maven project. This will tell Eclipse to allow Maven to manage the dependencies and build. There seems to be no easy way to do this from the Eclipse UI.

m2eclipse not finding maven dependencies, artifacts not found

I'm using m2eclipse as my maven 2 plugin for eclipse. I'm brand new to maven, so my mistake might be simple, although searching has not yielded any solutions for me. I can run maven from the command line and it build successfully. However if I import as an existing maven project, or use mvn eclipse:eclipse and then import I get the artifacts not found in my POM file resulting in no maven dependencies being loaded at all.
What's odd is that I have 2 projects, both children to a parent pom packaged project. One child loads all my maven dependencies while the other cannot find any and says my pom file is missing artifacts. Again, I can install and package them all just fine from the command line.
I even tried loading my projects into netbeans which worked flawlessly, however I am required to use eclipse.
Any ideas on how to fix this?
Update
Directory structure, pom files layout:
--main
--pom.xml (is a pom package that is the parent to two other projects)
--ProjectA
--pom.xml
--ProjectB
--pom.xml
Main is the parent project of both ProjectA and ProjectB. ProjectB has ProjectA as a dependency. Apprently m2eclipse is not happy about that depedency. As soon as I remove the dependency of ProjectA from ProjectB's pom file all the maven dependencies are found by eclipse.
- com.company.myproj:app:6.6.0:jar Missing: ---------- 1) com.company.myproj:main:pom:${myproj.version} ---------- 1 required artifact is missing. for artifact: com.company.myproj:main:pom:$
{myproj.version} from the specified remote repositories: apache-incubating (http://people.apache.org/repo/m2-incubating-repository/, releases=true, snapshots=true), central (http://
repo1.maven.org/maven2, releases=true, snapshots=false)
However, I still need ProjectB to have that dependency for ProjectA. The error message seems to be m2eclipse looking for ProjectA in a remote repo. Project A should only be in .m2 directory, but I'm not sure how to tell m2eclipse to look there.
I think this may be just a m2eclipse specific issue because using mvn from the command line works fine and like I said previously, netbeans loads the projects and dependencies just fine.
I had this issue for dependencies that were created in other projects. Downloaded thirdparty dependencies showed up fine in the build path, but not a library that I had created.
SOLUTION: In the project that is not building correctly, right-click on the project and choose Properties, and then Maven. Uncheck the box labeled "Resolve dependencies from Workspace projects", hit Apply, and then OK. Right-click again on your project and do a Maven->Update Snapshots (or Update Dependencies) and your errors should go away when your project rebuilds (automatically if you have auto-build enabled).
It sounds like your m2eclipse install is using the embedded Maven, which has its own repository (located under user home) and settings.
If you open up the Maven preferences (Window->Preferences->Maven->Installations, you can add your Maven installation by selecting Add... then browsing to the M2_HOME directory.
(source: sonatype.com)
For more details see the m2eclipse book
For me maven was downloading the dependency but was unable to add it to the classpath. I saw my .classpath of the project,it didnt have any maven-related entry. When I added
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
the issue got resolved for me.
One of the reason I found was why it doesn't find a jar from repository might be because the .pom file for that particular jar might be missing or corrupt. Just correct it and try to load from local repository.
Okay I fixed this thing. Had to first convert the projects to Maven Projects, then remove them from the Eclipse workspace, and then re-import them.
I had problems with using m2eclipse (i.e. it did not appear to be installed at all) but I develop a project using IAM - maven plugin for eclipse supported by Eclipse Foundation (or hosted or something like that).
I had sometimes problems as sometimes some strange error appeared for project (it couldn't move something) but simple command (run from eclipse as task or from console) + refresh (F5) solved all problems:
mvn clean
However please note that I created project in eclipse. However I modified pom.xml by hand.
This could be a problem if you are using a custom 'Settings.xml', with a different <localRepository> configured in it.
Eclipse will be using the default installation of MAVEN, and will be using the default location for the User to look for the local Maven repository, which on Linux systems would be '/home/${USER}/.m2/'
Eclipse can be easily configured to use the customized 'Settings.xml', by doing the following:
Goto -> Window -> Preferences -> Select 'Maven' -> Select 'User Settings'
Under 'User Settings', select the custom 'Settings.xml' file, for 'User Settings' by clicking 'Browse' and selecting the customized 'Settings.xml'.
Click on 'Update Settings', if the 'Local Repository' Textbox does not show the custom location from the file above, just key in the location and click 'Reindex'.
Click 'OK'
After this, you could proceed to select your project from the 'Project Explorer', right click, Select 'Maven' > 'Update Project'. Make sure that your project is selected (ticked) in the Window, and click 'OK'.
This should help to resolve the issue, if using custom 'Settings.xml' for Maven.
Hope it helps.