Create Java Project with Maven

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

This tutorial assumes you have installed maven on your computer. If you do not, please follow this list to download and install Maven https://maven.apache.org

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

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, I will add Apache HttpClient to my project.

  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 edit my App.java file which is inside of /src/main/java/appsdeveloperblog 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.

Package Java Project into Single 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 install to build and package the project
mvn install

mvn install 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 install I get a new file SendHttpRequestExample-1.0-SNAPSHOT-jar-with-dependencies.jar generated and copied into target folder.

Execute Java Jar File in via Command Line in Terminal Window

To run your java project via the 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 short blog post was of some value to you :).

To learn more about Maven check out these video courses:

Maven Crash Course: Step-by-Step Introduction for Beginners

Maven Crash Course
icon

Java Maven:101 Stop Building Java Programs the Hard Way!

Java Maven:101
icon

Maven Tutorial – Manage Java Dependencies in 20 Steps

Maven Tutorial - Manage Java Dependencies
icon