Once you have
created your maven project, which was the previous issue, it is time now to customize it to work with JEE6.
This post will show a bird view of maven and its pom configuration and a way to have a JEE configured project.
Maven in short
As said before
maven is a powerful tool that does a lot of things. This time we will show you a basic introduction to it as a build and dependency management tool.
The term artifact, is used as a deployable unit or a library. One of the
main problems in Java development is the dependency management, because
any project usually depends on third party libraries to achieve its
goals. Before maven this dependency management required to download the
artifacts and copy them inside the project in the worst case by hand.
The project in maven is described in a file called the "
project object module" or pom, an XML descriptor file that configures the project and its dependencies, of course it also has other information like the authors, license, etc. But for now let's focus on its basic structure. This is the content generated by the quickstart archetype.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.loxageek</groupId>
<artifactId>security</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>security</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
You can see the groupId, artifactId and version that compose the namespace of the project, its combination is suposed to be unique because it will identify the project in the repository.
The maven repository by the way is a centralized place where the build artifacts are suposed to be stored. There is a maven central repository where you can find the most known artifacts (i.e. Java EE API or JUnit) and there is also a local repository whose purpose is to maintain a copy of the commonly used artifacts and also to maintain the private organization artifacts. The maven repository is placed (in Linux) in a hidden directory called .m2 usually found under the $HOME folder.
Of course you can customize it and also create a mid size organization tailored private repository, take a look to
Nexus if you have this requirement. This could be a custom repository configuration that will save you some time and bandwith:
Figure 1. Maven repository configuration
With maven, the dependency management becomes easier, because pom.xml project descriptor enumerates the libraries (using its unique namespace) the project depends on in the
dependencies section. The pom previously shown for instance has a dependency for JUnit library version 3.8.1 that is part of the junit group.
Maven usage
As a build tool, it is necessary to stablish that maven has two types of tasks that can perform: phases and goals.
A
phase is one of the build steps that are executed commonly in project development, for instance you can
compile the code several times or
clean it to a fresh build, or maybe
test to run the unit tests.
Maven defines a lifecycle that every developer performs at least in part while building an application. So for instance to compile your project, you can execute the following command in your project directory:
$ mvn compile
this will create inside a
target directory the
classes folder that contains the compiled classes of the project.
A
goal in the other hand, is an activity that can be executed and serves to accomplish a purpose, a set of goals composes an archetype and is by this way that maven becomes extensible. We executed previously the goal
generate to create the project, this goal belongs to the archetype called
archetype, that is why the command is
$ mvn archetype:generate .....
Remember that you can open the project in the IDE of your choice. There are lot of other things you should know about maven like hierarchical poms, but we will show them when we start too modularize our application, because we will have a better environment to do it.
In the next post we will discuss our proposed sample project. So keep tuned.