Create Java Project with Maven

In this blog post, I am going to share with you how to generate a Java project with Maven. You will also learn how to package your project with all the dependent libraries into a single jar file and run your Java application via the command line in the terminal window.

Step 1: Install Maven

You can download the latest version of Maven from the Apache Maven website (https://maven.apache.org/download.cgi) and follow the installation instructions for your operating system. You can also follow one of my earlier tutorials that teaches how to install Maven on MacOS.

Step 2: Create a new directory

Now that you have Maven installed and working on your computer open a terminal window and navigate to the directory where you want to create your project. Create a new directory for your project with the following command:

mkdir my-java-project

Step 3: Change into a new directory

The next step is to change your current directory into a new directory you have just created.

cd my-java-project

Step 4: Generate Java Project from Maven Archetype

To generate java project from Maven template we will use following command:

mvn archetype:generate 
   -DgroupId={project-package}
   -DartifactId={project-name}
   -DarchetypeArtifactId=maven-archetype-quickstart
   -DinteractiveMode=false

where

  • {project-package} will need to be replaced with the desired package name for your project, and
  • {project-name} will need to be replaced with the name of your project.

For example, I will use the below command to generate a new project with name SendHttpRequestExample and package com.appdeveloperblog. 

  1. Open terminal window on your computer and navigate to a folder where you want to create your new java project.
  2. Run the below command to generate new java project in selected folder:
mvn archetype:generate -DgroupId=com.appdeveloperblog -DartifactId=SendHttpRequestExample -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This will create a very simple java project which can be ran via the terminal window and opened in your favourite Java Integrated Development Environment for further improvement.

To find the Java file which contains public static void main(String[] args) function, open your project and navigate to /src/main/java/. Inside of java folder you will see your package folders. For example, in my case it will see folder com inside of which will be folder appsdeveloperblog and thus the complete path to my main file will be /src/main/java/appsdeveloperblog/App.java

Step 5: Add Java Libraries with Maven

Next to the /src folder in your project you will find pom.xml. This is the file you will need to use to add additional java libraries your project depends on. For example, let’s assume that our project needs to use Apache HttpClient. So, to add Apache HttpClient to my project, I will need to add the following maven dependency to pom.xml file.

  1. Open pom.xml file in your favourite text editor,
  2. Right after the closing </dependency> element and before the closing  </dependencies> element, add the below xml elements:
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>

This will instruct maven to download Apache Http Components and make the HttpClient available to your project.

I will also edit my App.java file which is inside of /src/main/java/appsdeveloperblog package by adding a simple print statement like so:

public class App {

    public static void main(String[] args) {
    
        System.out.println(“Hello world”);
       
    }
}

This will print Hello world when I run my project. And it is time now to compile, package and run our application.

Step 6: Package Java Project into Jar with Maven

To make Maven package your project and all it’s dependencies into single Java Jar file do the following:

  1. Open pom.xml,
  2. Right before the closing </project> xml element add the following:
<build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.appdeveloperblog.App</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>  
                        <phase>package</phase>  
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

3. Make sure to replace the value in <mainClass> element with the complete value of your java class which contains main() function. In the example above the main function is in App java class which is in com.appdeveloperblog package.

This will make my complete pom.xml file look like this:

<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>com.appdeveloperblog</groupId>
    <artifactId>SendHttpRequestExample</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SendHttpRequestExample</name>
    <url>http://maven.apache.org</url>
    
    
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
    </dependencies>
   
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.appdeveloperblog.App</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>  
                        <phase>package</phase>  
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build> 
    
</project>

Once you done updating your pom.xml file, run the following command in your terminal window to compile your project and package your java project into a single java jar file which can be ran via the command line in terminal window.

  1. In your terminal window, navigate to your project
  2. Run mvn package to build and package the project
mvn package

mvn package will compile and package your project into a single jar archive and will place in a folder called target which is right next to you src folder. When I run mvn package I get a new file SendHttpRequestExample-1.0-SNAPSHOT-jar-with-dependencies.jar generated and copied into target folder.

Step 7: Run Java Jar File using Command Line

To run your java project using command line:

  1. In your terminal window, navigate to your project
  2. Run the following command:
java -jar target/SendHttpRequestExample-1.0-SNAPSHOT-jar-with-dependencies.jar

where SendHttpRequestExample-1.0-SNAPSHOT-jar-with-dependencies.jar should be replaced with the file name generated in your target folder.

I hope this tutorial was helpful to you. If you are interested in Java, check out other Java tutorials that I have.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *