DevOps

Designing a Deployment Pipeline: From Build to Release using Maven 

Maven Blogs

Introduction

The Apache Group created the build tool Maven, which is used to create, publish, and deploy numerous projects. Based on the idea of a project object model (POM). Maven allows developers to create and document the lifecycle framework while providing a standardized way to manage project dependencies, build processes, project structure, and Deployment Pipeline. 

In this blog, we will discuss designing and deploying from build to release using Maven. 

How Java Build Tool Used in Designing a Deployment Pipeline? 

A Java build tool is a program or command-line utility that automates the operations of compiling, assembling, and deploying software. It is frequently used to automate activities like creating an application binary code from source code.

They are not only capable of aiding in code compilation; they may also assist with package administration, handling of dependencies, and packaging produced code into ZIP or JAR files. The packed code can be installed in a local, server, or central repository as well.

Types Of Java Build Tool

Following is the list of Open source Java Build Tools:

  • Apache Maven
  • Ant with ivy
  • Gradle
  • SBT

We will have to work more manually if we don’t automate our build procedures. Applications should be built, packaged, and delivered automatically using the build tool.

How To Download And Install The Maven?

Pre-requisite:

  • To use the maven the Java JDK must be installed in the machine.
  • To check the Java version, open the command prompt, and type the command java –version.
  • In the below image, we can see the Java version, here we used open JDK version 18.9
Download and install Maven

Installation:

Step 1: Click here to download the maven https://maven.apache.org/download.cgi

Step 2: After downloading the maven, after extracting the downloaded zip folder you can see the maven folder. It is necessary to set the path in environment variables to use the maven.

Maven folder

Step 3: cop the path –  C:\Program Files\apache-maven-3.5.2\bin

Step 4: The given path will be set into environment variables.

Maven deployment path

Step 5: After setting the maven path in environment variables check whether the maven is successfully installed or not.

To check the version of maven, open the command prompt and type command mvn -v.

Functionality Of Maven?

  • Using Maven, we can quickly create a project.
  • With the aid of Maven, we can add jars and other project dependencies quickly.
  • Maven offers project data (such as log documents, dependency lists, findings from unit tests, etc.).
  • When upgrading the central repository of JARs and other dependencies, Maven is highly beneficial to a project.
  • Without using any scripting, we can use Maven to create any number of projects into output formats like JAR, WAR, etc.
  • We can quickly connect our project with a source control system (like Subversion or Git) by using Maven.
  • Maven also aids in controlling the project’s build lifecycle, which entails activities like code testing, compilation, packaging, and deployment.
  • Developers may easily create projects thanks to Maven’s common project structure.

How Maven Works?

Core Concepts Of Maven?

  • POM Files – Project Object Model(POM) Files are XML files that include data about the project and configuration data used by Maven to construct the project, such as dependencies, source directories, plugins, objectives, etc. In order to complete its configuration and activities, Maven reads the pom.xml file when we run a Maven command.
  • Dependencies and Repositories – Dependencies are outside Java libraries needed for the project, while repositories are folders of bundled JAR files. On your computer’s hard disk, the local repository is only a directory. If the dependencies can’t be discovered locally, Maven downloads them from a central repository and adds them to your local repository.
  • Develop Life Cycles, Phases, and Goals –A Build Lifecycle is made up of several phases 

Types Of Repositories and How It Works?

  1. Local repository- A developer’s computer’s local repository is a directory. All of the dependencies that Maven obtained are present in this repository. Even though several projects depend on the same dependencies, Maven only needs to download them once.
  2. Central repository: The Maven community established the central repository. Any dependencies required but not present in your local repository are searched for by Maven in this central repository. The dependencies are subsequently downloaded into your local repository by Maven. Using this URL, you may see the central repository.
  3. Remote repository– Maven may obtain dependencies from a remote repository located on a web server. It is used to host internal organization initiatives. The dependencies are subsequently downloaded into your local repository by Maven.

What is a Maven POM File?

A POM file exists for each Maven project. The root directory of your project should contain the POM file, which is called pom.xml. There will be one POM file for the parent project and one POM file for each subproject in a project that has been broken up into smaller projects.

With this framework, the entire project may be developed in a single step or any individual subproject can be built independently.

The resources of the project are described in an XML file called a Maven POM file (Project Object Model). This comprises the folders that the source code, test source, and other files are stored in, as well as any external dependencies (JAR files) that your projects have.

The POM file specifies what to construct, but it lacks knowledge. It is up to you how to build.

The pom.xml file structure is given below :

