Getting started with Apache Maven : Hello World

This article will serve as a guide for running your first program with Apache Maven.

Maven is a software management and comprehension tool based on the concept of Project Object Model(POM). It can manage project build, reporting and documentation, from a central piece of information. Basically, this tool can be used as both, a build tool (just like ANT tool), as well as a project management tool. However, in this article we will use it just for building our project.

Want to know more about Apache Maven? Follow this article.

Creating first project using Apache Maven

One of the goals of this framework is to make the whole development cycle easy. Therefore, we need not create anything from scratch. Maven creates project structures for us using existing templates.

  1. Open terminal or command prompt in the preferred directory.
  2. Run the following command. This will generate a project from an existing template.
    mvn archetype:generate 
    It will download all the required plugins and archetype for the project. Though, maven first checks the local directory for the plugins, and only if they are not found, it downloads them. The structure created is simple and best for the beginners.
  3. It will download all the required plugins and archetype for the project. Though, maven first checks the local directory for the plugins, and only if they are not found, it downloads them. The structure created is simple and best for the beginners.
  4. After the download is complete, it will ask to choose a number which refers to an archetype. Remember, archetype is a model of how you want the structure of your project. By default the archetype number chosen is for ‘Hello World’ project. 
    Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 990: 
    After this, maven will ask you to choose a version of the archetype.
    Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
    1: 1.0-alpha-1
    2: 1.0-alpha-2
    3: 1.0-alpha-3
    4: 1.0-alpha-4
    5: 1.0
    6: 1.1
    Choose a number: 6:
  5. Next step will be to define groupId, artifactId and version. Understand that groupId is analogous to package name, artifactId is analogous to class name, and version is the version of the app you are creating. 
     Define value for property ‘groupId’: com.test.app 
     Define value for property ‘artifactId’: myapp 
     Define value for property ‘version’ 1.0-SNAPSHOT: : 
     Define value for property ‘package’ com.test.app: :
     
    You can enter the values as you desire, but with the same format.
  6. Voila! We are done. A structure of the project has been created. Now if you traverse through your project folder, you will find src folder with App.java file containing the java code for printing ‘Hello World’.

Compiling you project

  1. Open terminal or command prompt in the project directory.
  2. Run command mvn compile to compile your project. This will download all the dependencies and plugins required for the project running.
  3. Run command mvn package . This will package everything into a JAR file. This might download more plugins.

Execute the compiled project

Run following command to execute the project.

java -cp target/myapp-1.0-SNAPSHOT.jar com.test.app.myapp

This will display output on the terminal or command prompt.