DevOps Classroom Series – 11/Feb/2021

Building/Packaging the Code

  • In this series lets try to understand how to build java code
  • Building java code is executing the .java files with javac <pathto.java> and then it creates .class file. Now to execute code java <pathto.class> Preview
  • In a enterprise software project, we will have multiple java files, so building each file to generate .class file and then combining .class files to build a jar file format is difficult
  • Apache ANT was the first famous tool which helped in building java code.
  • ANT build files are written in XML format.
  • Binary is the format that is packaged version of the code. It can be .exe file, .jar file, .dll file.
  • Lets install ant
  • Create a build.xml file in the source code and write build steps in xml file
<?xml version="1.0"?>
<project name="Hello World" default="info">
    <target name="info">
        <echo>Hello world - Welcome to Ant</echo>
    </target>
</project>
  • Now navigate to the folder in terminal where you have build.xml file and execute ant Preview

  • Refer Here for the sample ant build.xml and steps

  • When we write code we use lot of external libraries which is referred as dependency. Generally for any enterprise project build includes managing dependencies.

Apache Maven

  • This is a software project managment and comprehension tool Refer Here

  • Maven is a tool which follows Convention over configuration

  • Some of the conventions are (${basedir} is the folder where you have your project)

    • source code => ${basedir}/src/main/java
    • resources => ${basedir}/src/main/resources
    • Test cases => ${basedir}/src/test
    • Class file => ${basedir}/target/classes
    • Jar/War => ${basedir}/target/
  • Installing maven

    • Windows: choco install maven
    • Linux: Refer Here
      • Ubuntu:
      sudo apt update
      sudo apt install openjdk-8-jdk -y
      sudo apt install maven -y
      
      Preview
      • centos:
      sudo yum install java-1.8.0-openjdk -y
      wget https://mirrors.estointernet.in/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
      tar xzvf apache-maven-3.6.3-bin.tar.gz
      sudo cp -r apache-maven-3.6.3 /opt
      # sudo vi /etc/environment
      export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-1.el8_3.x86_64/jre/
      export PATH=$PATH:/opt/apache-maven-3.6.3/bin
      
    • mac: brew install maven
  • In Maven we create a pom.xml file where POM stands for Project Object Model. So our sample pom.xml

<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>helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>
</project>
  • Now lets move to the root folder of project and execute mvn compile and mvn package Preview Preview

Leave a Reply

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

About learningthoughtsadmin