Using a local dependency in Eclipse with Ivy / Ant - eclipse

I have an ivy.xml file with remote dependencies to a library I own, for example
<dependency org="myorg" name="myProjectLib" rev="default" conf="default"/>
In Eclipse , if I have the myProjectLib project in my workspace, and want to depend on that version, instead of the remote one I have to
1) go to the project Properties -> Java Build Path -> Projects ->Required Projects on BuildPath and add the local project dependency there and
2) comment out the remote project dependency in my ivy.xml file, in order to actually be using the local dependency when running the project locally .
<!-- <dependency org="myorg" name="myProjectLib" rev="default" conf="default"/> -->
This seems like a step too many for me . I don't have this issue in IntelliJ for example. In that, I can just set it to depend on the local project and do not have to change the ivy file. I often run into dependency issues in Eclipse by commenting out the remote dependency. Any ideas/ suggestions on another way to do this or how to simplify this workflow?

When adding a project as a dependency manually, you should also consider the order of the classpath entries. If the IvyDE classpath container is before the dependent project, then the resolved jars by Ivy will take precedence over the projects in Eclipse. If you move manually the project dependency to be upper than the IvyDE classpath container, then you should see the expected behavior.
Then regarding an even more simpler configuration, you can configure the IvyDE classpath container to lookup for dependencies directly in the workspace. Both of your projects should have their dependencies managed by Ivy in Eclipse. And then see "Resolve dependencies in workspace" in the settings of the IvyDE classpath container.
More info about that feature can be found in the doc: https://ant.apache.org/ivy/ivyde/history/latest-milestone/cpc/workspace.html

Related

Eclipse - Add build path vs Add maven dependency. Any differences?

I have added a Java project dependency in Maven dependencies however if I add the same dependency in build path it works. What is the difference between these 2 methods?
Adding your project as maven dependency would mean that
You don't have to bundle your jar with your project.
You don't check in your .classpath and .project eclipse meta files in your source control
Any other programmer in your organization will be able to build your application.
You should not manually add jar file in eclipse build path. Its an anti pattern.
Both works because eventually your application need classpath which is set both by adding dependency manually as well as adding in build path.

Eclipse adding the project overrides maven dependency

I have a maven project that uses other small projects. And I added them to master's pom file.
So my Eclipse project properties looks like following:
>Master project
>Java Resources
>Libraries
>JRE System Library
>Maven dependencies
>smallProj1.jar
>smallProj2.jar
The thing is I do not have the all code for smallProj1 and smallProj2 and whenever add them to my workspace Eclipse overrides the maven dependencies and simply calls the two projects instead the jar files from my local maven repository and then I have all over the code those red underlines. Project properties in Eclipse looks as following after I add them to my workspace:
>Master project
>smallProj1
>smallProj2
>Java Resources
>Libraries
>JRE System Library
>Maven dependencies
>smallProj1 (not jar anymore - folder)
>smallProj2 (not jar anymore - folder)
Since I also want to have a look to smallProj1 and smallProj2 code while I am coding, I want to keep them both in my workspace. But I want my Master project to use the jar files that I provided, not the small projects in the workspace.
Any ideas on that?
You could right click on a project and set "Maven" -> "Disable Workspace Resolution". This will let Maven find the dependencies from Maven and not within the workspace. I don't think it is possible to do this on a dependency basis.
But I think in your case it would be better to fix the broken smallProjX in your workspace (as they exactly contain the code you need), so that the master project could be build correctly.

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.

How to mavenize a standard eclipse project but only for some dependencies

I have an existing eclipse project with standard dependencies in the classpath. I need to add a new dependency through maven but the rest of the dependencies need to stay there as jars bundled with the project.
I am using m2e, if I right click on the project --> maven --> enable maven, a pom file is created. At this stage I can add my new dependency through the pom file but all the existing dependencies seem to be gone from the classpath.
Hence the question: Is there a way to create a hybrid project that has both standard dependencies and maven dependencies?
You could add your legacy dependencies by using the scope "system" (and the tag "systemPath") ?
See http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html.
I don't think you should even attempt to create such a thing, and I don't know why you would want to.
If you want to use Maven, then you should define all your dependencies in your pom so that a maven build will actually work. If all your dependencies aren't defined, how do you expect maven to build your project?
You need to decide which means of dependency resolution you want to use instead of trying to mix two together.

Eclipse m2eclipse getting dependencies from local repository

I have dependencies installed local on my machine (~/.m2/repository/blah/blah/blah) and m2eclipse is not recognizing them as there - I think m2eclipse is using its own maven instance. Is there any way to force m2eclipse to use the local maven installation in osx?
I had the same problem, and this is what worked for me. Using Eclipse Indigo:
Windows->Show View->Other
In the pop-up:
Maven->Maven Repositories
Once the view is displayed, right click on the appropriate repo, and select "Rebuild Index"
Wait a few seconds and done!
I have dependencies installed local on my machine (~/.m2/repository/blah/blah/blah) and m2eclipse is not recognizing them as there.
Do they have good metadata? How did you install them? Because m2eclipse definitely uses your local repository for dependency resolution.
I think m2eclipse is using its own maven instance. Is there any way to force m2eclipse to use the local maven installation in osx?
Yes, m2eclipse uses its own version of Maven by default and you can add an external installation via Window > Preferences > Maven > Installations but this won't change anything to the local repository used by one or the other that you can configure via Window > Preferences > Maven > User Settings as shown below:
alt text http://www.imagebanana.com/img/w5y2vevt/screenshot_008.png
This send us back to the questions above: how did you "install" the problematic dependencies?
You can change the used Maven instance in Windows->Preferences->Maven->Installations. But I'm not sure whether this will help as a comment below that setting says that dependency resolution will still be done with the internal Maven installation.
If I were you, I would try to add that local repository to the list of known repositories. Use Window->Show View->Maven repositories (this is sadly not visible in the preferences).
Check that the jar was installed properly. Maven does not give an error when you provide an incorrect path to the jar when installing to local repository. The jgravatar.jar was not in my ~ directory when I ran the command below.
mvn install:install-file -Dfile=~/jgravatar.jar -DgroupId=jgravatar -DartifactId=jgravatar -Dversion=06292012 -Dpackaging=jar
[INFO] Installing /Users/steve/~/jgravatar.jar to /Users/steve/.m2/repository/jgravatar/jgravatar/06292012/jgravatar-06292012.jar
[INFO] Installing /var/folders/gz/gjyqtkzj3ys8lpmh_38qvmn00000gq/T/mvninstall2662938607942511865.pom to /Users/steve/.m2/repository/jgravatar/jgravatar/06292012/jgravatar-06292012.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
I was getting the exact same issue. I added the JARs to the local repository via the install-file command. I verified they were installed. I rebuilt the index. I went to the directory and inspected the JAR file using 7Zip, but I could not get it to appear in the Maven Dependencies inside of my eclipse project.
For me the solution was pretty silly. When I added the dependency into the pom.xml using the dependencies editor, Maven threw in a packaging stanza into the XML. So the XML entry looked like:
<dependency>
<groupId>slf4j</groupId>
<artifactId>jdk14</artifactId>
<version>1.5.8</version>
<type>JAR</type>
</dependency>
When I removed the last stanza "JAR" and made the entry:
<dependency>
<groupId>slf4j</groupId>
<artifactId>jdk14</artifactId>
<version>1.5.8</version>
</dependency>
it all worked fine.