2013-05-30

Feeling comfortable with maven

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.

2013-05-14

Generate a maven project

This series of posts will try to show the way that a JEE6 project should be organized in terms of modules and physical organization.

The tools and technologies used in this sample project are:
  • JEE 6 
  • HTML 5
  • JBoss A.S. 7
  • Maven
  • JUnit
  • Jenkins
  • IDE Netbeans 7.3
So, why is this series of posts going to do different from a lot of other posts that talk about the same issues? Maybe it is that it will try to show a way to organize the project in modules and start a discussion about the ways to do it. Yes, there are a lot of software engineering, architecture or technical books that have a deep discussion about this, but almost all of them fail in terms of practical and straight ways to show a "how to do it", so there is the why.

The sample chosen for this blog will be a user management module (yes, module), a big enough sample to show concepts like modularization, unit testing, and other concepts that I am trying to expose in here. The explicit requirements will be shown in a future post as "user histories" to illustrate some Agile and TDD concepts.

As you can see this blog is a bit ambitious, but I do it because it is the only way that lot of isolated concepts get clear and be useful to the software developers. There is a lot of work to do, so let's start.

Creating the initial project

Maven is becoming the defacto tool for building and deploying tasks, slowly replacing our good and well known ant system.

What I like about maven is the fact that it is IDE agnostic, you can create a maven project in the IDE flavour you prefer and open it everywhere else. The tool tries to put order in the way the projects are organized and avoids the "reinventing the wheel" symptom on every new project. But in what maven shines is in avoiding us the libraries management nightmare through a repository whose main goal is to centralize the developed artifacts (i.e. jars) and its versions.

In maven, a project is usually intended to generate a deployable unit whose namespace is composed of three elements: A group identifier, an artifact identifier and a version number. The first one is intended to identify the organization behind the project, the second will identify the project name  and the third one the version of that artifact in time. This is a typical project configuration:

<groupId>org.loxageek</groupId>
<artifactId>security</artifactId>
<version>1.0-SNAPSHOT</version>

Once installed maven in your box, it is easy to create a new project. For instance to create the project identified previously you can execute the following command in your command line, remember to change to your sandbox directory.

$ mvn archetype:generate -DgroupId=org.loxageek -DartifactId=security -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command will create a project directory structure under security as shown in the next scheme:


And that is it, you have now a basic project template to start coding as soon as you want opening it in the IDE you like it more.

The next post will unveil some of the inner basic features of maven.