How to add Maven dependencies in Eclipse libaries - eclipse

Can any one explain how to add Maven dependency JAR i.e. build path libraries? To be very specific how to include JAR file mentioned in pom.xml to Java build path? Maven dependencies - unable to see jars over here.
Don't have a option to update Maven dependencies. Also executed the script mvn eclipse:eclipse, seen on StackOverflow.

Generally, you should not have any need to deal with such a task manually. Here is the correct procedure for importing of maven project to your Eclipse IDE:
Make sure you have m2eclipse plugin installed (Maven support for Eclipse). As far I remember it is included to Eclipse IDE for JEE developers by default.
Checkout your maven project to any directory
From this directory (root pom.xml is located in), run: mvn eclipse:eclipse
Go to Eclipse, File -> Import, select "Existing maven project"
Select directory where your root pom.xml is located, Eclipse should find all maven modules.
Optional: if eclipse will tell you that it can't recognize some maven plugins - ignore this, just continue.
If you follow this instruction, IDE will create all maven modules as eclipse projects and you will have an option to update maven dependencies and also willsee all of them rigth in IDE.

Related

Connect project via local maven (in Eclipse)

In Eclipse, I can create dependency project and refer it from Projects tab from Java Build Path dialog.
May I substitute this with something "mavenish"?
For example, may be I can post maven project to local repository and then refer it by maven coordinates as usually in pom.xml?
Yes you could install it to your local maven repository (mvn install). But it also works if you just have the project in your workspace you don't even have to refer it from Java Build Path as you've mentioned. Just add it as a dependency in your pom and the maven plugin in eclipse will use the dependency from your workspace.

How is the maven build different from eclipse build?

I have integrated m2e plugin.When i change the java file .It invokes building workspace. Is this is eclipse build or maven build.
And what exactly the difference between Eclipse clean and mvn clean ?
The m2e plugin injects the Maven classpath into the Eclipse project plus it disables the default Eclipse resource copying so Maven can do it's magic (the Maven resource copy step can filter/transform resources).
The actual build for Java files uses the Eclipse compiler with the classpath supplied from m2e.
Clean: Unless you have configured something special, Eclipse clean will delete target/classes and target/test-classes while mvn clean will delete the whole target/ folder.
Also, Eclipse will build the project again right after clean. For Maven, you need to issue another command (mvn compile or mvn install).
Check your .project file.
Do you have something like org.eclipse.m2e.core.maven2Builder there? Then it's Maven build executed by Eclipse via m2e.
The exact difference between "Eclipse clean" and mvn clean...
mvn clean executes Maven clean phase, as it is configured (or inherited) in the pom.xml.
For Eclipse clean, see this question:
Function of Project > Clean in Eclipse

How eclipse maven and ant work together?

I made simple maven project and I opened it with Eclipse. I have installed maven plugin for Eclipse. I'm interested in following:
How Eclipse compiles code when I hit save on my source code (does it use configuration from ant or maven or something else)?
When I run tests from JUnit plugin for Eclipse those Eclipse calls mvn test (I suppose not, but what is then happening exactly)?
Is it possible that maven does the build successfully but Eclipse is
showing errors in code?
The Maven Integration for Eclipse makes it easier to edit POM files, allows you to execute maven builds from within Eclipse and to help with dependency management. It doesn't actually compile your code (unless of course you execute a maven build from within Eclipse). The main help is with the dependency management and writing the .classpath file of your project within Eclipse.
To try and answer your questions:
Eclipse uses its standard mechanism to compile code. With a standard eclipse for java developers your project will have a Java Project nature and Eclipse will then use the Java Development Tools - JDT to compile the code. (Internally this uses an incremental builder to build the code http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2FresAdv_builders.htm). What source files it will compile and where it will place the resultant .class files is configured in your project's Java Build Path (which I am guessing the maven plugin may well configure for you)
JUnit support is part of the Java Development Tools as well.
It is possible that maven will successfully build a project outside of Eclipse, but that the same project will show errors within Eclipse. This is usually down to classpath errors (dependencies defined in the project's POM not being added to the classpath in Eclipse). If you are using the maven plugin with eclipse this probably shouldn't happen. If you are not using the maven plugin within eclipse you can execute maven eclipse:eclipse to have maven update the Eclipse .classpath file of the project which should then fix any of these problems.

Eclipse Indigo with m2e can't find maven dependencies in a multi-module project

I've created a multi-module project using Indigo with m2e 1.0. One of the child modules has a dependency on the other. It all builds correctly under maven.
Eclipse, however, can't find any of the classes that this module uses from the dependency .jar. The project properties shows the artifact under Maven Dependencies, but it does not show the actual .jar file itself.
I added the dependency with the Maven menu for this project.
The .project and .classpath got generated automagically at some point. I did not have to run mvn eclipse:eclipse or mvn eclipse:m2e (or whatever the goal is for m2e). The .classpath doesn't have the dependent .jar in it, but it does have org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER.
I've tried all the options under the Maven menu (update dependencies, update configuration) and refreshing the project. I've closed and re-opened Eclipse. It still shows errors.
This makes Eclipse worthless as a Java editor in multi-module projects. I could manually add the dependency .jar in the .classpath, but this defeats the purpose of integration Eclipse and Maven with m2e.
Is there any solution for this in Eclipse?
Thanks.
The answer turned out to be the last answer to this question given by Jody Box. It's pretty bizzare that in order for Eclipse to resolve dependencies from another project in the Workspace that you have to uncheck the "Resolve dependencies from Workspace projects" checkbox.
I know mvn can be convoluted but this checkbox is doing the opposite of what it says.

why do i have to manually download the jars in maven2 repository when m2e already downloaded the pom and sha1

I've installed elcipse 3.6.2 and m2e plugin
M2E - Maven Integration for Eclipse (Incubation) 0.13.0.201105261543 org.eclipse.m2e.feature.feature.group.
I have imported an existing maven pom project. I right clicked on the project then went to
Debug As > Maven Install.
It downloads most of the things but leaves out a lot of jar even though it's corresponding .pom and .sha2 are downloaded. So it leaves me with error messages like missing .jar.
So i had to download it manually from the ibiblio site.
Is there something wrong with my configuration?
BTW, the imported projects are from jetty 8.0.3M.
I don't think their pom needs changing.
I suppose it's in the eclipse/m2e plugin config?
This has worked for me almost always:
Delete your .pom and .sha2 files for the jar that is missing.
From Eclipse run the menu command "Update All Maven Dependencies" under Project.
This seems to download what is missing.
Hope that helps.