Maven is a Build Automation tool, or in a more accurate way, a Project Automation Tool
This is a simple tutorial of how to buld a Java code using Maven. For this we will first create a Java code to display “Hello my world – Shiju Varghese!!”
Prerequisite:
- CentOS system with Java and Maven installed.
Let us create a folder named “/data/myProject” and navigate to the folder by using the command “cd /data/myProject”
We have to start this project by crating a “pom.xml” file for the specific project that will assist Maven to build the project. This will inherit other attributes from the super POM. Let us use the posted below code:
===========
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.shijuvarghese</groupId>
<artifactId>helloWorld</artifactId>
<version>1.0</version>
</project>
===========
Maven expects the java code in a specific folder. Therefor let us create the required folder using the command “mkdir -p src/main/java/com/shijuvarghese”
Let us go to this folder by using the command “cd /data/myProject/src/main/java/com/shijuvarghese”
Let us create a java file using an editor, with the file name as hworld.java. The content can be as follows:
=========
package com.shijuvarghese;
public class hworld {
public static void main (String[] args){
System.out.println (“Hello my world – Shiju Varghese!!”);
}
}
===========
Now come back to the folder having the pom.xml file and execute the command “mvn compile”
If all goes well without any error, you will see a line towards the end that read “[INFO] BUILD SUCCESS”
To run the java program, go to the classes folder by using the command “cd /data/myProject/target/classes” and type the command “java com.shijuvarghese.hworld“. The result will be a file that displays:
- Hello my world – Shiju Varghese!!
======== =====
Additional commands:
Clean and remove the class files and folders that was created by Maven : “mvn clean“