Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Deployment Process
3.
Setting Up Deployment in Maven
3.1.
Basic Setup
3.1.1.
Distribution Management
3.1.2.
Maven Assembly Plugin
3.2.
Adding Source Control
3.3.
Adding Maven Release Plugin
4.
Running Automated Deployment
5.
Frequently Asked Questions
5.1.
Can deployment be done manually in Maven projects?
5.2.
Do we require a specific IDE to perform deployment automation?
5.3.
Are only JAR files supported?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Maven Deployment Automation

Author Satvik Gupta
1 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

MVN is a software project management tool. It helps us automate tasks such as testing, compiling, packaging, etc. Deployment is an important step in software development. It involves creating an output file from our source code and publishing it as a release. 

While manual deployments can be time-consuming, complex, and prone to error, It's an easy process with maven automation. There are several benefits of automating your deployments with a single click. Let us see how we can do this using Maven.

Maven Deployment Automation

Deployment Process

The deployment process generally consists of the following steps: 

Step 1: Pushing new code into the source control (such as Git).
 

Step 2: Download any new changes from online repositories (such as GitHub).
 

Step 3: Update versions in the source code. Change the version from a development version to a release version.
 

Step 4: Compile the code, resolve dependencies - and build an output file. For Java projects, output files are usually JAR. The output file should contain the release version, not the development version. Documentation should also be generated.
 

Step 5: Push the output file to source control or any other release repository. 
 

Step 6: Go back to the development version, and increase the version number by 1.
 

In large organizations, these tasks may be given to different teams. One team might handle testing, another for generating documentation, and another for releases. This may cause human errors, and steps might get missed. 

Automating these steps will help avoid these issues. Let's see how we can automate deployment for a Maven project. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Setting Up Deployment in Maven

The main plugin we will use to automate deployment is the Maven Release plugin. However, we also need to configure a few things beforehand. Let us do this.

Basic Setup

For this article, we have created a demo project. The pom.xml of the project looks like this. 
 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.codingninjas</groupId>
    <artifactId>DemoProject</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>9</maven.compiler.source>
        <maven.compiler.target>9</maven.compiler.target>
    </properties>

    <distributionManagement>
        <repository>
            <id>Ninja-Release</id>
            <name>ReleaseRepository</name>
            <url>
                file://D://NinjaRelease
            </url>
        </repository>
    </distributionManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>
                                DemoPackage.Runner
                            </mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <phase>deploy</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>

</project>

 

The two main parts of this pom.xml are the <distributionManagement> property and the maven-assembly-plugin. 

Distribution Management

This property specifies where our final output file should be pushed. This can be a remote repository, such as Maven Central Repository or GitHub Packages. It can also be a local directory - a simple folder in your system. Here, we have configured it to be a folder in our system for simplicity. 

For local folders, the URL should be - file://<path>, where <path> is the path to the folder. The id and name tags are for identification purposes. 

Maven Assembly Plugin

This plugin assembles our project into a jar after compiling it.

Here, we have specified a mainClass property under the archive->manifest tag. This is the class that will be run when our JAR executes. The main class in our project is a simple class that prints something to the terminal. 

Runner class code


The descriptorRefs property is set as jar-with-dependencies to tell the plugin to include the dependencies in the final JAR file. 

The executions property contains a single execution. This tells Maven that this plugin is to be included in a build phase. We specify that it should be included in the deploy phase. It should execute the goal single

Single is the only goal that the plugin provides. All other goals are deprecated, and they will be removed in future releases. The single goal will resolve dependencies and package our file into a JAR. 

Adding Source Control

Source Control Management (SCM) tools are used to manage the source code. They keep track of changes and authors, and resolve conflicts. Git is the most popular SCM tool in use these days. It is generally used along with GitHub, an online repository where we can store our code. 

We will also be using Git and GitHub for this. You can read more about Git and GitHub in these articles - Git Basic GuideGitHub Guide. You will be able to set up a repository in GitHub using these articles. 

Now, we need to add Source Control to Maven as well. We need to add the following lines to our pom.xml.

<scm>
        <connection>scm:git:~git_url~</connection>
        <developerConnection>scm:git:~git_url~</developerConnection>
        <url>~git_url~</url>
        <tag>v@{project.version}</tag>
    </scm>


