Release Management: GIT to Build to Deploy First Maven Project

Release Management: First Maven Project from Git to Build to Deploy on Tomcat

Assumptions

  • Tomcat is working
  • Maven is installed on Machine
  • Git is installed on Machine
  • You have a working project on GitHub
  • You are able to down load your project from github
  • You are able to send changes to github from your machine

 

Create And Build a First Java project using maven

 

Objective

You should be able to run the maven commands below. Each maven command should produce “BUILD SUCCESS”

You should be able to run the generated JAR File to produce the “Hello World!” message.

 

Ref: https://maven.apache.org/guides/getting-started/

 

Below environment variables should already be setup in your “.bash_profile”

  • $ export MAVEN_HOME=/home/u_jenkins/MAVEN
  • $ export PATH=$PATH:$MAVEN_HOME/bin

We will use a brand new directory for our work

  • $ mkdir /home/u_jenkins/TESTING2
  • $ cd /home/u_jenkins/TESTING2

We will take help from Maven to create a new project

  • $ mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes   -DgroupId=www.segintechnologies.com.app   -DartifactId=my-first-app
    • [INFO] ————————————————————————
    • [INFO] BUILD SUCCESS
    • [INFO] ————————————————————————
    • [INFO] Total time: 14.579 s
    • [INFO] Finished at: 2016-01-18T21:02:21-05:00
    • [INFO] Final Memory: 13M/40M
    • [INFO] ————————————————————————
  • $

Maven created the following files for us

  • $ cd /home/u_jenkins/TESTING2
  • $ find .
    • ./my-first-app
    • ./my-first-app/pom.xml
    • ./my-first-app/src
    • ./my-first-app/src/test
    • ./my-first-app/src/test/java
    • ./my-first-app/src/test/java/www
    • ./my-first-app/src/test/java/www/segintechnologies
    • ./my-first-app/src/test/java/www/segintechnologies/com
    • ./my-first-app/src/test/java/www/segintechnologies/com/app
    • ./my-first-app/src/test/java/www/segintechnologies/com/app/AppTest.java
    • ./my-first-app/src/main
    • ./my-first-app/src/main/java
    • ./my-first-app/src/main/java/www
    • ./my-first-app/src/main/java/www/segintechnologies
    • ./my-first-app/src/main/java/www/segintechnologies/com
    • ./my-first-app/src/main/java/www/segintechnologies/com/app
    • ./my-first-app/src/main/java/www/segintechnologies/com/app/App.java
  • $

In order to run maven commands we need to cd to the directory that has the “.pom” file

  • $ cd /home/u_jenkins/TESTING2/my-first-app

You should run the below commands and you should get “BUILD SUCCESS”

  • $ mvn clean
  • $ mvn test
  • $ mvn compile

The package command produces the output of the build which may be a JAR or a WAR.

  • $ mvn package
    • [INFO] Building jar: /home/u_jenkins/TESTING2/my-first-app/target/my-first-app-1.0-SNAPSHOT.jar

This is a Java project, hence we can run this from the command line using below. Expected output is “Hello World!”

  • $ java -cp /home/u_jenkins/TESTING2/my-first-app/target/my-first-app-1.0-SNAPSHOT.jar www/segintechnologies/com/app/App
    • Hello World!
  • $

 

Create And Build a First Java Web project using maven

 

Objective

You should be able to run the maven commands below. Each maven command should produce “BUILD SUCCESS”.

You should be able to copy the WAR file to the running tomcat “/home/tomcat/TOMCAT/webapps” folder and verify the output on a browser.

 

We will use a brand new directory for our work

  • $ mkdir /home/u_jenkins/TESTING3
  • $ cd /home/u_jenkins/TESTING3

We will take help from Maven to create a new project

  • $ mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=www.segintechnologies.com.app -DartifactId=my-first-webapp -DinteractiveMode=false
    • [INFO] ————————————————————————
    • [INFO] BUILD SUCCESS
    • [INFO] ————————————————————————
    • [INFO] Total time: 4.144 s
    • [INFO] Finished at: 2016-01-18T21:17:58-05:00
    • [INFO] Final Memory: 13M/32M
    • [INFO] ————————————————————————
  • $

