Building a Java Program with Apache Maven

Follow the steps below to create, package, and run a simple Java application using Maven, a free automated build tool for Java maintained by the Apache Software Foundation. These instructions assume the computer is running a Microsoft Windows operating system. The code within these instructions is based on samples found at the URL “”.

1. If you have not already done so, download and install Java and a Java Development Kit.

2. In any convenient location, create a new directory named “MavenTest”.

3. Download Maven. As of this writing, the latest download is available at the URL “”. Navigate to that page in a web browser, click the “Binary zip archive” link, and download the file to the newly created MavenTest directory.

4. Inside the MavenTest directory, use an extraction utility to extract the contents of the downloaded Maven archive file. Once the file is extracted, locate the directory containing the file named “mvn.cmd” within the directory structure. For this example, the file was located in the directory “apache-maven-3.3.9-bin/apache-maven-3.3.9/bin”.

5. Create a new text file in the MavenTest directory named “MavenTest-Generate.bat”, containing the text shown below. If a different version of Maven than 3.3.9 is being used, it will be necessary to adjust the command accordingly.

"apache-maven-3.3.9-bin/apache-maven-3.3.9/bin/mvn" archetype:generate -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

6. Run the script file created in the previous step. If this is the first time Maven has run on the system, it may take a few moments to download several files from the internet. It will then create a new directory within the MavenTest folder, named “my-app”. Within this inner directory Maven will create a tree structure of new files and directories.

7. Navigate through the newly created my-app directory to locate the file For this example, the file is located in the “my-app/src/main/java/com/mycompany/app” directory. By default, the file contains the following text:


 * Hello world!
public class App 
    public static void main( String[] args )
        System.out.println( "Hello World!" );

8. Back in the “my-app” directory, create a new text file named “MavenTest-Package.bat”, containing the text below, and run it. The Maven project will be built and packaged.

"../apache-maven-3.3.9-bin/apache-maven-3.3.9/bin/mvn" package

9. Still in the “my-app” directory, create a new text file named “MavenTest-Run.bat”, containing the text below, and run it. The application will execute, and the text “Hello World!” will be displayed.

java -cp target/my-app-1.0-SNAPSHOT.jar
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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