How to create a simple Java project with Maven 3?

Managing your project with Maven gives you a lot of advantages. You can automatically compile, pack into JAR or run tests on your project. You can also manage your project dependencies with almost no effort. To create JAR project I will use a powerful Maven capability called archetypes. Maven gives you a possibility to predefine your project template (archetype in Maven terminology), so that for different projects you can use different archetypes. Maven also comes with predefined archetypes for creating different projects like simple JAR or J2EE project.


Requirements

  • Eclipse Kepler 4.3 (with m2eclipse plugin)
  • Apache Maven 3
  • JDK (1.5 or above)

A simple JAR project in Maven 3

To create a simple JAR project I will use an archetype called maven-archetype-simple (put it as one line in your shell):

mvn archetype:generate -DgroupId=com.geekingspree.maven -DartifactId=maventutorial -Dpackage=com.geekingspree.maven -Dversion=1.0-SNAPSHOT

After you type this command you will get interactive Maven archetype generation with three questions:
  1. You need to choose the number of an archetype. Here you need to specify 294 (maven-archetype-quickstart).
  2. The second question regards the version of an archetype to be used. Select default (by default it's always the newest one).
  3. At the end you need to confirm your project properties: groupId, artifactId, version and package. Just press Enter to confirm.

Opening Maven project in Eclipse IDE

Let's take a look at Maven project structure. I will open this simple project in Eclipse. I could use any other IDE like NetBeans or IDEA, but since I have Eclipse Kepler 4.3 installed right now, I will just use it. First I need to Eclipsify my Maven project. To do so I need to go to maventutorial directory and execute eclipse:eclipse maven command:

cd maventutorial
mvn eclipse:eclipse

Now you can open Eclipse and go to File -> Import -> Existing Maven project. Than select your project from the file system and click Finish.

Overview of maven-archetype-quickstart project structure


As you can see above, Maven creates the following project structure:
  • maventutorial <-- this is project root directory
    • src <-- contains source code
      • main/java <-- contains source code for business logic of application
        • com.geekingspree.maven <--  source code java package
      • test/java <-- contains source code for automated tests
        •  com.geekingspree.maven <-- source code java package (tests)
    • pom.xml <-- see description below

What is pom.xml

Generally POM means Project Object Model. This file describes your project for Maven. It's like Makefile for make program or build.xml for Ant, but much more powerful.

What is inside pom.xml

Let's take a look into pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.geekingspree.maven</groupId>
  <artifactId>maventutorial</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>maventutorial</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

This pom file is really simple, but it gives you an insight to what kind of elements can be contained in this file. I will bring more complicated pom files in the future. Now I will describe this one:
  • groupId it's a so-called project coordinate. This one describes a group in which this project is stored. If I wanted all my projects to be stored under com.geekingspree I would set this as groupId.
  • artifactId is also a project coordinate. It is a unique name for a single project.
  • version is a project version (also a project coordinate).
  • packaging either jar or war (also a project coordinate):
    • jar tells Maven to produce a Java archive
    • war tells Maven to produce a Web application
  • name specifies the project website title (see Maven site generation).
  • url project homepage URL (see Maven site generation).
  • project.build.sourceEncoding source files encoding.
  • dependencies is a section which describes project dependencies. If not specified Maven dependencies will be downloaded from http://repo1.maven.org/maven2 (this is a default repository location). Each dependency is described by coordinates (groupId, artifactId, version) and scope. In this case,  scope is set to test, which means that this dependency will be included only during test execution. I will talk about Maven scopes in another post.

Maven site generation

Project site is yet another really cool feature of Maven. Just go to your project directory and execute:

mvn site
This one will generate a website of your project. Now you can go to maventutorial/target/site and open index.html in your browser:



Building JAR in Maven

To build JAR execute:

mvn install
It will compile your sources and archive them into JAR: maventutorial/target/maventutorial-1.0-SNAPSHOT.jar.

Running tests

You can also execute tests from src/test/java directory by typing:

mvn test

0 comments :: How to create a simple Java project with Maven 3?

Post a Comment