Maven dependency and eclipse classpath - eclipse

I tried to search for existing questions but cant find any - feels like my question is quite simple but probably because it's quite specific I cant find the answers on Stackoverflow / Google.
Anyways - I have few projects with Maven that are depend on each other. In certain cases I want the dependency to be on the JAR rather than a project dependency (ie. I want the dependency to be part of the "Libraries" in Eclipse rather than "Projects" in the Build Path).
Your help is greatly appreciated! Thanks

To get the referenced projects in the same workspace as jar files instead of the projects, we could use the VM parameter -Declipse.useProjectReferences=false or add it in the pom file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<useProjectReferences>false</useProjectReferences>
</configuration>
</plugin>
See this URL for more info.

In your project properties in Eclipse, select Maven. There is a checkbox, 'Resolve dependencies from workspace projects'. If this is checked, then the Eclipse projects are used. Otherwise the jars are used as with other maven dependencies, assuming that you've got the dependencies in your pom.xml as normal.
EDIT: If your project is not a maven project, then you'll have to create the jar outside Eclipse and add it as a jar or external jar as normal. If the project is a maven project, then the above will work.

Say Client-Project depends on Services-Project. If Services-Project generates a JAR. In the Client-Project POM you would express a dependency on this JAR. It would be something like:
<dependency>
<groupId>group.id.of.services.project.goes.here</groupId>
<artifactId>artifact.id.of.services.project.goes.here</artifactId>
<version>version.number.of.services.jar</version>
</dependency>
If services project generates a JAR called com.mycompany.services-1.3.jar, the dependency would be:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>services</artifactId>
<version>1.3</version>
</dependency>

Related

Eclipse plugin with Maven dependencies for third party libraries

I am converting Subclipse to build with Eclipse Tycho and Maven.
Subclipse depends on a few third party JAR files that are not Eclipse plugins so do not exist in any p2 repository. Currently, I just include these in a lib folder within the plugin.
Since these JAR files do exist in Maven, I was hoping that by converting the plugins to build with Maven I could use Maven dependencies. IOW, the plugin would have a pom.xml where I used Maven dependencies to grab and include the third party jar's that have to be included in the plugin. Basically, it would just automate having to refresh what I include in the lib folder of the plugin.
Is this possible? I tried doing what I said above by when I build, I saw no sign that Maven/Tycho was trying to fetch the dependencies. I imagine it is because when the packaging is eclipse-plugin it looks solely at the Eclipse configuration files for the dependency information.
Thanks
To add plain (without OSGi metadata) jar files into your folder at biuild time, you can specify an <execution> of the maven-dependency-plugin to fetch them. However it will require to update your MANIFEST.MF Bundle-Classpath directive whenever a version changes.
It's usually better to hunt for OSGi-able jars or to make an extra effort to package existing libs as OSGi bundles/p2 artifacts like Eclipse Orbit or JBoss Tools Locus do.
Did you try doing the following after adding the dependencies to the pom.xml file?
Project->Clean
Right click on project: Maven->Update dependencies
Right click on project: Maven->Update project configuration
Just adding the plugin to pom dependencies and including the entry <pomDependencies>consider</pomDependencies> in the configuration of target-platform-configuration makes it work.
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho.version}</version>
<configuration>
<!-- The configuration to make tycho consider the maven dependencies -->
<pomDependencies>consider</pomDependencies>
<!-- other configurations -->
</configuartion>
</plugin>
<!-- other plugins-->
</plugins>
<dependencies>
<!-- An example third-party bundle (plugin) present in maven repository-->
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.gogo.shell</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
Reference link here.

eclipse maven external jars unable to compile

Maven compilation failes even after adding the external jars to eclipse. My Eclipse codes are okay with external jars, however when I compile Maven complains package blah blah not found, and I have almost 50 external packages.
I will use the mvn dependency to add the jars later.
However It should work, but not luck.
Any troubleshooting/suggestion please.
I think you can not get away with Maven Project without having <dependencies> tag in your pom.xml (Whether you run it from eclipse or from command-line) . That too for the project which is dependent on classes that are coming from 50 external jars.
If you don't want maven to look for these jars, you have to add below entry in pom.xml with system scope:
<dependencies>
<dependency>
<groupId>selenium-server-standalone</groupId>
<artifactId>selenium-server-standalone</artifactId>
<version>2.46.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/selenium-server-standalone-2.46.0.jar</systemPath>
</dependency>
</dependencies>
With this, groupID & artifactID are meaningless. You can write such 50 <dependency> tags for referring your 50 external jars.

Eclipse searching for the wrong surefire dependency

On building my workspace, my Java 6 Maven project is marked with an error (a Maven problem):
Could not calculate build plan: The repository system is offline but the artifact org.apache.maven.surefire:surefire:pom:2.7.1 is not available in the local repository.
What strikes me as odd is that it is searching for org.apache.maven.surefire:surefire while the true dependency is org.apache.maven.surefire:maven-surefire-plugin.
My effective pom is showing:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.1</version>
<executions>
I'm using Eclipse Indigo with the m2eclipse plugin. And it compiles correctly when running any Maven goal. I tried cleaning the project, reimporting it, clearing the .metadata file.
Where does this behavior come from? Thanks
The mentioned dependency is the parent project for the maven-surefire-plugin and should usually not given directly only via the maven-surefire-plugin itself.
Furthermore What strikes me as odd is that it is searching for org.apache.maven.surefire:surefire while the true dependency is org.apache.maven.surefire:maven-surefire-plugin. which is simply wrong, cause the correct groupId and artifactId for the maven-surefire-plugin is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.2</version>
...
</plugin>
It could be possible having problems while accessing maven central. Apart from the above you should update the maven-surefire-plugin cause the current up-to-date version is 2.15.

