Apache Maven Build Tool for Java Source Code

Michael Mensah
5 min readFeb 24, 2022

--

This project will focus on using maven as a build tool to build Java source code projects to produce output (Artifacts — .war or .jar). The end result of the build process is to see either .war or .jar file extension for webserver deployment. Maven is based on Project Object Model or POM which is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project.

Goals for this project:

  1. Download and Install Apache Maven
  2. Install JDK
  3. Maven Build Process
  4. Artifact Type

One — Download and install Apache Maven

Since the download will be done in the EC2 server copy the link from the maven website. Is always advisable to keep any 3rd party application in the /opt location. Copy the link to the terminal with the command — wget https://dlcdn.apache.org/maven/maven-3/3.8.4/binaries/apache-maven-3.8.4-bin.zip

Unzip the folder with the command— unzip apache-maven-3.8.4-bin.zip

Folder unzipped and apache-maven-3.8.4 file is extracted

After extraction is done. Be sure to work in the apach-maven-3.8.4 present working directory.

In the apach-maven-3.8.4 pwd. The Bin dir is going to be our present working directory to configure the maven tool.

The next step is to know what version of mvn but before that Java Development Kit (JDK) needs to be installed. Am going to be doing a yum search the JDK in my Linux repo. Command is yum search Java

yum install java-1.8.0 openjdk-devel -y.

Run the command Java— version to check to see if Open JDK was install successfully and the version of Java the install came with.

Now that JDK is successfully installed. We can run the ./mvn — — version to see the configuration status.

Apache Maven 3.8.4 — Maven home :/opt/apache-maven-3.8.4 — Java version — 1.8.0.3.2

At this point since we have done all the necessary configuration with respect to Maven and JDK. The next step is to build our source code. Before that I will install git to pull my java code from bitbucket repo. The command for git installation is yum install git -y

Now that Git is installed am going to add my java code to my opt location as a working copy by doing a git clone from my remote repo (Bitbucket) to pull the java code in my bitbucket I will copy the git clone link and paste it in the opt location to PULL code remote repo. As you can see all the codes have been pulled and ready for the build process. Always know that if you see pom.xml and src source then it is the a maven project.

Now that Java source code have been pulled the build process can begin. I am going to run the mvn package command to begin the build process. Since my pwd is in the java source code location. I am going to run the command /opt/apache-maven-3.8.4/bin/mvn package.

My build process FAILED due to some plugins.

After removing code and git cloning code again to the server and running the mvn package the BUILD was SUCCESSFULLY.

Once the Build Process is done. Go to the Java source code directory. You will see a folder called Target as part of the build process files. Inside the target folder is where the build process artifacts is available.

To see what is inside the target folder the command is cd target/ and the the next command is ls -ls to see what inside the target folder. As you can see in my target folder is my artifact with .war extension(dptweb-1.0.war) after the build process was SUCCESSFULL.

In conclusion — Since the source code is built, compiled and packaged into a .war file extension. I need the .war extension to deploy into my webserver to run the Java application. This makes it easy to manage the .war file alone instead of managing an entire source code in the deployment face or transitioning them into different stages for deployment.

Thank you for stopping by to read my mini project with MensahCloud.

--

--