Java Development with NetBeans and Maven: Part 1

Overview

Regarding to my previous post, I’ve finished preparing my environment so that it supports the Maven development. Now I will start preparing my IDE, the NetBeans 6.9.1. By default, it supports the Maven 1.x and 2.x, please note at the moment the supporting Maven 3 is planned for the NetBeans 7.x which is not released yet. If there is any updated, I will post.

NetBeans 6.9.1 Configuration

It is straightforward configuration as the following steps: -

  1. Open the NetBeans IDE
  2. Go to menu “Tools” –> “Options”.
  3. Inside the “Options” window, click “Miscellaneous”.
  4. Choose “Maven” tab.
  5. Enter the “External Maven Home”.
  6. Optional, enter the “Local Repository”. Please note, my previous post also mentions about the maven configuration, the settings.xml. If you set the value here, it is double setting.

Create the first Maven / Java Project

It is simple as usual when you create a project under the NetBeans as the following steps:-

  1. Open the NetBeans IDE
  2. Go to menu “File” –> “New Project…”.
  3. Inside the “New Project” windows, choose “Categories: Maven”, “Project: Maven Project”.
  4. Select the “Maven Quickstart Archetype(1.0)” for the “Maven Archetype”.
  5. Enter the project information as usual. Please decide the suitable value for the “Group Id”, “Version” and “Package”.
  6. After you have finished, the system will download the related resources from the “Maven Repository Manager”, for me, it is “Articatory”. You will see the process via the “NetBeans: Output Panel”.
  7. Please look around your environment for making yourselves to be familiar with this new project type, the project folder.
  8. Not only the project folder, but also your “Local Repository” which is configured either “settings.xml” or the NetBeans options.
  9. Let’s build this empty project. Please look around your environment as well.
  10. You may be wondered that the “settings.xml” display under the “Project: Project Files”, but you cannot find this file from your physical project directory/folder. Please note, it is a relative file to your original “settings.xml”.

Configure the Java Source Code Version and File Encoding

By default it is set to support the java 1.3 we will change it to either 5, 6 or 7. At the moment, I prefer 1.6 with UTF-8 encoding. The step is as following: -

  1. Go to project properties.
  2. Inside the “Project Properties” windows, under the “Categories”, choose “Source”.
  3. Set “Source/Binary Format” to “1.6”.
  4. Set “Encoding” to “UTF-8”.

The Downloading Action

The Maven will download only the missing resources which are not in your “Local Repository”. Please look around your project POM file (pom.xml) or see further information here.

The First Java Class

The “HelloWorld” is still nice as always. The java class is created inside the project automatically. We will make some modifying over it by creating a method named “says” which receives the String parameter named “name” and return String which we say.

package com.scc.maven.java.prj01;
/**
* Hello world!
*
*/
public class App{
   public String says(String name){
      return "Hello " + name;
   }
}

How we can test it.

It is not a good practice to create the main method for testing. I encourage you to use the JUnit. By default the predefined is JUnit verion 3.8.1, I would like to use the latest version, at the moment, it is version 4.8.2.

Configure the JUnit

Please open your project POM file (pom.xml), it is under your “Project: Project Files”, go to the <dependencies> tag. You will see the Junit configuration. Just change it to 4.8.2. Please note, the automation completion is applied here, too.

The automatic completion may seem not working. Please open the “Maven Repository Browser” by going to menu “Window” –> “Other”. Click the “Update Indexes” button, or right click and choose “Update Indexes”.

The system will download the new configure resources automatically as always. Please look around your environment.

Cautions

This action requires the internet accessing, the downloaded indexes is quite big. Around 200-300 MB or more. You may copy from the one who already loaded by copying and pasting to your machine.

Path for copying and pasting the Remote Maven Indexes

<USER_HOME>\.netbeans\6.9\var\cache\mavenindex

Example: C:\Users\Charlee.Ch\.netbeans\6.9\var\cache\mavenindex

Create a Unit Testing

The java class which is a unit testing is also created inside the project as well. It is just testing the “says” method for ensuring the returned value.

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.Test;
import static org.junit.Assert.*;

/**
 * Unit test for simple App.
 */
public class AppTest{

    public AppTest(){
    }

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

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

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    /**
     * Test saying
     */
    @Test
    public void testAppSaying(){
        App app = new App();
        String message = app.says("Charlee");
        assertEquals(message, "Hello Charlee");
    }
}

Building & Testing

Just right click at your project and choose “Build” or “Clean and Build”. The system will build your project, including with executing your unit testing automatically. Please look around your environment.

You can execute the unit testing explicitly by right clicking at your test class and choose “Test File”.

Summary

At the moment we configure the NetBeans IDE so that it can work with Maven. Not only preparation, but we also create a simple java project plus the unit testing. During these actions we will see the Maven behavior, e.g. downloading the missing resources. Especially we have looked our environment, the Local Repository.

Next I will introduce you about adding the other library to our project. I focus on our most famous one, the Apache Log4J. Please stay tuned as always.

About these ads

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

Follow

Get every new post delivered to your Inbox.

Join 119 other followers

%d bloggers like this: