Tutorial: Setting up Java projects with Maven2

aus eecoo wiki, der freien Wissensdatenbank

This article covers the following issues:

  • create a Maven project
  • setup the project to use Java 1.5
  • integrate the project into Eclipse
  • add dependencies to the project
  • setup Eclipse to use Maven2 to build the project

For installation issues about Maven2, Eclipse and other tools used in this tutorial please refer to the tutorial on setting up maven2 and external sites.

Eclipse requirements:

System requirements:

Inhaltsverzeichnis

Create a Maven2 project

Creating a Maven2 project is quite simple: (Note to replace all strings in brackets with your data, including the brackets themselves)

Open a terminal (command line interface) either by executing Start > Execute... > 'cmd' + Enter (Windows) or
by opening a new terminal (Unix/MacOSX). Browse to the base location of your project. This folder will be the parent folder of your project. For our purposes we select Eclipse's workspace directory as parent folder as we're planning to use the project inside Eclipse. Type
cd [path/to/eclipse]/workspace
to change to that directory. Create the Java project with Maven2's default layout (which is strongly recommended for straighten up the development process with Maven2) by typing:
mvn archetype:create -DgroupId=[com.your.company] -DartifactId=[project_name]
Maven2 creates the base structure of your project including a java source folder (src/main/java) and a output folder (target/java) for compiled classes. The source folder contains the package declared by the groupId and a first App.java file in it.

Note: to create a Webproject with Maven2 use the following command:
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp

Now we configure Maven2 to use the Java 1.5 compiler for our project: with a text editor open the pom.xml file in the projects root directory and include the following lines inside the <project ..>..</project> tag:

 <build>
   <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <configuration>
         <source>1.5</source>
         <target>1.5</target>
       </configuration>
     </plugin>
   </plugins>
 </build>

Integrate the project into Eclipse

Next, we enable the project to be a simple Eclipse Java Project by executing:
mvn eclipse:eclipse

After that you can open the project in Eclipse via "File > Import... > Existing Projects into Workspace...".

Image:Tutorial_Java_projects_with_Maven2-1.png

Image:Tutorial_Java_projects_with_Maven2-2.png

Enable Maven2 support

Then turn on Maven2 support for the newly created project: in the Package Explorer select 'Maven2 > Enable' from the context menu of the new project.

Image:Tutorial_Java_projects_with_Maven2-3.png

Solve classpath problems

Now we have to solve the classpath problems to be able to compile the project. Therefore configure the M2 Eclipse plugin to find the local Maven2 repository. In the preferences dialog Window > Preferences... select the Maven2 tab and put in the path to your local Maven2 repository (which is mostly located in /your/username/.m2).

Image:Tutorial_Java_projects_with_Maven2-4.png

If you encounter other classpath related problems, make sure that your classpath only contains the "JRE System Libraries" and "Maven2 Dependencies" which should be automatically added as you enable Maven2 support. Normally you can just kick the other classpath entries by selecting each entry and hitting the "Remove" button on the right hand side. The result should look similar to:

Image:Tutorial_Java_projects_with_Maven2-5.png

Finally we can clean and compile the new project via Eclipse's Project menu.

Performing basic Maven tasks

Managing dependencies

One advantage of using Maven is that it can help you to manage your projects dependencies. You don't have to worry about downloading every library you want to use in your project any more, Maven does this for you now. What you simply have to do to use this feature is editing the "dependencies" section in the projects pom.xml file. Happily, as we use the Maven2 plugin for Eclipse, you don't have to edit the pom-file manually.

To add Maven dependencies within Eclipse right click on the (Maven2 enabled) project, select "Maven2 > Add dependency..." as shown below.

Image:Tutorial_Java_projects_with_Maven2-8.png

In this dialog you can query the default Maven2 remote repository (which normally is located under [1]) for your dependent library. The following image i.e. shows the available libraries of Apache's "commons-logging" utils.

Image:Tutorial_Java_projects_with_Maven2-9.png

After selecting the right version of the library and submitting the dialog with "Ok" the dependency is automatically added to your project's pom.xml-file and consequently to your project's classpath. The next time you build the project (using Maven2 as described in the next section) Maven will download the library to your local repository.

The removal or editing of dependencies has (in the current version of the m2 Eclipse plugin) to be done manually by editing the pom-file in the root folder of your project.

Building with Maven2

As Maven is not just a dependency tracking tool but rather a complete build solution, you should regularly perform goals like compile, clean, install etc. This is important especially if the project is shared between different users and set up for (remote) continuous integration based on Maven (aka Continuum). For those projects it is strongly recommended that you perform all compile and test targets on your local machine successfully before committing any source to the remote repository. In the following section we describe how to access those Maven goals from within Eclipse.

As you can recheck in the project properties there appears a new builder in the "Builder" section when you enable Maven2 support. Your "Builder" section should look similar to this one:

Image:Tutorial_Java_projects_with_Maven2-6.png

With this builder installed you can access the Maven tasks via the "Run > External Tools > External Tools..." menu item. Double click on the "m2 build" entry on the left bar to create a new configuration. You can name it i.e. "Maven install" as you can use it for several projects at once. As base directory you should choose "${project_loc}" (via the "Variables..." button) in case you want use this config for more than one specific project. The last thing to do is to put in the goals you want to perform when running this build command. We simply use "install" to perform all goals untill the "install" phase (yes, all phases before the selected one like "test" etc. are executed by Maven as well; so just put in the last goal you want to be performed). After clicking the "Apply" button your ready to use this build command. The following screenshot summarizes the settings describe above:

Image:Tutorial_Java_projects_with_Maven2-7.png

Now you can hit the "Run" button to compile the sources and install the compiled JARs in your local Maven2 repository (which normally is placed under [userHome]/.m2/repository). The generated artifacts are placed in the "target" directory in your project's folder.

Where to go from here?

This article described the basic tasks for setting up a Maven2 project and integrate it within the Eclipse IDE. You can now go on with the following steps:

Furthe you can read more on the links to related technologies given below.

Read more

Feedback

Found any mistakes in the text? Want to provide some feedback on this article? Please feel free to contact the author of this article.