Maven created the following files for us

  • $ cd /home/u_jenkins/TESTING3/my-first-webapp
  • $ find .
    • ./pom.xml
    • ./src
    • ./src/main
    • ./src/main/resources
    • ./src/main/webapp
    • ./src/main/webapp/index.jsp
    • ./src/main/webapp/WEB-INF
    • ./src/main/webapp/WEB-INF/web.xml
  • $

In order to run maven commands we need to cd to the directory that has the “.pom” file

  • $ cd /home/u_jenkins/TESTING3/my-first-webapp

You should run the below commands and you should get “BUILD SUCCESS”

  • $ mvn clean
  • $ mvn test
  • $ mvn compile

The package command produces the output of the build which may be a JAR or a WAR.

  • $ mvn package
    • /home/u_jenkins/TESTING3/my-first-webapp/target/my-first-webapp.war
  • $

 

To Deploy the WAR FILE

 

  • Copy “/home/u_jenkins/TESTING3/my-first-webapp/target/my-first-webapp.war” to “/home/tomcat/TOMCAT/webapps” on Tomcat Host
  • Tomcat will automatically expand it .
  • Hit the tomcat URL (you need to use your IP and your Port)
  • http://192.168.1.9:8080/my-first-webapp/
  • The browser will say Hello world.

 

 

To Test and confirm that it is really working we will change the code.

We will update index.jsp and change the text in the file

  • vi ~/TESTING3/my-first-webapp/src/main/webapp/index.jsp
  • $ cat ~/TESTING3/my-first-webapp/src/main/webapp/index.jsp
    • <html>
    • <body>
    • <h2>Hello World ! This line added for testing </h2>
    • </body>
    • </html>
  • $

Rebuild and redeploy the war file and then hit the tomcat URL

  • $ cd /home/u_jenkins/TESTING3/my-first-webapp
  • $ mvn clean
  • $ mvn test
  • $ mvn compile
  • $ mvn package
  • Copy “/home/u_jenkins/TESTING3/my-first-webapp/target/my-first-webapp.war” to “/home/tomcat/TOMCAT/webapps” on Tomcat Host
  • Wait for a few minutes for tomcat to expand the WAR File
  • Hit the tomcat URL (you need to use your IP and your Port)
  • http://192.168.1.9:8080/my-first-webapp/

RM_FirstMaven_Project_image001

 

 

 

Putting the Code in GitHub

Earlier

  • We have generated ssh keys
  • We have started ssh agent
  • We have copied ssh keys to Git Hub
  • We have tested connection between our machine and github

 

We are cleaning to remove the temporary files. We will then copy the files to local git clone.

  • $ cd /home/u_jenkins/TESTING3/my-first-webapp/
  • $ mvn clean
    • [INFO] BUILD SUCCESS
  • $

We will use a brand new dirctory

  • $ mkdir /home/u_jenkins/TESTING
  • $ cd /home/u_jenkins/TESTING
  • $ git clone git@github.com:pathikpaul/learngit.git
    • Initialized empty Git repository in /home/u_jenkins/TESTING/learngit/.git/
    • remote: Counting objects: 3, done.
    • remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 3
    • Receiving objects: 100% (3/3), done.
  • $

Copy From: /home/u_jenkins/TESTING3/my-first-webapp/

Copy To: /home/u_jenkins/TESTING/learngit

  • $ cd /home/u_jenkins/TESTING/learngit
  • $ cp -R /home/u_jenkins/TESTING3/my-first-webapp/* .

Below git commands are needed to store the new files to Git Hub

  • $ git add *
  • $ git commit -m “creating first webapp in GIT”
    • [master 6ceb99c] creating first webapp in GIT
    • 3 files changed, 33 insertions(+), 0 deletions(-)
    • create mode 100644 pom.xml
    • create mode 100644 src/main/webapp/WEB-INF/web.xml
    • create mode 100644 src/main/webapp/index.jsp
  • $
  • $ git push
    • Warning: Permanently added the RSA host key for IP address ‘192.30.252.128’ to the list of known hosts.
    • Counting objects: 10, done.
    • Compressing objects: 100% (6/6), done.
    • Writing objects: 100% (9/9), 1.09 KiB, done.
    • Total 9 (delta 0), reused 0 (delta 0)
    • To git@github.com:pathikpaul/learngit.git
    • .6ceb99c master -> master
  • $

We have sent the code to GITHUB

 

We can verify on GitHub. We should be able to see the new files we copied on GitHub.

 

Author: Pathik Paul

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *