Java CI: Build and Push a WAR Application to Nexus

- By Avi Cavale on May 21, 2018

This tutorial explains how to build and push a Java-based web application to a Nexus Repository using Maven. We are using a Java demo application that has basic CI tests.

These are the following concepts you need to be familiar with to proceed further:

 

Step-by-step instructions

Follow the steps below in order to deploy your application.

  • Create a GitHub repo that will hold the code for your application. If you do not have your own application, you can clone our sample repository devops-recipes/java_web_app.
  • Install Nexus Repository if you do not already have an available instance. You will need the following information:
    • Nexus Repo URL
    • Repo Name
    • Username
    • Password
  • Install Java Oracle 8 SDK on your local machine and make sure $JAVA_HOME is set. You can follow this on Digital Ocean blog or refer to is our version
  • Install Maven on to your local machine using the following commands:
wget -nv http://redrockdigimark.com/apachemirror/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz

tar xzf apache-maven-3.5.2-bin.tar.gz -C /usr/local && rm -f apache-maven-3.5.2-bin.tar.gz

ln -fs/usr/local/apache-maven-3.5.2/bin/mvn /usr/bin
  • Create settings.xml in this location ~/.m2/settings.xml and  replace the highlighted fields ${nexus_url}${nexus_username} & ${nexus_password} with your values. This file sets global settings for Maven. We are defining 2 nexus endpoints, one for snapshots and one for releases. We also have a central mirror to download all your dependencies. If you are using our sample application, this file is available in the temp folder.

settings.xml

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

    <servers>
        <server>
            <id>nexus-snapshots</id>
            <username>${nexus_username}</username>
            <password>${nexus_password}</password>
        </server>
        <server>
            <id>nexus-releases</id>
            <username>${nexus_username}</username>
            <password>${nexus_password}</password>
        </server>
    </servers>

    <mirrors>
        <mirror>
            <id>central</id>
            <name>central</name>
            <url>${nexus_url}/repository/maven-public/</url>
            <mirrorOf>*</mirrorOf>
        </mirror>
    </mirrors>

</settings>
  • Update your pom.xml for the app. Replace ${repository_url} with the full URL of your repository to where you want to push the WAR. ${group_id} is the group id, ${artifact_id} is the name of your artifact, ${artifact_extension} is  war for our scenario, ${artifact_version} is a version number. If you are using our sample application, this file is available in the root folder.

pom.xml

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>${group_id}</groupId>
    <artifactId>${artifact_id}</artifactId>
    <packaging>${artifact_extension}</packaging>
    <version>${artifact_version}</version>
    <name>HelloWorld Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring.version>4.3.2.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
                <version>1.4.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
                <version>1.4.0.RELEASE</version>
    </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
                <version>1.4.0.RELEASE</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>HelloWorld</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
            <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <redirectTestOutputToFile>true</redirectTestOutputToFile>
                    <reportsDirectory>${ship_test_res_loc}</reportsDirectory>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.19.1</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
    <distributionManagement>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <url>${repository_url}</url>
        </snapshotRepository>
    </distributionManagement>
</project>
  • Execute this command to test and then push the WAR file to your Nexus repository:
mvn -q -B clean install deploy
  • You should see a new version uploaded to your Nexus repo.

 

Challenges with manual execution

There are a few challenges with manually executing the workflow described above on your local machine.

  • You have to prepare your local environment with all software, tools and packages. This could interfere with other projects you might be working on.
  • Builds don't trigger automatically if your source code changes.
  • You have to manually manage credentials and be careful to use the right credentials. It's very easy to make mistakes.
  • You cannot trigger further steps of your pipeline when you update a new version, automatically. For example, you might want to trigger a multi-stage deployment workflow through Test and Staging environments.
  • Your machine is under load during the build process.

 

Automatically building and pushing to Nexus

To show you how to automate this CI workflow, we have designed a step by step tutorial in our documentation:

Build and Push a Java WAR application to Nexus using Maven

If you want a live demo of the Shippable platform and watch this scenario in action, schedule a demo with us:

Schedule a demo

Topics: continuous integration (CI), maven, java, nexus


Add comments below...