Missing Maven dependencies in Eclipse multi-module project

I’m using STS 2.9.1 (build on Eclipse 3.7.2) with m2e plugin bundled with STS (v1.0.200.20111228-1245).
I have a problem regarding missing dependencies in Eclipse project that contains several modules, or maybe I don’t fully understand how it should work.
It’s a maven project.
In my Project > Properties > Java Build Path > Libraries I have “Maven Dependencies” library, but it's empty (and that’s the problem).
The main POM doesn’t have any dependencies, but it has several modules declared in it.
Adding a dependency to module’s POM doesn’t add it to the “Maven Dependencies” library (what was my expectation) and leads to Eclipse showing errors in source files.
Adding a dependency to the main POM adds it to the “MD” lib, but of course I don’t want to add all of my modules’ dependencies to the main POM just to have it in “MD” lib and adding every single dependency to the Build Path doesn’t seem right nor practical.
I’ve tried:
Project > Clean,
Maven > Update dependencies,
Maven > Update project configuration,
Unchecking the checkbox: Project > Properties > Maven > Resolve dependencies from Workspace projects.
None of the above seems to do the trick.
Example:
Simplified project structure:
simple.project
...
sample-module
...
pom.xml
pom.xml
simple.project/pom.xml:
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>simple.project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>sample-module</module>
</modules>
<dependencies>
<dependency><!-- This dependency is present in "MD" lib. -->
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
simple.project/sample-module/pom.xml:
<project ...>
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>simple.project</artifactId>
<groupId>test</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>test</groupId>
<artifactId>sample-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency><!-- I've expected this dependency also to appear in "MD" lib. -->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
It is not supposed to work. A project only imports a dependency from another one if it depends on that project (using dependency) or if it inherits from it (using parent). The module element only represents an aggregation.
The question is from time ago, but I solved this creating a Maven Project and adding Maven Modules: right click on project and "New > Project... > Maven > Maven Module".
After that, no more errors were shown in code.
First thing that I see is that you're defining dependencies in a pom parent. There I would expect to see a <dependencyManagement> (see here the doc) structure. In this way the submodules will inherit properly those common dependencies.
Aside from that lets start for the most simple test. Try to compile your project from the maven utility in the command line. If it works then you have a problem in your Eclipse configuration, otherwise the problem is in the way you have defined your project.
If your project compiles properly from the command line, lets see what else can be happening.
The fact that the Maven Dependencies Library is empty means that the Eclipse Maven plugin is not resolving properly your poms. I had quite bad experiences with the embedded STS maven plugin. Try to downgrade it to the m2e 0.10 version. You only need to open the STS DashBoard / Find Updates / Install m2e 0.10
I hope some of these tips can help you.

Maven: Jar in Referenced Libraries despite not showing in effective POM

Before importing my Maven project, I build it using the following: mvn clean install
I then create the necessary Eclipse files as follows: mvn -Dwtpversion=2.0 eclipse:eclipse
I notice that my Referenced Libraries in Eclipse contains this jar: validation-api-1.0.0.GA.jar
However, when I view the effective POM in Eclipse no such jar appears. Therefore, I am wondering how this jar gets added to my Eclipse classpath?
I require this jar for #Valid annotation I am using and I need Maven to be aware of it. If I build my classpath files using Maven then how come Maven is not aware of it?
Thanks
Check the Dependency hierarchy-tab in your pom (in Eclipse), maybe the jar is a dependency of some other jar you use.
In this case, the reference was in the project's Java Build Path, and was probably added when the project was created because of the -Dwtpversion=2.0 -parameter.
Maven also adds to your class path the sub-dependencies of your main dependencies (which are those specifically declared by you in the pom.xml). Do a
mvn dependency:tree -Dverbose
To see what other dependencies are pulled in with a specific pom-declared dependency.
Also, if you only wanna see the subdependencies of a certain dependency, called x.y.z you can do:
mvn dependency:tree -Dverbose -Dincludes=x.y.z
(where x.y is the groupId and z is the artefactId)
One of your project dependencies probably has a dependency for this jar file. Check the graphical dependency graph or just search for that, find out your project dependency that is dependent on this and exclude this dependency, if possible, by using maven's 'exclude' tags.
You can use maven dependency exclusions, as below:
<project>
...
<dependencies>
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<scope>compile</scope>
<exclusions>
<exclusion> <!-- declare the exclusion here -->
<groupId>sample.ProjectB</groupId>
<artifactId>Project-B</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
Source: Maven - Dependency Exclusions
I have encountered the same problem. And after some research I realized that transitive dependencies of your parent pom may not show up in Effective POM, but would be present in Reference libraries.
The whole confusion raised because when I ran the following command
mvn dependency:tree -Dincludes=X (where X is the group-id of the jar I was looking for)
It did no mention of parent pom, instead it referred to dependency in parent pom which brings X to the table. (Which make sense because we inherit from parent pom).