Tag Archives: maven2

Related softwares:

  1. Eclipse 3.5
  2. JDK 1.6
  3. Maven 2.2.1

First, install M2clipse-plugin in Eclipse via below M2clipse update site:

http://m2eclipse.sonatype.org/update/

If you don’t know how, please refer to this article on how to install Eclipse plugin via update site.

To use the latest Maven2, which is version 2.2.1 at the point of writing this article, you need to download the binaries from below site:

http://lawyersdb.com/mirrors/apache/maven/binaries/apache-maven-2.2.1-bin.tar.gz

Once downloaded the apache-maven-2.2.1.tar.gz archive file, unzip/untar it and place the apache-maven-2.2.1 folder in /opt. So, /opt/apache-maven-2.2.1 will be known as $M2_HOME. To set this environment variable manuallly into the system, add this below line in /etc/environment file.

JAVA_HOME=/usr/lib/jvm/java-6-sun
M2_HOME=/opt/apache-maven-2.2.1

Add also the $JAVA_HOME variable, if you haven’t done so. But make sure it is pointing to your JDK home folder. You need to re-login into your Ubuntu for this new environment variable to take effect

After re-login, create a mvn link in /usr/bin, for you to run the mvn command globally. Type the following:

cd /usr/bin
ln $M2_HOME/bin/mvn /usr/bin/mvn

To check Maven2 installation status, type below command in your console:

mvn -version

You should see Maven2 printing some info regarding the version.

Back to the Eclipse. Now, you need to setup External Tools to point to this new /usr/bin/mvn command we just created. At Eclipse main menu, choose Run > External Tools > External Tools Configuration…

Create External Tools

External Tools Configuration dialog will be prompted as below.

Empty External Tools Configuration

Right-click on Program and choose New.

Create New External Tool Menu

A new program form will be displayed. Enter the following details:

  1. Name : Maven
  2. Location : /usr/bin/mvn
  3. Working Directory : ${project_loc}
  4. Arguments : ${string_prompt}

Maven2 External Tools Config Setup

Click on Apply and Close buttons.

To use the newly created External Tools we just created, click to highlight your Maven2 project.

Choose Maven2 Project

On the Eclipse main menu, choose Run > External Tools > External Tools Configuration…

Choose External Configuration Menu to Run Maven2

Then, on the External Tools Configuration dialog, choose Maven and click on Run button.

Choose Maven to Run External Tools

A Variable input dialog will be prompted. Key in your mvn parameter you wish to pass and click OK button.

Variable Input dialog

You will see your Maven2 console is running and processing your command as shown in below.

Maven2 console

If you had setup a private remote repository, you can save the hassle for downloading artifacts from the public remote repository (central repo). Your private remote repository will download it for you and cache it on the private server. If you want to download from your private remote repository, you need to update or create your Maven2 settings.xml file. This file should be located at ${USER_HOME}/.m2 folder. The content of the file is as following:

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 <pluginGroups/>
 <proxies/>
 <servers/>
 <mirrors>
   <mirror>
     <id>myPrivateRemoteRepository</id>
     <mirrorOf>*</mirrorOf>
     <name>My Private Remote Repository</name>
     <url>[PRIVATE_REMOTE_REPOSITORY_URL]</url>
   </mirror>
 </mirrors>
 <profiles/>
</settings>

Replace the [PRIVATE_REMOTE_REPOSITORY_URL] with your private remote repository URL. You can put any value on the id and name elements, but it just need to be unique between other mirrors.

Traditionally, creating a new Java project is very cumbersome and time consuming, especially when you required a lot of external libraries. I might have to go to each library’s project website to search and download the jar file. Most probably I might just copied from previous project’s libraries.

Maven and Eclipse help me to avoid this mundane work. The feature of Maven with Eclipse I like to demo right now is the ‘Search for dependency from central repository’.

First, you need to do is create a Maven Project like below.

Create New Maven Project

Create New Maven Project

Then you will have the new Maven project in your package explorer. New project will consists of JUnit library by default.

Maven Project in Package Explorer

Maven Project in Package Explorer

It will also comes with App.java class by default. I will use this java class for the demo.

Standard App.java

Standard App.java

Let’s say I need to use commons-logging library, which is not yet included in the project’s classpath. I just typed in the code and of course I will get the errors as below.

Added Log and LogFactory object.

Added Log and LogFactory object.

Next, I highlighted the ‘Log’ object and I pressed Ctrl+1 to pop out the context menu. Then, choose ‘Search dependency for Log’ option.

Search for dependency from context menu

Search for dependency from context menu

I typed ‘*logging’ to search for the specific Log’s library. It will search the maven central repository based on the pattern. Then, I select Log class from commons-logging artifact.

Choose specific library

Choose specific library

Automatically, commons-logging library will be in my project’s classpath.

Commons-logging jar file is added

Commons-logging jar file is added

Log class also will be automatically imported into App.java class.

Log object is imported

Log object is imported

Since LogFactory class is from the same library, I just need to import the class by pressing Ctrl+Shift+O. This would solve all the problem and error.

Import LogFactory class.

Import LogFactory class.

My pom file is also updated automatically.

POM updated

POM updated

Isn’t this a great feature? No more searching and downloading the libraries manually from the web. Hellooo productivitayyyy.

Need to start a project in lightning speed? If you want to skip those dirty works in setting up a project framework, well…, I just found out a great utility just for that. It is called Appfuse 2. Now with version 2, it is tied together with Maven 2 which makes the setup a lot easier. The prerequisites for applying Appfuse is Maven 2 and MySQL 5. You need to install these 2 in your machine.
There are several archetypes you could choose from, and for the sake of writing, I just choose appfuse-basic-jsf. So I just run below command, it will create a project with JSF,Spring & Hibernate. There are also other persistence files included, such as JPA and iBatis.

mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-jsf -DremoteRepositories=http://static.appfuse.org/releases -DarchetypeVersion=2.0.2 -DgroupId=org.dhydrated.appfuse.jsf -DartifactId=appfuseJsfWeb

Then, I need to tweak the pom according to my MySQL installation. By default, it uses root with no password as the MySQL user. So I just simply add my password in the pom file below.


<?xml version="1.0" encoding="UTF-8"?>
<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.dhydrated.appfuse.jsf</groupId>
<artifactId>appfuseJsfWeb</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>AppFuse JSF Application</name>
<url>http://www.mycompany.com</url>
...
<properties>
...
<jdbc.username>root</jdbc.username>
<jdbc.password>password</jdbc.password>
</properties>
</project>

Then, I run below command and let Maven 2 do all the boring stuffs such as dependencies checking, creating tables in database and etc. It will take a while if you doing it for the first time.

mvn

Then, I need to package it into war, so I just simply run below command.

mvn war:war

Next, I need to tweak again the pom since I’m deploying the application into Glassfish 2 in my machine. By default, the application is set to run with Jetty or Tomcat. So if your choice of application server is one of these default settings, you can skip this step. I’ll add below maven plugin to enable me to deploy my application into Glassfish 2.


<?xml version="1.0" encoding="UTF-8"?>
<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">
...
    <build>
        <finalName>${artifactId}</finalName>
<plugins>
<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>deploy</id>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>C:/Program Files/glassfish-v2ur1/bin/asadmin.bat</executable>
                    <arguments>
                        <argument>deploy</argument>
                        <argument>--user=admin</argument>
                        <argument>--passwordfile="C:/passwordfile.txt"</argument>
                        <argument>--host=localhost</argument>
                        <argument>--port=14360</argument>
                        <argument>target/${artifactId}.${packaging}</argument>
                    </arguments>
                </configuration>
            </plugin>
            ...
        </plugins>
    </build>
    ...
    </project>

Above, I’m using exec-maven-plugin to run an executable command to deploy my war into Glassfish 2. In order to call above plugin, I typed below command.

mvn exec:exec

After that, you should be able to see your application is being deploy from your server console. After all these steps, Presto!, now you have a full-fledged Java Enterprise application running on your server. Thanks to Spring, it is also equipped with security and transaction management. Couple of users with specific roles are already included.

If I run my application, I should get the Login page as below.

appfuse-jsf-login

appfuse-jsf-login

Above is the login page. One of the default user is admin.

appfuse-jsf-home

appfuse-jsf-home

Above is the home page once you are logged in. It uses Struts-Menu for menu layout.

appfuse-jsf-users

appfuse-jsf-users

It also includes CRUD pages for Users.

How’s that for speed? It only took 10 minutes to create this application (if you know how to do it, of course). There are several other UI frameworks you could choose from, besides JSF. I guess the 2 most commonly used are JSF and Struts2. If I only knew about Appfuse 2, I would have used it on my last project. It is awesome. I don’t need to do all the basic stuffs anymore and I can just straight away focus on the business requirements. Yeah!

Starting a new project is time consuming. You need to determine the technologies involved, build the project structure, setup the configuration files, work out the dependencies of jar files and etc. Before I had encountered Maven, I just used to copy my existing project, and start working from there. I need to add/delete, Java, jar and other resources files prior to starting a new project. With Maven came into the picture, I need not to worry about most of the tasks I just mentioned.

There are several ways you could start you project with Maven. But I choose to use the generate goal from archetype plug-in because it seems to be the easiest way to start a new project. Archetype is like a project template, where Maven has a set of predefined project templates we could choose from. If we do not specify a specific archetype, it would goes into an interactive mode, which is awesome. I would hate it if I need to remember those archetype names. Below picture, is how I run the generate task command.

When you hit the [Enter] key, you would get the archetype list as below picture.

From the picture above you could see, there are 44 archetypes you could choose from. For a simple webapp, I should choose number 18, which represents maven-archetype-webapp archetype. Key in number 18 and hit [Enter] key.

Then, you will be asked to enter your project details as below.

Console will ask you to confirm the given details as below.

Finally, Maven will start working in the background and produce below output in the console.

If you are running for the first time, it will check all dependencies and download it if necessary. Downloaded dependencies are stored in your local repository. By default, if you are installing Maven in Windows XP, it will be at C:\Documents and Settings\<user>\.m path.

Now, Maven had created below project structure.

From here, you need to add more details to your pom.xml file to add more dependencies details in order for Maven to download the necessary jar files. By default, the pom.xml will be configured with JUnit the testing component.

Maven is a good utility to speed up and facilitate the building process of your application. It adopts standard approach so all projects has similar architecture. New developer, who just comes in to join a project, could easily get a good understanding of an application. Developer will requires less time on wading through specific configurations and project structure.

For those who are fan of Eclipse or NetBean IDE, there is a good news for you. Maven have plug-ins which enables it to be integrated with these IDE. Working with Maven through IDE, is much simpler than above mentioned process. :)