-1

May be it a pretty basic question as I am starting to learn Maven.

Usually, we copy jar files to lib folder under WEB-INF folder. Eclipse uses those libraries to help during coding, like automatically importing the necessary packages, showing the class elements when you press . (dot), and auto-completing with Ctrl+Space, etc.

With maven, the jars are not available in the project until we build/compile the project. That is when, it downloads all jar files. I am not sure if m2eclipse is of any use here.

Any idea how it works? Googling did not give me any info.

Peter Mularien
  • 2,578
  • 1
  • 25
  • 34
Kevin Rave
  • 13,876
  • 35
  • 109
  • 173
  • This really isn't a clear question... if you're looking to understand how Maven-based dependencies are managed in Eclipse, this is clearly documented in the available Eclipse plugins for Maven (m2e or m2eclipse). As long as you've configured these properly, all the standard Eclipse functionality should work as you expect. Please rephrase in the form of a question or repost a question if it's something other than "how do I use the plugin". – Peter Mularien May 07 '13 at 15:29
  • I am sorry if my question was not clear. I chose the answer that answers my question. I clarified it further with comments. I guess that is what I need. Basically, how eclipse context-help works without jars in the path. Because maven downloads them only when we build/compile the projects. Meaning, jars are not available to eclipse, until we do a build/compile. – Kevin Rave May 07 '13 at 15:35
  • By "context help", do you mean auto-complete functionality, or do you mean context-sensitive javadoc (for example, `F2`)? – Peter Mularien May 07 '13 at 16:57
  • Sorry again. auto-complete. auto-import, etc. – Kevin Rave May 07 '13 at 17:04
  • possible duplicate of [How to configure Eclipse build path to use Maven dependencies?](http://stackoverflow.com/questions/2037188/how-to-configure-eclipse-build-path-to-use-maven-dependencies) – Paulo Scardine May 07 '13 at 22:28

3 Answers3

1

Eclipse is able to load the classes which are there in the project classpath. In non maven projects we add the libraries in project under Java Build Path manually, which gets added to the classpath. In case of maven projects, when we add dependencies in pom, eclipse add the jars in the classpath from M2_HOME directory. As soon as you save your pom, dependencies are downloaded in your M2_HOME directory and also added to your classpath.

The goal of the Eclipse m2eclipse project is to provide Apache Maven support in the Eclipse IDE, making it easier to edit Maven's pom.xml, run a build from the IDE and much more.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • So when we edit and add dependency to POM, the jars are downloaded immediately and automatically without compile/build, and eclipse uses these libraries from maven repo to do the context help? – Kevin Rave May 07 '13 at 14:49
  • I believe so in case you have build automatically enabled in eclipse. If not then when you build it then jars will surely be downloaded. m2eclipse enables you to build the project from eclipse with an easy interface to provide the build params, otherwise you will have to do it form the command line always. – Juned Ahsan May 07 '13 at 14:55
  • But then how does eclipse help with the coding like I mentioned above? Do we need to build the project before we start coding, so eclipse can download all the jars? – Kevin Rave May 07 '13 at 14:58
  • Maybe, simply add the dependencies and do a clean build once. You should have all the required jars with you for coding. Even when you don't use maven, you need to add the jars in your build path before coding. – Juned Ahsan May 07 '13 at 14:59
  • Yes, I understand we need to add jars before we start coding. But I had this question, because Maven does the downloading for us. So I was wondering how it works without jars upfront. – Kevin Rave May 07 '13 at 15:03
  • I believe you mean 'work upfront' wihtout adding any jar and still be able to write some code without errors. The answer to this that jre libs are always in the class path even when you create a new project. So all the jre libs are available upfront. – Juned Ahsan May 07 '13 at 15:08
  • JRE libs, yes. But what about other framework libs, such as Spring, etc? We need to download them – Kevin Rave May 07 '13 at 15:40
  • You just need to add the dependencies, all the required libs will downloaded automatically once you build the project. – Juned Ahsan May 07 '13 at 16:33
  • I got that. But without the 'maven build' upfront (to download Jars), eclipse will not be able to do the context-help, like it does with the JRE libs. – Kevin Rave May 07 '13 at 16:36
0

You have to import your project as a Maven project (not as a normal Java project) to eclipse. Then m2eclipse gets active and will download the libraries. In the eclipse preferences you should configure Maven to download the sources also.

See Import local Maven projects for reference.

Kai
  • 38,985
  • 14
  • 88
  • 103
0

You don't really need m2eclipse. You can just use the mvn eclipse:eclipse goal to make the libraries visible in the IDE.

m2eclipse makes using Maven within Eclipse easier (refreshing dependencies when you change the pom from Eclipse, running goals, and many more).

You'll find this question useful: How to configure Eclipse build path to use Maven dependencies?.

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • The question is about how eclipse uses these libraries to help in the coding like I mentioned above. Because these libraries are not available until maven is run. Only when the jars are downloaded to the repo and project. – Kevin Rave May 07 '13 at 14:52