DevOps Classroom Series – 09/May/2020

Day Builds and Night Builds

  • The trigger to Start CI/CD will be
    • A Schedule (Every day at 7:00 AM )
    • Whenever developer commits code
  • Whenever above events are triggered we should start ci/cd, so any ci/cd engine will start its journey for Version Control System to Production Systems. The whole steps are called as pipelines.
  • Lets look at popular steps in the order of execution in a Typical Continuous Delivery Pipeline
    • Developer Pushes the code to Git

    • Build the Code

      • This is very much dependent on Development Language
        • C => Make, GCC
        • Java => Ant, Maven, Gradle
        • .net => MSBuild, dotnet build
        • Generic tools: buildbot etc
        • Container => Docker Image Build
    • Test the Code using Unit Tests written by Dev Teams

      • Running Unit Tests (JUnit, MSTest, Pytest, Jasmine)
    • Create a Package

    • Create all the possible test Environments (QA, SystemTest, PerformanceTest) and deploy the package to the respective Environment and Execute tests

      • To Create Environment we use
        • InfraProvisioning (Terraform)
        • Configuration Management (Ansible)
        • Orchestration (Kubernetes)
      • Automated System Tests and Performance tests are triggered from Jenkins
        • Selenium
        • JMeter
        • Karate
    • Gather all the Test Results and Give Feedback

Maven Installation

  • Windows:
  • MAC:
    • Use homebrew to install mac
  • Ubuntu:
    sudo apt-get update
    sudo apt-get install maven -y
    
  • Centos/Redhat: Refer Here

Scenario-1: Java Web Application without much knowledge of build

  • Preparation:

    • Fork the Spring Pet Clinic Project from here Preview
    • My repo is located at here
  • Target:

    • We need to Create a package for the Java Code and this needs to be generated whenever developer pushes the commit
  • Implementation

    • Create a freestyle project Preview Preview Preview Preview Preview

    • Both PollSCM and schedule uses cron syntax to schedule the job and poll time. You can use cron generators as shown Preview Preview

    • Developer has asked you to install maven3 and execute “`mvn package“

    • Installing maven on ubuntu machine

    sudo apt-get install maven -y
    
    • Configure Build as shown below and save the project (Wait for build or do manual) Preview

Maven

  • Maven is a Popular tool and known for Java and Java Based Languages.

    • Builds
    • Dependency Management
    • Reporting
    • Documentation
    • Release Management
  • Prior to Maven, Java Projects used to use a tool called as Ant to build the projects. ANT is a configuration based tool.

  • In ANT to Build the code

    • Configure Build file
    • Add configuration in XML file with target Sections Refer Here
  • Maven had wonderful dependency resolution and followed a convention approach. (Convention over Configuration)

Maven Model

  • Project Object Model (POM) is used to manage projects build, reporting, dependencies and dcumentation.

  • Convention Over Configuration:

    • Every Maven Project starts with pom.xml => Projects base directory => ${project-dir}
    • Java Code => ${project-dir}/src/main/java
    • Groovy Code => ${project-dir}/src/main/groovy
    • Scala Code => ${project-dir}/src/main/scala
    • Resources => ${project-dir}/src/main/resources
    • Tests => ${project-dir}/src/test
    • Folder for Packages and Classes => ${project-dir}/target
  • Lets Create a Simple Java Project

    • Create a directory hello-maven
    mkdir hello-maven
    cd hello-maven
    touch pom.xml
    
    • Copy the following content into pom.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <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>com.qualitythought.devops</groupId>
    <artifactId>hello-maven</artifactId>
    <version>1.0</version>
    
    </project>
    
    
    • Create a directory tree as mentioned below and create a file Application.java
    mkdir src
    mkdir src/main
    mkdir src/main/java
    touch src/main/java/Application.java
    
    • Copy the following code into Application.java
    class Application {
        public static void main(String args[]) {
            System.out.println("Hello World Continues");
        }
    }
    
    
    • navigate to projects base directory and execute “`mvn package“
  • Maven Phases:

    • compile: This creates class files by compiling Java Code. To execute this phase mvn compile Preview
    • Test: This Phase executes the unit tests. To execute use mvn test
    • package: This Phase creates the Java Package (JAR/WAR/EAR) mvn package Preview
    • install
    • Deploy
    • clean: Will Remove Target folder Preview
  • Maven Dependencies:

    • When developers develop applications they will use lot of libraries which are defined as dependencies in pom.xml
    • So change pom.xml in the hello-maven/pom.xml to
    <?xml version="1.0" encoding="UTF-8"?>
    <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>com.qualitythought.devops</groupId>
    <artifactId>hello-maven</artifactId>
    <version>1.0</version>
    
    <dependencies>
        <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>29.0-jre</version>
        </dependency>
        
        
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
    
    </dependencies>
    
    </project>
    
    
    • Now execute mvn compile and Maven tries to download dependencies for this it has a repository system Preview

    • Repositories in Maven Preview

    • Local Repository:

      • Generally present in HOME_DIRECTORY/.m2 folder and can be changed with Environmental Variable M2_HOME
    • Central Repository:

    • Remote Repository:

      • Hosted with in Organization to resolve dependencies, Organizations use Artifactory(JFrog), Nexus Softwares for Remote Repository
    • In Maven we configure the builds using pom. With POM we need to understand the concept of Super POM and effective POM

    • Super POM is Mavens Default POM. ALL POM inherit from default.

    • While building any project have a look at effective pom by executing mvn help:effective-pom . Below is effective pom of the hello-maven

<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Generated by Maven Help Plugin on 2020-05-09T04:23:58Z                 -->
<!-- See: http://maven.apache.org/plugins/maven-help-plugin/                -->
<!--                                                                        -->
<!-- ====================================================================== -->
<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Effective POM for project                                              -->
<!-- 'com.qualitythought.devops:hello-maven:jar:1.0'                        -->
<!--                                                                        -->
<!-- ====================================================================== -->
<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.qualitythought.devops</groupId>
  <artifactId>hello-maven</artifactId>
  <version>1.0</version>
  <dependencies>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>29.0-jre</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-java8</artifactId>
      <version>5.4.15.Final</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>/home/ubuntu/hello-maven/src/main/java</sourceDirectory>
    <scriptSourceDirectory>/home/ubuntu/hello-maven/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>/home/ubuntu/hello-maven/src/test/java</testSourceDirectory>
    <outputDirectory>/home/ubuntu/hello-maven/target/classes</outputDirectory>
    <testOutputDirectory>/home/ubuntu/hello-maven/target/test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>/home/ubuntu/hello-maven/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>/home/ubuntu/hello-maven/src/test/resources</directory>
      </testResource>
    </testResources>
    <directory>/home/ubuntu/hello-maven/target</directory>
    <finalName>hello-maven-1.0</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.5.3</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>default-testResources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testResources</goal>
            </goals>
          </execution>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>install</phase>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.3</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>site</phase>
            <goals>
              <goal>site</goal>
            </goals>
            <configuration>
              <outputDirectory>/home/ubuntu/hello-maven/target/site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <outputDirectory>/home/ubuntu/hello-maven/target/site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <outputDirectory>/home/ubuntu/hello-maven/target/site</outputDirectory>
          <reportPlugins>
            <reportPlugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
            </reportPlugin>
          </reportPlugins>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <outputDirectory>/home/ubuntu/hello-maven/target/site</outputDirectory>
  </reporting>
</project>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Floating Social Media Icons by Acurax Wordpress Designers

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube