How to add maven repository jars to eclipse buildpath? - eclipse

From eclipse I can see all the necessary jars in maven repositories view. But I have around 100 errors for the missing jar files. So I have set M2_REPO environment variable. I have ran the mvn eclipse:eclipse command from command line. When I run this command an ear package is added to one of subpackages of my project. For example my project name is portal. And sub package is portal_ear. The ear after running mvn eclipse:eclipse command is added to target folder of portal_ear. So my question is as I have the jars already in the repository, how can I add those to the portal buildpath inside eclipse?
Thanks.

When you're using M2Eclipse, all you need to do is to make sure your Eclipse project is a Maven project. Select the project, click on "Convert > Convert to Maven project" or "Maven > Enable dependency management".
Do not manually add libraries. Also, the M2_REPO library variable is no longer used with m2eclipse, neither is to run the "mvn eclipse:eclipse" command. That is legacy you don't need to do any more.
You can also import your project by using "File > Import > Maven > Existing Maven project" and M2Eclipse will correctly set up the project's build path.
If you have custom Maven settings, such as an Enterprise Repository, you may want to tell M2Eclipse where to find your settings.xml file, if you do not have it in your user home but in the Maven installation (people do this sometimes). Go to "Window > Preferences > Maven > User Settings" and choose the right settings.xml.

Install m2e from the eclipse marketplace. You can now import existing maven projects into your workspace or convert existing ones.
Works very well for Eclipse 3.7

In my case, I had an existing project that was deployed in Eclipse fine. Thank you Mkyong: http://www.mkyong.com/spring-security/spring-security-hello-world-example/
But when I needed to add JSTL for Logout tag support, I didn't see how to add the the jstl.jar file that was just pulled into my repo as another M2_REPO/xyz configuration. Instead, I could only import it as you would a regular .jar file - and that didn't seem right to me.
After reading a bunch of posts without success, I decided to see if I could get around the Eclipse UI and update the project configuration manually. From the project I opened .classpath in a text editor and added a new entry for the jar file:
<classpathentry kind="var" path="M2_REPO/javax/servlet/jstl/1.2/jstl-1.2.jar" sourcepath="M2_REPO/javax/servlet/jstl/1.2/jstl-1.2.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
Then I just refreshed my project in Eclipse (no restart required) and ...
the JSTL build error I was having in my JSP page went away
the build path entry for JSTL was appearing with the same M2_REPO as the other entries
the deployed code worked fine in Tomcat
Yes a bit of a hack, but still relatively clean I think - cheers

Right click on the project --> Maven --> Update Project...
For me it worked simply like that

Related

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.

Wrestling with Eclipse-modified .classpath in a Mixed Windows/Mac Team Environment

I'm using Eclipse on a small development environment where one developer has a Windows PC and the other has a MacBook. We share code using a subversion repository, and use Maven as an "External Tool" (not using the m2e plugin).
We each have an M2_REPO variable that has been working great for resolving paths to .jar files on our two systems. Recently, however, I ran a command to tell Maven to pull down sources and javadocs.
mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true
Since then, I see some very Windows-centric references in my .classpath that are a nuissance to my Mac-using friend.
<classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-api/1.6.4/slf4j-api-1.6.4.jar"
sourcepath="M2_REPO/org/slf4j/slf4j-api/1.6.4/slf4j-api-1.6.4-sources.jar">
<attributes>
<attribute value="jar:file:/C:/Users/John/.m2/repository/org/slf4j/slf4j-api/1.6.4/slf4j-api-1.6.4-javadoc.jar!/" name="javadoc_location"/>
</attributes>
</classpathentry>
Even after having removed the downloadSources and downloadJavadocs parameters, and verified that they're false in my plugin configuration for maven-eclipse-plugin, deleted my .classpath file, and reran mvn eclipse:eclipse I still see those unwanted references placed into my .classpath file.
Has anyone found a good working solution to this problem? Have I overlooked some setting that tells Maven to use the M2_REPO variable in its references to javadoc files?
On my team we don't commit Eclipse .settings or .classpath files into source control. When we initially check out projects we use m2e to build the classpath (did it with eclipse goal as you are once upon a time) and everything is set up correctly no matter what platform the developer is using. We have people using Win 7, Macs, and various Linux flavors and the build machine is Linux, no issues I know of anywhere.
I don't use maven, I am the only developer and I have a Windows PC and a Mac. For me this "small" solution works fine. Create a symbolik link so your paths on both systems match:
C:\> mklink /d "C:\Users\xxx\android-sdks" "C:\Users\xxx\AppData\Local\Android\android-sdk"
I did this on the windows machine. The command is mklink /d (for directory) newDir existingDir. Works on Windows 7 at least. Maybe this will help.
Try enabling the dependency management of your maven project by:
Right Click on the project –> Maven –> Enable Dependency Management
After this delete and recreate your .classpath file.
Rather than use 'mvn eclipse:eclipse', install M2Eclipse either from the project site or through the Eclipse Marketplace. It will handle all the linkups for you, and not embed absolute paths.
(I always version .classpath and .settings/)
If you have javadocs in your repository, the maven eclipse plugin will always add them to your classpath.
The -DdownloadJavadocs=true option only tells the eclipse plugin to download them. It does not tell the plugin to include them or not in your classpath. If you do not want them in your classpath anymore, you have to remove all -javadoc. from your local repository (or delete your locate repository).

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.

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.