Friday, April 22, 2022

git commands

List of important commands for git CLI users

  • git clone - clone project to local machine
  • git status - check current status  
  • git add ./filename - add new changes locally
  • git commit -m message - commit new changes into bitbucket
  • git push - will send changes into bitbucket
  • git pull - get updates from bitbucket to local repository
  • git branch - view current working branch
  • git branch dev - create a new branch called dev
  • git checkout dev - switch into dev from current branch
  • git push --set-upstream origin dev - push changes to dev branch

  • git remote -v  - find remote repository url

  • git remote set-url origin https://github.com/suji/TestApp.git - change remote repository url to  a new one
  • git merge --abort -abort current merge command
  • git remote rm origin -remove origin url of repository


Create a new repository on the command line

  • echo "# TestApp" >> README.md
  • git init
  • git add README.md
  • git commit -m "first commit"
  • git remote add origin https://github.com/sujith/TestApp.git
  • git push -u origin master


Push an existing repository from the command line

  • git remote add origin https://github.com/sujith/TestApp.git
  • git push -u origin master


When you add new file(which has already in the git repository) to git ignore file, run below command to ignore it. 

  • git rm -r --cached .  
  • git add .
  • git commit -m 'removing ignored files'

Configure git for the first time

  • git config --global user.name "Sujith Delpachithra"
  • git config --global user.email "sujith@g**.lk"


If your code is already tracked by git then set this repository as your "origin" to push.

  • cd existing-project
  • git remote add origin http://bitbucket.mobitel.lk/suji/testapp.git
  • git push -u origin --all
  • git push origin --tags


You can use the git cherry-pick command to merge one branch to another.

  • git cherry-pick d4d8e7c

(Replace d4d8e7c with  relevant commit id you need to merge from another branch to this branch)


You can revert to previous commit.

  • git reset --hard <commit_id>
  • git push -f origin staging


Let's say you want to merge local development branch into local staging branch.

Then first you need to checkout stating branch with this command.

  • git checkout staging

Then you can do merging with this command.

  • git merge development


How to merge development branch changes to staging branch.

1. First checkout to staging branch

2. Then execute below command

  • git merge development


Thursday, April 21, 2022

Sending mails/ sms from Linux server

You have to configure mail server in Linux server. Then you can use following command to send mails.

For mail -

mail -s "subject" recipients_emails < mail body

For SMS -

mail -s "subject" number@your_email_domain < mail body


Schell script to restart JAVA apps

#!/bin/bash

ps -ef |grep "java -jar" > /apps/sujith/apps

x=$(cat /apps/sujith/apps |grep "SMS_App.jar" |wc -l)

echo $x

        if [ $x = 0 ]

        then

                date

                echo " Starting ......Please check logs !! "

                echo ""

                cd /apps/sujith/sms

                nohup java -jar -Xms32m -Xmx128m SMS_App.jar 2>> SMS_App.err &

        elif [ $x = 1 ]

        then

                date

                echo "STOPPING "

                cat /apps/sujith/apps |grep "SMS_App.jar" | awk -F " " '{print $2}' | xargs kill -9

                echo "STOPPED "

                date

                echo "STARTING"

                cd /apps/sujith/sms

                nohup java -jar -Xms32m -Xmx128m SMS_App.jar 2>>SMS_App.err &

        else

                echo "SMS App"

        fi


Monday, April 18, 2022

sample docker file for JAVA application

 You can add following Dockerfile to JAVA docker image.

#Use open JDK 8 image as the base image
FROM openjdk:8

#Create a new app directory for my application files (Inside image file system)
RUN mkdir /app

#Set the directory for excuting future commands
WORKDIR /app

#Copy my application files from host machine into image file system
COPY target/docker-helloworld-1.0-SNAPSHOT.jar  app.jar

#Run jar file
ENTRYPOINT ["java","-jar","app.jar"]
#CMD java -jar app.jar

docker commands

  • docker -version

checking current installed docker version

  • docker container run hello-world

run hello-world docker image. if image is not available in local repository, above command will download the image from docker-hub

  • docker run -it ubuntu bash
run ubuntu container in docker.
  • docker ps
list running docker container
  • docker image  build -t hello-world:1.0 .
use above command for buid a docker image. (here . means Dockerfile contains in the current directory)
  • docker container run hello-world:1.0
use below command for run above docker image.
  • docker image ls
list images in local repository

Wednesday, March 23, 2022

Shutdown Spring Boot Command Line Application

You can use ConfigurableApplicationContext to shutdown/closes all resources used by application as follows.

 public static void main(String[] args) {

        ConfigurableApplicationContext ctx=SpringApplication.run(Application.class, args);

        ctx.close();

    } 

    @Bean

    public CommandLineRunner commandLineRunner(RestTemplate restTemplate) {

        return args -> {

}

    }


Monday, March 14, 2022

Remove the <return> tag from SOAP response in JAVA

 Following JAVA annotation can be used to avoid <return> tag from SOAP web service response.

@WebService(serviceName = "Operations")

public class Service {

    @WebMethod(operationName = "processUser")

    @SOAPBinding(parameterStyle=ParameterStyle.BARE)

    public Result processUser(@WebParam(name = "userName")String userName) {

        Result result = new Result();

        result.setResultCode("1");

        result.setResultDesc("success");

        return result;

    }

}

ParameterStyle.BARE will return SOAP response with your defined object attributes.