Friday, April 30, 2010

Advantage of Maven Transitive Dependency

Getting back to the Hibernate tutorial, now I understand the advantage of using Maven. Defining the dependency elements in the pom.xml is like asking Maven to take care of downloading the required files from the default repositories (repo1.maven.org by default) and paste those files in your repository so that they are accessible by your classpath.
Typing the "ibiblio.org maven2 hsqldb" in Google, you can find maven-metada.xml. This file gives you the groupId, the artifactId and the version number that you can use to have Maven retrieve an HyperSQL database server file that you can include in your project.
For Hibernate, you don't need to download Hibernate bundle as you'll have to manually include hibernate3.jar file and its related libraries in your project and create the correct folder layout for all those libraries to be included in your classpath. Maven relieves you of this burden; what you need to do is to specify the groupId (org.hibernate) and the artifactId (hibernate-core) and the last version available (3.3.1.GA). Those information can be found in either ibiblio.org or Hibernate tutorial book.

Thursday, April 29, 2010

Understanding Dependency - Starting HSQLDB server from Maven

Dependency is a mechanism for controlling modules in a project. Each dependency needs to be added under the dependencies element in pom.xml.

A dependeccy needs at least those elements: groupId, artifactId and version. To have mvn starting the HSQLDB server as specified in the tutorial, we need to add a dependency refering to an installed version of HSQLDB on the local drive. For that purpose, we need to know the groupId, the artifactId to be used. Using Google, ibiblio can help you know the correct groupId and artifactId to use for HSQLDB. In Google, search for "http://www.ibiblio.org/ maven2 hsqldb".
Open maven2-metadata.xml; this file contains the information about the groupId and artifactId for HSQLDB which are for both hsqldb. You also need to specify the scope element.
Maven Guide defines the scope of the dependency as a property that limits dependency transitivity. When you run Maven, it needs to discover the libraries needed for your application. Maven uses the dependency defined in pom.xml, but sometimes those dependencies require other dependencies that Maven will discover and include them automatically.
As specified earlier, the scope limits the transitivity of a dependency. There are 6 scopes that can be used, and the one to use when using a jar file from your local system (hsqldb is actually a jar file) is system.
The dependency element to add in your pom.xml is going to be:
<dependency>
<groupeId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.0.0</version>
<scope>system</scope>
<systemPath>path/to/hsqldb.jar</systemPath>
</dependency>

You need to download hsqldb and add \lib to your system Path environment variable (user environment variable for Linux should work).
Then from the command line, type: mvn exec:java
-Dexec.mainClass="org.hsqldb.server.Server" -Dexec.args="-database.0 file:target/data/tutorial"

Understanding Maven Archetype

The Archetype plugin allows you to use a pre-defined pattern for project development. You can have a pattern for different kind of project (Hibernate/Spring, JSF or JBoss Service Archive). You can have 249 choices of Archetype using this command from the CLI:

mvn archetype:generate.

Choice #15 is a quick start template, it sets up a simple JEE application embedded with Apache Jetty application server.

Getting back to Hibernate tutorial, they recommand you to use mvn archetype:create to build your template.

create goal has been deprecated as of version 2-0 of the plugin although it still works. To create the template, you can start running the goal and define groupId and artifactId:

mvn archetype:create -DgroupId=org.hibernate.tutorials -DartifactId=hibernate-tutorial

The Archetype plugin creates the following project template:

hibernate-tutorial

--src
--main
--java
--org
--hibernate
--tutorials
--App.java
--test
--java
--org
--hibernate
--tutorials
--AppTest.java



As you can see, the groupId property defines the package structure of the project, the artifactId property is the root folder of the application. A test folder is also generated with the same package structure and a Test file.
The pom.xml file is created at the same level as src. If you edit the file, you will see that a dependency is created for JUnit v3.8.1.

Tuesday, April 27, 2010

Maven Configuration Setup

As I was doing the Hibernate tutorial (which is a nightmare by the way), I was required to learn about Maven and the HSQLDB database server. To build the tutorial, you have to use Maven and HSQLDB. Following this tutorial step by step was impossible, and in Hibernate's community forum, it seemed that I was not the only person complaining.
HSQLDB is pretty straightforward to set up. It is a full Java database; its database server runs with the java command from command line and it also comes along with a Swing client.
Maven is a little more challenging. Maven as far as I understand is similar to Ant but is easier to manipulate. Like Ant, it's a build automation tool and a project management tool.

Once, you have downloaded Maven installation file, follow the configuration steps in the readme file. Yes... it seems obvious but I rarely read readme files. Maven website doesn't give directions for basic configuration. So when I started Maven's tutorial, nothing worked. I was stucked with Maven, and I could not go ahead with Hibernate. Here are the basic directions from Windows or Linux:
- Unzip the files in a folder.
- Add M2_HOME environment variable (user level).
- For Linux, add a M2 environment variable that points to $M2_HOME/bin at user level.
- Optionally, add MAVEN_OPTS environment variable with the following values: "-Xms256m -Xmx512m".
- Make sure the JAVA_HOME is defined.
- Add MAVEN's binary files into PATH (M2_HOME\bin for Windows and M2 for Linux).
Test your configuration from the command line interface with: mvn --version.