Example – the typical DevOps process for a Java development stack
The prerequisites are as follows:
- Java version 1.6 or above
- Apache Maven 3.5.2
- Jenkins server
If you haven't already installed Java, you can download it from the official website: https://java.com/en/download/.
Use the following commands for this sample CI/CD application. I will be using an Ubuntu 16.04 instance for setup. So let's start by installing Java.
Log in to your Ubuntu server machine as a normal user. We need a user with sudo access for installing packages on the machine. I will be using the Ubuntu username for this example:
# sudo apt-get update # sudo apt-get install default-jdk
The previous commands will install Java version 8 on the machine. Verify this with the following command:
$java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)
Now, we have Java in place, so we will move on to the installation of Maven. The installation steps are as follows:
- Download Maven from the official website:
$wget http://www-eu.apache.org/dist/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz
- Extract the package using the following:
$tar -xvf apache-maven-3.5.2-bin.tar.gz
- We need to set up environment variables. Add a path to .bashrc:
$export M2_HOME=/home/ubuntu/apache-maven-3.5.2
$export PATH=${M2_HOME}/bin:${PATH}
$source ~/.bashrc
- Verify whether Maven was installed on your machine:
$ mvn -v
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T07:58:13Z)
Maven home: /home/ubuntu/apache-maven-3.5.2
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "4.4.0-1049-aws", arch: "amd64", family: "unix"
We will create a project in Maven. Here we will be using the official Maven example Maven in 5 Minutes. You will find a link in the References section.
Let's move on to creating a sample project in Maven:
$mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
It will take some time to download dependencies. After completing execution of command, it will create a directory with value passed to DartifactId that is my-app.
In this example, src/main/java contains source code, src/test/java has test code, and pom.xml contains all information required to build the project.
The following is the source code we have from the sample app:
cat src/main/java/com/mycompany/app/App.java
package com.mycompany.app;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
Now we will build our sample project:
#mvn package
This will compile the project and create a JAR in the target folder as my-app-1.0-SNAPSHOT.jar.
Test the JAR with the following command:
#java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
We get the following output:
Hello World!
So, we got our sample application running but what if we have many developers working on the project? We need a version control tool and some standard procedure for deploying this project to the server environment. For this, we will follow our sample DevOps pipeline with Git and Jenkins.
Git is a version control tool that helps us to track changes in our source code and coordinate our work with different developments. It is the most commonly used version control tool nowadays. We will have a look at Git in depth in Chapter 5, Version Control. For this sample pipeline, we can use a GitHub account.
Create a repository on GitHub and push sample code to this repository:
$cd my-app
$Git init
$Git add .
$Git commit -m "first commit"
$Git remote add origin https://Github.com/priyankadive/devops-sample.Git
$Git push -u origin master
Now that we have our version control ready, we will move on to the next step in using a continuous integration and continuous delivery process using Jenkins.
Jenkins is an open-source tool written in the Java programming language. It is used to automate continuous integration and continuous delivery jobs. We will see Jenkins in detail in Chapter 6, Continuous Integration.
Let's get started with Jenkins:
- Install Jenkins. I have installed Jenkins on an Ubuntu server.
- Log in to the Jenkins server.
- Install the required Jenkins plugins on the Jenkins server using the following process.
- Go to Manage Jenkins | GitHub Plugin and search for maven plugin.
- Install the Maven plugin in Jenkins if it was not installed while setting up Jenkins. You need to configure Maven in Jenkins Global Tool Configuration as we have already installed it.