~git_url~ should be the HTTPS URL to your GitHub repository. We can also use methods like SSH, but they are more complicated. The final pom.xml should look like this. 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>


    <groupId>com.codingninjas</groupId>
    <artifactId>DemoProject</artifactId>
    <version>1.1-SNAPSHOT</version>


     <scm>
        <connection>scm:git:~git_url~</connection>
        <developerConnection>scm:git:~git_url~</developerConnection>
        <url>~git_url~</url>
        <tag>v@{project.version}</tag>
    </scm>


    <properties>
        <maven.compiler.source>9</maven.compiler.source>
        <maven.compiler.target>9</maven.compiler.target>
    </properties>



    <distributionManagement>
        <repository>
            <id>Ninja-Release</id>
            <name>ReleaseRepository</name>
            <url>
                file://D://NinjaRelease
            </url>
        </repository>
    </distributionManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>


                    <archive>
                        <manifest>
                            <mainClass>
                                DemoPackage.Runner
                            </mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <phase>deploy</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>


    </build>


</project>


We will see later what the <tag> tag is used for.

Adding Maven Release Plugin

The final step is to add the Maven Release Plugin. We need to add this to the <plugins> section of the pom.xml file. 

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-release-plugin</artifactId>
	<version>2.5.3</version>
	<configuration>
	<goals>pre-integration-test</goals>
	<preparationGoals>package</preparationGoals>
	<tagNameFormat>v@{project.version}</tagNameFormat>
	</configuration>
</plugin>


The configuration for this plugin has been specified as follows.

  • Goals - This is written as a pre-integration test. This means that we want to run this before integration tests.
     
  • Preparation Goals - These goals must be run before the release occurs. Here, we say that we want our project to be packaged before we release it. 
     
  • TagNameFormat - 
    In SCM software, tags are used to manage the lifecycle of the software. Each tag behaves like a checkpoint in the life of the software. For example, each version that is released should have its own tag. 
    By using the value v@{project.version}, Maven will take the current version and put it in Git tags. 
    For example, for version 1.0 - Maven will create the tag v1.0 
     

Let's now try it out. 

Running Automated Deployment

Let's now try to run the automated deployment, and see the effects.

In the terminal, type the command

mvn release:prepare
Maven shows an error

We get this error. Maven is telling us that we have local changes on our system - that have not been synced with Git. Here we see how Maven can help reduce human errors. Now, we will be sure to commit our code before releasing it. 

If we run it after committing, this is our output:

Maven asks us to specify release version

Maven asks us to specify the release version. The tag, and the next development version. We will leave them as defaults as they are fine. 

After a lot of outputs, we will finally get our success message:

Build success


We can go on GitHub and check, that a tag corresponding to version 1.0 has been created.

GitHub tag created

We can check in the pom.xml now, it will have an updated version.

pom.xml updated

Maven has automatically increased the version from 1.0 to 1.1.

Now, the only thing left to do is to deploy the project to our repository. We earlier set the deployment repository as a folder in our local system. 

To do that, run the command. 

mvn deploy
mvn deploy running
mbn deploy running -2
mvn deploy success

We can check in our file path, the JAR file will be there.

JAR files present in output directory

 

Must Read Apache Server

Check out most important Git Interview Questions here.

Frequently Asked Questions

Can deployment be done manually in Maven projects?

Yes, we can still deploy manually. We will have to perform assemble, release and deploy tasks separately. We will also have to make sure no errors occur manually. 

Do we require a specific IDE to perform deployment automation?

No, you don't! In fact, we can perform these tasks without any IDE at all. Maven should just be installed on your system.

Are only JAR files supported?

No, we can use EAR, WAR and other file formats can also be used. 

Conclusion

This blog has explored how we can perform deployment automation in Maven projects. We have seen what basic setup is necessary, how we can add source control like Git, and how to configure the Maven release plugin. 

We hope you leave this article with a broader knowledge of Maven, deployment, and Software project management. We recommend that you explore our different articles on these topics as well, such as:

What is a Maven Artifact?

Phases of Compiler

What are the features and advantages of Maven?

Maven Eclipse Plugin

You can practice questions on various problems on Coding Ninjas Studio, attempt mock tests. You can also go through interview experiences, interview bundle, go along guided paths for preparations, and a lot more!

 

Keep coding, keep reading Ninjas. 

 

Live masterclass