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.
- Open terminal or command prompt in the preferred directory.
- Run the following command. This will generate a project from an existing template.
mvn archetype:generateIt 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.
- 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.
- 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:
Choose a number: 6:
- Next step will be to define
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.
- Voila! We are done. A structure of the project has been created. Now if you traverse through your project folder, you will find
App.javafile containing the java code for printing ‘Hello World’.
Compiling you project
- Open terminal or command prompt in the project directory.
- Run command
mvn compileto compile your project. This will download all the dependencies and plugins required for the project running.
- 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.