Building Java Applications using Maven
- JDK comes with in built java compiler, which can be used to create the java compiled classes
- Build tools in Java help in building the jar/war files from the java code
- In this we would also like to run unit tests
- There was a popular tool called as Ant to perform these operations.
- In Ant we are supposed to write configuration files and in the configuration files we need to define the sequence of instructions that needs to be carried out.
- The instructions are generally written in build.xml file Refer Here for the sample
- Refer Here for the sample java project with ant
- When we write the java code, we use lot of code which is already. This is something which we call as dependency
- So now in this day , we need build tools to
- build the java code
- unit test the java code
- manage the dependencies.
Maven
- Maven is a powerful build tool for Java Projects (Java Based Languages).
- Maven believes in Convention over Configuration
- Refer Here for the website of maven
- Lets use maven to build some java code
- Lab Setup
- System 1: Create an ubuntu linux instance in any cloud/hypervisors and ensure it has internet connection
- Install Open JDK8
sudo apt update sudo apt install openjdk-8-jdk -y- JAVA is installed on the folder /usr/lib/jvm/java-8-openjdk-amd64 which is considered as JAVA HOME and we need to create an environment variable JAVA_HOME representing home directory of java
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64- Add the JAVA_HOME to either /etc/environment or ~/.bashrc
- Download the latest version of maven
cd /tmp wget https://mirrors.estointernet.in/apache/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz tar -xvzf apache-maven-3.8.1-bin.tar.gz sudo mv apache-maven-3.8.1 /var/lib/maven-3.8.1- Now we need to create M2_HOME environment variable which point to /var/lib/maven-3.8.1 and add /var/lib/maven-3.8.1/bin to path
- Open ~/.bashrc and add the following lines to the end of the file
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64" export M2_HOME="/var/lib/maven-3.8.1" export PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin- Now logout and login
mvn --version- Can we install maven latest version with JDK 11
- System 1: Create an ubuntu linux instance in any cloud/hypervisors and ensure it has internet connection
- On windows to install java and maven . First install chocolatey Refer Here
choco install jdk11 -y
choco install maven -y
-
On Mac install homebrew Refer Here
- Java Refer Here
- maven Refer Here
-
Conventions over configurations
| Item | Default |
|---|---|
| source code | ${baseDir}/src/main/java |
| Resources | ${baseDir}/src/main/resources |
| Tests | ${baseDir}/src/test |
| Compile java classes | ${baseDir}/target/classes |
| Jar/War file | ${baseDir}/target |
Maven POM
-
POM stands for Project Object Model.
-
This is fundamental unit of work in Maven
-
This contains information about the project and various configuration details used by Maven
-
Goals:
- compile: will compile the java code
- test: will compile and run the test code
- package: will test and create the jar/war file
- install: will package and copy the package with pom file in local repository
- deploy: will install and deploy the package to maven repository
- clean : will remove the target folder
-
In POM we will have the following information
- project dependencies
- plugins
- goals
- build profiles
- project version
-
Elements of POM
| Element | Description |
|---|---|
| modelVersion | refers to the schema version of maven and is generally 4.0.0.0 |
| groupId | This refers to the project which you are trying |
| artifactId | This refers to id of the project |
| version | This refers to Version of the project if version has SNAPSHOT it means the project is still under development |
- The build package will have the following format
<artifactId>-<version>.<packagingformat>
- Super POM:
- This is Maven’s default POM. All POMs inherit from this parent or default POM
- Try executing
mvn help:effective-pom - What are Repositories in maven?
- What is Build life cycle in maven
- What are Build profiles
- What are Plugins
Jenkins contd
-
From Jenkins lets build a java project and show the jar files to user
-
Execute mvn package from Execute shell after configuring git repository of java project Refer Here
-
Now lets explore how jenkins is storing the projects
-
cd in to the home directory of jenkins (/var/lib/jenkins) as jenkins user and see the contents

-
Now get into jobs/your job name

-
The jenkins project created gets stored as xml file in /var/lib/jenkins/jobs/$job-name/config.xml
-
Now lets cd into /var/lib/jenkins/workspace

-
Now lets cd into firstbuild

-
Jenkins when a project is created stores an xml file in the
$JENKINS_HOME/jobs/<your-job-name>/config.xmland when the project is executed all the work is done in the workspace folder$JENKINS_HOME/workspace/<your-job-name>/
