Using Maven and Eclipse, the basicsFrom WikiJava
Maven is a very powerful tool, because it allows you to start many types of project from a template, rather than from a scratch. And because it frees you from having to download and possibly store in your repository the Jars with the libraries you need. In this article I'm going to show you how to create a simple java project, and how to include it in Eclipse, keeping also all the dependencies on external libraries. I'll show you in the article: Using Maven with any jar dependency, how to deal with the specific situation where the dependency we want to use is not available in the main repository. (which is not so rare apparently)
a Little bit about MavenMaven, according to the official site, was made for:
To achieve these goals the way they took was to make maven handle very specific (state of the art/best practice ... ) organizations of the projects. So that maven would be able to build, deploy and perform many actions on any project, as long as it follows the specifications. Maven has a great system to help us following the project specifications. That is: It can create most of all of them automatically. This is achieved via a superb template system. We will see below that with just two command line commands we can create a full-fledged java project, and even the Eclipse project files so that we don't need to bother any more with importing libraries. Since all the projects created with maven have more or less the same file structure, Maven can build, deploy, package and test them with always the same few commands. offering the uniform build system mentioned above. Starting a maven projectAs said I won't go through this too deeply, this is covered very well else where and it makes no sense to repeat it here. We will just generate a simple (empty) java project with the command: mvn archetype:create -DgroupId=com.test -DartifactId=mytest This will create a subdirectory called mytest containing a pom file and a src directory which contains java files for unittest and for your code. In this new directory there's also the important pom.xml file, containing all the important data for building and using your new project. This includes also the dependencies that tell maven which Libraries to download and put in the classpath of your project so that it can work. Next step we want to achieve is to use the information contained in the pom.xml file to generate the project files for Eclipse, so that we can use our favourite IDE to write our code. Using your project in EclipseSince all the information required for the eclipse project is available in the pom.xml file, it is really simple to use maven to do the magic. The command is the following: cd mytest
mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=trueAt this point you can:
The Dependencies of the eclipse project generated by Maven are done (in the way they are supposed to be) so that all the paths are relative to a variable called M2_REPO which you should point to the directory where Maven holds the local repository. The local repository is at:
Here's how to set a variable in Eclipse for it: open the eclipse preferences panel and type in the search box the word variables. This should let you see in the list below the menu Classpath variables looking like in the figure. Click on New
and in the window type At this point you can rebuild the project and it's all there. You can add any other dependency to your project by editing your pom.xml file and adding chunks like:
<dependency> <groupId>net.sourceforge.javacsv</groupId> <artifactId>javacsv</artifactId> <version>2.0</version> <optional>false</optional> </dependency> Then you can repeat the steps explained in this section. to refresh the eclipse project files. and have everything ready again. conclusionsIn this article we have seen how to generate a new Java project, having few file structures ready for use, and we imported the project in Eclipse, maintaining the Library dependencies. We did all this using Maven, in two simple commands. In the article Using Maven with any jar dependency We will get to the funny part, where we will use as dependencies some jars that are not in the maven main repository, and for this reason need special treatment.
|

