Build and Deploy Maven/Java Project on Apache Tomcat with Jenkins CI CD Pipeline

Introduction

Apache Maven is a popular build automation and project management tool primarily used for Java projects. It provides a consistent and efficient way to manage project dependencies, build processes, and project documentation. Maven uses a declarative XML file called pom.xml (Project Object Model) to define the project structure, dependencies, plugins, and other project-related configurations.

Here are some key features and benefits of using Maven:

  • Dependency Management: Maven simplifies the management of project dependencies by automatically downloading and including the required libraries from remote repositories. It allows you to specify dependencies in the pom.xml file, and Maven takes care of fetching and resolving them.
  • Build Automation: Maven automates the build process, including compilation, testing, packaging, and deployment. It follows a convention-over-configuration approach, providing default build lifecycle phases and plugins for common tasks. This allows developers to focus more on writing code rather than dealing with complex build scripts.
  • Consistent Project Structure: Maven enforces a standard project structure, which makes it easier for developers to understand and navigate different Maven projects. By following the conventions, developers can quickly locate source code, resources, and test files in predefined directories.
  • Code Quality and Testing: Maven integrates with various testing frameworks and provides plugins for code quality checks, unit testing, and code coverage analysis. This helps in ensuring the quality and reliability of the codebase.
  • Project Reporting and Documentation: Maven generates project reports automatically, providing insights into various aspects of the project such as code metrics, test results, and more. It also supports the generation of project documentation, including Javadocs and other report formats.
  • Team Collaboration: Maven promotes collaboration among team members by standardizing the build process. All team members can use the same build tool with consistent configurations, ensuring that everyone is working on a common build environment.
  • Overall, Maven simplifies the build and management of Java projects, improves productivity, and enhances project maintainability by providing a structured and automated approach to software development.

Overall, Maven simplifies the build and management of Java projects, improves productivity, and enhances project maintainability by providing a structured and automated approach to software development.

System Requirements

The system requirements for Apache Maven are generally modest, as it is a lightweight tool that runs on top of the Java Virtual Machine (JVM). Here are the system requirements for using Maven:

  • Java Development Kit (JDK): Maven requires a compatible JDK to be installed on your system. Maven 3.3.9 and above require JDK 7 or higher. It is recommended to have the latest stable version of the JDK installed.
  • Operating System: Maven is designed to be platform-independent and can run on various operating systems, including Windows, macOS, and Linux. The specific operating system requirements are dictated by the JDK you are using.
  • Memory: The memory requirements for Maven depend on the size and complexity of your project. As a general guideline, allocating at least 512 MB of RAM to the JVM running Maven is recommended. For larger projects, or if you are performing memory-intensive tasks, you may need to allocate more memory.
  • Disk Space: Maven itself requires minimal disk space. However, the disk space requirements will vary depending on the size of your project and the number of dependencies you have. Maven downloads and caches project dependencies in the local repository, so you should ensure that you have enough disk space to accommodate your project’s dependencies.
  • Network Connectivity: Maven requires internet access to download dependencies from remote repositories, such as the Maven Central Repository. Ensure that your system has a working internet connection so that Maven can resolve and download the required dependencies.
  • Command Line Interface (CLI): Maven can be used through the command line interface, so a terminal or command prompt is needed to execute Maven commands.

In this post, We will show you how to create CI CD pipeline to build, test and deploy the JAVA project to the Apache tomcat server using Jenkins automation server.

Step 1: Installing Maven Plugin In Jenkins

We need o login Jenkins portal and click on Manage Jenkins button To install the Maven plugin in Jenkins, you can follow these steps:

Here we should get the Plugins section click on it.

Now we need to click on Available plugins button left menu options.

In this page we need to search maven and after that we need to install Maven Integration plugin.

Click on Install without restart or you can also choose Download now and install after restart.

After successful installation we should get success message for Maven Integration plugin.

Step 2: Configure Maven in Jenkins

Once installed maven integration plugins we need to configure the maven installation in tools section, Go to the Dashboard > Manage Jenkins > Tools and search the Maven sub section like this.

Here we need to define the name which we can put maven along with version and we need to also select the install automatically maven version from the drop down menu and in future we can also add and update the maven version here as per our need.

Once we put the maven name and version we are goo to click on Apply and Save button.

Step 3: Configure GitHub Creds with Jenkins

By following given link we can Creating GitHub’s personal access token for Jenkin step by step.

Now you should have GitHub account username and token, Now we need to configure in Jenkins’s credentials option like this.

Go to the Dashboard > Manage Jenkins > Credentials > System > Global credentials (unrestricted)

Save your username and token from GitHub account in order clone the repository, Here our GitHub Creds ID will be github.

After create the Jenkins creds with GitHub account using token, We should get status like this.

Step 4: Installing SSH Agent Plugin

Now we need to install the SSH Agent plugin in order o copy the war file from Jenkins workspace to tomcat’s webapps path.

Go to the Dashboard > Manage Jenkins > Plugins like before, Search in Available plugins “SSH Agent

Once you get this, You need to select and click on Install without restart button.

After installation, We should get success message like this.

Create SSH’s creds using Jenkins credential option, SSH Agent compatible with PEM (Private SSH key ) based authentication.

Go to the Dashboard > Manage Jenkins > Credentials > System > Global credentials (unrestricted)

and create the SSH creds using PEM file like this, We will this creds to transfer the war file to tomcat server in later Jenkins pipeline.

After creating the creds we should get out put like this.

Step 5: Creating CI CD Pipeline

We are good to create a Jenkins CI CD pipeline with any name like BuildandDeploy for this we need to click on Create a job from Jenkins dashboard.

Here we need to define the job name like BuildandDeploy.

and then we need to select the type, In my case i am going to use the pipeline.

Now go to the end the of page, You should get blank pipeline script editor like this.

Here we need to copy and paste the following pipeline script that having Git clone, Maven build, Maven test and SCP command to copy the war file after the build process to tomcat server.

pipeline {
    agent any
    tools {
         maven 'maven3.9.2'
    }

    stages {
        stage('Checkout') {
            steps {
                git credentialsId: 'github', url: 'https://github.com/aftab70/maven.git'
            }
        }
        stage('Build') {
            steps {
                sh 'mvn package'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }        
        stage('Deploy') {
            steps {
                sshagent(credentials: ['tomcat']) {
                    sh 'scp -oStrictHostKeyChecking=no target/*.war USERNAME@IPADDRESS:TYPE_PATH'
                }
            }
        }       
    }
}

We need to replace the GitHub id, SSH agent creds id along with tomcat server IP address and webapps path and all good, We are good to apply and save this job to run it.

After save this job we need to click on Build now button from left side menu after this we should get our job up and running state like this.

Here we can see that our all the stage got completed now.

Open the Apache tomcat server IP with port number and add the project name to deployment, Like in my case I am having profile page like this.

Conclusion

We have done successful created CI CD pipeline to build and deploy Maven project to the tomcat server, Still you are having any issue, So please leave a comment below.

Author

Build and Deploy Maven/Java Project on Apache Tomcat with Jenkins CI CD Pipeline

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top