Every element in the pom.xml file is described as follows: 

  • Project – It is the root element of pom.xml file. 
  • Model version – It is the sub-element of the project. It specifies the model version. It should be set to 4.0.0. 
  • GroupId – It is the sub-element of the project. It specifies the id for the project group. 
  • ArtifactId – It is the sub-element of the project. It specifies the ID for the artifact (project). An artifact is something that is either produced or used by a project. Examples of artifacts produced by Maven for a project include JARs, source and binary distributions, and WARs.
  • Version – It is the sub-element of the project. It specifies the version of the artifact under a given group. 

Other Elements of Pom.xml file:

  • Dependencies – The dependencies element is used to define a list of dependencies of the project. 
  • Dependency – Dependency defines a dependency and is used inside the dependencies tag. Each dependency is described by its groupId, artifactId, and version. 
  • Name – This element is used to give a name to the maven project. 
  • Scope- This element is used to define the scope for a maven project that can be compiled, runtime, test, provided system, etc. 
  • Packaging – The packaging element is used to package our project to output types like JAR, WAR, etc. 

Advantages Of Maven

  • By scanning the pom file, Maven can automatically add all the dependencies needed for the project.
  • Maven makes it simple to create a project in the desired format, such as a jar, war, etc.
  • Maven makes it simple to launch projects in many contexts and eliminates the need to handle dependency injection, builds, processing, etc.
  • A new dependence may be added extremely quickly. Just the dependent code has to be written in the pom file.
  • Maven requires a maven installation on the computer for it to function, as well as a maven plugin for the IDE.
  • One cannot add a dependent using maven if the maven code for an existing dependency is not available.
  • When the project’s dependencies are many. Maven may then be used to manage such dependencies with ease.

The Description Of The Maven Deployment Pipeline:

Above we have seen how to build the code by using Maven and its configuration. The following set of actions constitutes a typical Maven deployment procedure in a real-time project:

  • Submit the code for storage.
  • From the repository, get the source code.
  • From the repository, get the source code.
  • Find the created war/jar in a shared network location that is widely used.
  • Install the target server with the jar/war file that was downloaded.
  • The application’s versioning information and documentation also need to be updated.

Since many teams will always be working on the aforementioned activities in a real-time project, it is possible for one step to be overlooked, which would cause the build and deployment process to fail.

Also, it is observed practically that manually handling the above steps will be error prone most of the times. To overcome this issue, the deployment process should be automated so there will not be any intervention and also the deployment is successful.

How Can The Deployment Process Be Automated Using Maven?

Basically, the release plugin of the maven will be utilized to automate the build and release process. Open the project’s pom.xml and make the following updates:

Look closely at the pom.xml and note the following set of crucial tags that have used:

  • scm – Configures the scm (in this case SVN is used) of the project.
  • repositories – Storage location of the WAR/EAR/JAR file after the successful build.
  • plugin – maven-release-plugin to automate the process.

What Is The Functionality Of The Maven-Release-Plugin During Deployment?

It is crucial to comprehend what precisely occurs in the background to make the deployment process so automated. The following tasks are carried out in accordance with the invocation of the maven-release-plugin by maven:

  • mvn release:clean – cleans up the workspace for the last build and sets up for a fresh build.
  • mvn release:rollback – if the last process was unsuccessful, it reverts/rollback the workspace.
  • mvn release:prepare – This tasks performs the below list of operations:
  • Checks out for any uncommitted files in the local workspace.
  • Checks and ensures for no SNAPSHOT dependencies.
  • Prepares the Release version.
  • Updates the pom to SCM (SVN/Git/Mercurial/CVS)
  • Executes the test cases.
  • Performs the final commit to the SCM.
  • Tags the code.
  • Increments the version no. and adds the SNAPSHOT as part of future releases.
  • mvn release:perform – checks out the code from the repository and runs the maven goal to build and deploy the artifacts to the repository.

How Deployement Is Done Using Maven With The Eclipse IDE

Maven also offers a top-notch plugin that makes it easier to combine maven with eclipse. Maven and Eclipse are combined via the M2eclipse plugin. Some of the main advantages of this plugin are listed below:

  • Launching maven builds from the eclipse.
  • Launching maven goals as well.
  • Managing the dependencies for eclipse build path.
  • Automatic download of maven dependencies from the repository.
  • GUI based (wizards based) for creating maven projects.
  • Quick fix for the required dependencies.

Conclusion:

Maven offers a robust framework for designing deployment pipelines, facilitating efficient software delivery from build to release. With its automation capabilities and extensive plugin support, Maven streamlines the development process, promoting faster, more reliable deployments. By embracing Maven’s features, teams can enhance collaboration, ensure code quality, and adopt continuous integration practices seamlessly.

In the next blog, we will talk about how deployment is done with other building tools. Stay tuned!