Introduction to Apache Maven

In this post you will learn about Apache Maven. What is Maven? In simple words, Maven is a tool that we can use to build and manage a Java based project. Compare to the older type of build tool such as Ant, which is also an Apache project, Maven gives developers a standard way to build projects, a clear definition of the projects consisted of, an easy way to publish project information and an easy management of the project libraries dependency.

In Maven, the project libraries (Jars) will be maintained in the Maven repository and can be shared among projects. We don’t need to include the Jars in the project’s source code, as what we usually do when using tool like Ant. And here are the main objectives of the Apache Maven project:

  • Making the build process easy.
  • Providing a uniform build system.
  • Providing quality project information.
  • Providing guidelines for best practices development.
  • Allowing transparent migration to a new features.

After you know a little about Maven, let’s continue to the next step, installing the Apache Maven.

Step 1. Downloading Apache Maven

  • Go to Apache Maven and from the Get Maven section on the left sidebar click Download link.
  • Download the appropriate installer for your platform. For example, I am downloading the .zip file. The latest version when this post is written is Maven 3.1.0 (apache-maven-3.1.0-bin.zip).
  • Extract the downloaded zip file. For example, I am extracting it into D:\Toolbox\apache-maven-3.1.0 directory.

Step 2. Configuring Environment Variables

  • After extracting Maven distribution you have to define M2_HOME environment variable. The value of this variable is the path to your Maven installation.
  • You can create environment variable using the following steps:
    • Right click on My Computer.
    • Select Properties menu.
    • Select Advanced system settings.
    • In the System Properties window select the Advanced tab and click the Environment Variables button.
    • Add the variable in the System variables section.
    • Click OK to proceed.
  • Add the %M2_HOME%\bin; into your PATH environment variable so that you can execute Maven command from any path inside your command prompt. This PATH variable can also be updated in the System variables section.

  • Open your command prompt and type echo %M2_HOME%. Pressing Enter and you should see the value of the variable printed on the console. For example:
D:\>echo %M2_HOME%
D:\Toolbox\apache-maven-3.1.0
  • To check if Maven also in your Path variable you can execute echo %PATH% in your command prompt. You should see the path to Maven binary in the Path variable.
D:\>echo %PATH%
D:\Toolbox\apache-maven-3.1.0\bin;C:\Program Files (x86)\Java\jdk1.7.0_13\bin;

Step 3. Running Maven

  • Now you have Maven installed and configured. Let’s run Maven for the first time.
  • To run Maven we use the mvn command.
  • For example to check the version of the Maven we run mvn -version
  • You will see the following output, more or less, it depends on the JDK version you have in your machine.
D:\>mvn -version
Apache Maven 3.1.0 (893ca28a1da9d5f51ac03827af98bb730128f9f2; 2013-06-28 10:15:32+0800)
Maven home: D:\Toolbox\apache-maven-3.1.0
Java version: 1.7.0_13, vendor: Oracle Corporation
Java home: C:Program Files (x86)\Java\jdk1.7.0_13\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"

Congratulations! You’ve installed Apache Maven successfully on your development machine. On the next post I will show you how to create a new project using Maven.

How do I get file separator symbol?

Creating a program to be run on more than one platform such as Windows and Linux our program need to understand the difference between both platform. The simplest thing for instance is the file separator. Windows use "\" (backslash) while Linux use "/" (forward slash).

To avoid manual checking for the operating system we can get the file separator symbol from the system property using the file.separator key.

package org.kodejava.lang;

public class FileSeparatorExample {
    public static void main(String[] args) {
        // file.separator system property return the correct file 
        // separator for each different platform (Windows = \), 
        // (Linux = /)
        String dataFolder = System.getProperty("user.dir") +
                System.getProperty("file.separator") + "data";

        System.out.println("Data Folder = " + dataFolder);
    }
}

How do I read and write data in Windows registry?

The java.util.prefs package provides a way for applications to store and retrieve user and system preferences and data configuration. These preference data will be stored persistently in an implementation-dependent backing stored. For example in Windows operating system in will stored in Windows registry.

To write and read these data we use the java.util.prefs.Preferences class. The following example will show you how to read and write to the HKCU (HKEY_CURRENT_USER) in the registry.

package org.kodejava.util.prefs;

import java.util.prefs.Preferences;

public class RegistryDemo {
    public static final String PREF_KEY = "kodejava";
    public static void main(String[] args) {
        // Write Preferences information to HKCU (HKEY_CURRENT_USER),
        // HKCU\SOFTWARE\JavaSoft\Prefs
        Preferences userPref = Preferences.userRoot();
        userPref.put(PREF_KEY, "https://kodejava.org");

        // Below we read back the value we've written in the code above.
        System.out.println("Preferences = "
                + userPref.get(PREF_KEY, PREF_KEY + " was not found."));
    }
}