Java Development with NetBeans and Maven: Part 2

Overview

Now we will focus on the further maven configuration. There are a lot of pulgins provided by the Maven. I would like to pay more attention to the unit testing, the Maven Surefire Plugin and Maven Surefire Report Plugin. Normally the JUnit is supported by default. I also prefer the latest JUnit 4.

Not only the unit testing, but also my most favorite library, the Apache Log4J. I will add it to my project at the same time. I cannot live without Log4J.

Adding New Library To The Project

Previously, without Maven, I’ve faced the trouble about managing my java library. I’ve no idea to organize it as the following scenarios: –

  1. Where is the right place to store the library? The my decided shared folder? The Java project folder? The WEB-INF/lib folder?
  2. Should I set the user library inside my IDE? Which IDE should I use? How to share them across my team?
  3. How to use this structure across my team?
  4. How to ensure the completion when we get the source code from repository or version control?
  5. Blah blah blah….

My trouble is solved by the Maven. They are stored and managed transparently by the Local Repository. There is no any stored library inside my project folder. Only the dependency configuration is required.

Let’s try to add the Log4J

I will show how simple it is. Just open the Project POM file (pom.xml) and add the Log4J dependency, inside the <dependencies> tag. Again I prefer the latest version as always.

<dependencies>
    ................
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

Next we will add the Log4J configuration to our project, by default this configuration file is nice to be stored at the “Project Default Package”, by the way it would be better to be stored at the “CLASSPATH”. Anyhow, regarding to my experience, I prefer to store it at the “Project Default Package”. Please note, it is just a simply copying and pasting to that folder. Then we will configure the Project POM file (pom.xml) so that it knows our configuration by adding the <resources> tag inside the <build> tag.

<resources>
    <resource>
        <directory>${basedir}/src/main/java</directory>
        <includes>
            <include>YOUR_RESOURCE</include>
            <include>YOUR_RESOURCE</include>
        </includes>
    </resource>
</resources>

Please note, you can add more resources as you need.

Writing The Code

I will modify my previous class, the “HelloWorld” application so that it print out the message before returning.

package com.scc.maven.java.prj01;

import org.apache.log4j.Logger;

/**
 * Hello world!
 *
 */
public class App{
    private static final Logger logger = Logger.getLogger(App.class);
    public String says(String name){
        String message = "Hello " + name;
        logger.info("The saying message is: " + message);
        return message;
    }
}

Create TestSuite

At the moment I will be back the unit testing by adding the “TestSuite” to my project. Normally I name it by add the wording “TestSuite” at the end of the class name. You will see the reason why I do like this.

package com.scc.maven.java.prj01;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

/**
 *
 * @author Charlee.Ch
 */
@RunWith(Suite.class)
@Suite.SuiteClasses({com.scc.maven.java.prj01.AppTest.class})
public class MyFirstTestSuite {

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

}

It has not been finished yet, The Project POM file (pom.xml) need to be modified by mention the “Maven Surefire Pluginat the <plugins> tag and configure it so that it accept my test suite class.

The key is “**/*TestSuite*.java” and it is a reason why I name it as it is.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <includes>
            <include>
                **/*TestSuite*.java
            </include>
        </includes>
    </configuration>
</plugin>

Building & Testing

As I’ve mentioned before, during the “Build” or “Clean and Build” is executed, the unit testing will be executed automatically. If you would like to explicitly execute, just right clicking at your test class and choose “Test File”.

Configure The Unit Testing Report

I would like to create a summary report for my unit testing by using the “Maven Surefire Report Plugin”. Please note, the report will be created automatically when the maven site is created, anyhow you can explicitly execute. At the moment I prefer running explicitly.

Configure the Maven Surefire Report Plugin

Firstly we will modify the Project POM file (pom.xml) by adding the <reporting> tag and “Maven Surefire Report Plugin” configuration at the same level of the <build> tag.

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>2.6</version>
        </plugin>
    </plugins>
</reporting>

Add Custom Maven Command

Second I will add the “Custom Maven Command” to the NetBeans Java Project so that the unit testing report can be generated. It is simply action as the following step: –

  1. Go to project properties.
  2. Inside the “Project Properties” windows, under the “Categories”, choose “Actions”.
  3. On the right hand panel, Click “Add Custom..” button.
  4. Enter the “Action Name” as “My.Surefire.Report”.
  5. Enter the “Execure Goal” as “surefire-report:report ”.

Execute The Custom Maven Command

Right click at your project and choose “Custom” –> “My.Surefire.Report”, It will display my previous created “Custom Maven Command”. Please note, the missing resources which are not in your “Local Repository” will be downloaded automatically as usual. The generated report is stored at <Project Folder>/target/site/surefire-report.html. Please look around it.

Summary

Now we are understand about the further Maven configuration, including with adding new resources, configuring the test suite and especially the unit testing report, plus create a custom maven command inside the NetBeans.

Next I will introduce more Maven Configuration, e.g. creating a site, configuring the version control, configuring the issues tracker, make use of them as a release note, and so on. Please stay tuned as always.

About Charlee Chitsuk

I've been working as a software developer since 1998. At the moment I focus on system integration including with SOA and Enterprise Security Solution.

Posted on November 10, 2010, in Artifactory, Java SE, JUnit, Maven and tagged , , , . Bookmark the permalink. 1 Comment.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: