Apache Maven for Beginners

Apache Maven is a build tool widely being used by Java developers to manage project dependencies, control build process and automate tests. Apache Maven makes our life easier especially in building a complex Java project. However, beginners stay away from Apache Maven as I did years ago just because they find it complex to learn and use. This article simplifies the concept of Apache Maven and introduces Maven in a smooth way to beginners. In this article, you will see how you can use Apache Maven to manage your project dependencies using a simple Java project as an example. The article is structured into two main topics: Apache Maven in Eclipse and Apache Maven in IntelliJ IDEA. Of course, you can use Apache Maven without any IDEs. However, I stick with IDEs to make it simple for beginners. Other applications of Apache Maven like build management and test automation will be covered in another article.
Let's begin with manual dependency management using a simple calculator application. Suppose you want to develop a Calculator that receives a simple arithmetic expression like "2 + 3 * 5" as input and prints the output in the console. It is a complex task to evaluate such a String input and calculate the result by ourselves. Fortunately, there is a library: exp4j which can evaluate a String expression and return the output.

In the following sections, we are going to develop a Calculator using exp4j but the whole purpose of this project is just explaining how hard it is to manage dependencies manually and how Apache Maven makes it easy. I am using Eclipse to create this project but you can use any IDEs you are comfortable with. In the last section, I also show a step by step example to create a Maven project in IntelliJ IDEA. However, I recommend you to read the article from top to bottom in order to understand the concept. You can just read the article and grasp the idea or follow each and every step with me to get on-hand experience.

Step 1:
Create a new Java project named Calculator.


Step 2:
Right click on the Calculator project and create a new folder lib.

Step 3:
Visit the exp4j official website and download the latest Binary Jar file. At the time of writing this article, it is exp4j-0.4.8.jar.

Copy and paste the downloaded file into the lib folder.

Step 4:
Right click on the project and go to Properties. In the appeared dialog, go to Java Build Path → Libraries → Select Classpath → Click on Add JARs... button → Expand the project folder and select the exp4j-0.4.8.jar file → Click OK → Click Apply and Close.


Now, we have successfully added the exp4j library to our project.

Step 5:
Right click on the src folder and create a new package com.javahelps.calculator.

Step 6:
Right click on the package and create a new class Calculator with the main method. Modify the code as shown below to build an Expression from a String and to evaluate it using exp4j
package com.javahelps.calculator;

import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;

public class Calculator {

    public static void main(String[] args) {
        String exp = "2 + 3 * 5";

        Expression expression = new ExpressionBuilder(exp).build();
        double ans = expression.evaluate();
        System.out.println(ans);
    }
}


Running this code will print the result (17.0) of the given expression. The purpose of this project is not to explain how to evaluate an arithmetic expression but to demonstrate the pain in managing dependencies manually. As you have seen, we had to manually download and add the library to the project. A reasonably large Java project may require hundreds to thousands of libraries. In such a scenario, you have to download all the libraries one by one and add them to the project. After some time, if you want to upgrade a dependency to its latest version, you need to again download the latest JAR file and replace the existing JAR file. The next problem arises when you share your project with your team. Having plenty of JAR files as part of the project will significantly increase the size of your project. Pushing them to GitHub or copying them to pen drive is an unwanted overhead which we can avoid easily with Apache Maven.

Let's create another project with the same name but using Apache Maven for dependency management. You can delete the current project because we are going to create the same project again.

Apache Maven Project in Eclipse

Step 1:
Create a new Maven project in Eclipse using the groupId com.javahelps.calculator and artifactId calculator. The groupId of a project is a unique name for your organization or the team. Inside your organization or team (aka group), the artifactId should uniquely represent this specific project.


The pom.xml stands for Project Object Model is the configuration file used by Maven to setup your project. You can also notice that there are additional folders created by Maven for resources and unit tests.

Step 2:
In order to add a dependency, you must first visit the Maven Repository. This is a central point where you can find all the libraries supported by Maven and other build tools.


In the search box, enter the library name (exp4j in our case), search for it, choose the right library from the list of libraries matching your keyword, select the version you like to use in your project and copy the Maven declaration. The maven declaration of exp4j 0.4.8, is given below:
<!-- https://mvnrepository.com/artifact/net.objecthunter/exp4j -->
<dependency>
    <groupId>net.objecthunter</groupId>
    <artifactId>exp4j</artifactId>
    <version>0.4.8</version>
</dependency>

Step 3:
Open the pom.xml file in Eclipse and modify it as shown below.
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.javahelps.calculator</groupId>
    <artifactId>calculator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Calculator</name>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/net.objecthunter/exp4j -->
        <dependency>
            <groupId>net.objecthunter</groupId>
            <artifactId>exp4j</artifactId>
            <version>0.4.8</version>
        </dependency>
    </dependencies>
</project>

Notice the dependencies parent surrounding the dependency we have copied from the Maven Repository. A pom.xml can have only one  dependencies element and all your dependency declarations must be added inside that dependencies as children.


Once you have saved the changes, you should see the exp4j-0.4.8.jar file added under Maven Dependencies in your project. If not, right click on the project and select Maven → Update Project and click OK in the appeared dialog.


Step 4:
Right click on the src/main/java folder and create a new package com.javahelps.calculator.

Step 5:
Create a new class Calculator in that package with the following code and run it.

package com.javahelps.calculator;

import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;

public class Calculator {

    public static void main(String[] args) {
        String exp = "2 + 3 * 5";

        Expression expression = new ExpressionBuilder(exp).build();
        double ans = expression.evaluate();
        System.out.println(ans);
    }
}


If you want to share this project with someone else, you need to share only the project with pom.xml. They can import the project and update the project as explained in Step 3 to download the necessary dependencies in their computer. Suppose you want to upgrade or downgrade the version of the library, the only thing you need to do is changing the dependency version in the pom.xml and updating the project again.

This article won't be complete without explaining how to create a Maven project in the best IDE out there: IntelliJ IDEA. If you are an Eclipse user, you can skip the following section.

Apache Maven Project in IntelliJ IDEA

Step 1:
Create a new Maven project in IntelliJ IDEA using the groupId com.javahelps.calculator and artifactId calculator. The groupId of a project is a unique name for your organization or the team. Inside your organization or team (aka group), the artifactId should uniquely represent this specific project.


Step 2:
Follow Step 2 from the previous section and modify the pom.xml as shown 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.javahelps.calculator</groupId>
    <artifactId>calculator</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/net.objecthunter/exp4j -->
        <dependency>
            <groupId>net.objecthunter</groupId>
            <artifactId>exp4j</artifactId>
            <version>0.4.8</version>
        </dependency>
    </dependencies>
</project>


Once you have made the changes, IntelliJ will ask you to import the changes you made into the project. You can either Import Changes only this time (if you choose this option, you will be asked this question every time you modify the pom.xml) or you can ask IntelliJ IDEA to import changes automatically whenever you modify the pom.xml.

After importing changes, you should be able to see exp4j-0.4.8.jar file under the External Libraries section as shown below:


Step 3:
With step 2, we are done with Apache Maven. As usual, create a new package com.javahelps.calculator in the src/main/java folder.

Step 4:
Create a new class Calculator with the following code and run it.
package com.javahelps.calculator;

import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;

public class Calculator {

    public static void main(String[] args) {
        String exp = "2 + 3 * 5";

        Expression expression = new ExpressionBuilder(exp).build();
        double ans = expression.evaluate();
        System.out.println(ans);
    }
}


Congratulations! You have successfully used Apache Maven for dependency management. If you have any questions or suggestions, feel free to comment below. If you are curious to learn more about Apache Maven, please check the recommended books at the official website.
Previous
Next Post »

Contact Form

Name

Email *

Message *