Automatic Installation of Java programs

Source: Internet
Author: User

I have used Java service wrapper, which is good. However, I can also create scripts to complete basic functions. Java service wrapper is not fully functional, but it is sufficient and fully controlled.

Objectives:

1. Generate the installation package from the maven Project

2. Install the installation package as Ubuntu service.

My Java program is used to send an email by reading the email content of MongoDB and sending information. It also relies on a config. xml file as the startup parameter, which configures the basic information of MongoDB and the basic information of the SMTP server.

Now, run create_deploy.sh to create the installation package. The script content is as follows:

#!/bin/bashsource ./tool.shmvn clean package -DskipTestsmvn dependency:copy-dependenciesremoveFolder deploymkdir deploycp -r ./target/dependency ./deploycp ./target/email-1.0.jar ./deploycp ./config.xml ./deploycp ./email ./deploycp ./install.sh ./deploycp ./tool.sh ./deploy

Dependent tool. Sh script content:

#!/bin/bashfunction removeFolder {    if [ -d "$1" ]    thenecho "$2 folder exists already, remove it..."rm -rf $1    elseecho "$2 folder doesn't exists"    fi}#$1 src folder#$2 dst folderfunction copyFolder {    if [ -d "$2" ]    thenecho "$2 folder exists already, remove it..."rm -rf $2    elseecho "$2 folder doesn't exists, start copying..."    fi    cp -r $1 $2}#remove the folder if exists already#$1 folder pathfunction createFolder {    if [ -d "$1" ]    thenecho "$1 folder exists already, remove it..."rm -rf $1    elseecho "$1 folder doesn't exists, create it..."    fi    mkdir -p $1}#remove the link if exists already#create a link($2) to file($1)function createLink {    if [ -L "$2" ]    thenecho "$2 link exists already, removing it..."rm $2    elseecho "$2 link doesn't exists, creating it..."    fi    echo "creating link: $2 to $1"    ln -s $1 $2}#$1 source file name#$2 source folder#$3 destion folder#remove the file if exists already#create a filefunction copyFile {    if [ -f "$3/$1" ]    thenecho "$3/$1 exists already, removing it..."rm $3/$1    elseecho "$3/$1 doesn't exists, copying it..."    fi    cp $2/$1 $3}# $1 variable name# $2 expected value# put this into /etc/environment if not foundfunction setEnv {    source /etc/environment    if [ "${!1}" = "$2" ]    thenecho "$1 is correct: $2"    elseecho "$1 is wrong: ${!1} != $2"h=`grep "$1=\"$2\"" /etc/environment`if [ -n "$h" ]then    echo "/etc/environment has $1 already"else    echo "Adding $1 into /etc/environment..."    echo "$1=\"$2\"" >> /etc/environmentfisource /etc/environment    fi}#$1 means the full name of dpkg#return 1 if dpkg is installed (found 'ii dpkg-name' in the returned string)#otherwise return 0function hasDpkg {    r=`dpkg -l | grep "$1"`    if [ -n "$r" ]    thenh=`dpkg -l | grep "ii  $1"`if [ -n "$h" ]then    return 1else    return 0fi    elsereturn 0    fi}#$1 search string#$2 file path#return 1 if found#return 0 if not foundfunction findStringInFile {    h=`grep "$1" $2`    echo "h: $h"    if [ -n "$h" ]    thenreturn 1    elsereturn 0    fi}#$1 dpkg namefunction installDpkg {    hasDpkg $1    r=$?        if [ $r -eq 1 ]    thenecho "$1 was installed"    elseecho "$1 was not installed, installing..."apt-get install $1    fi}#$1 user name#return 1 if exists#return 0 if doesn't existfunction hasUser {    h=`grep "$1" /etc/passwd`    echo "h: $h"    if [ -n "$h" ]    thenreturn 1    elsereturn 0    fi}#$1 user group name#return 1 if exists#return 0 if doesn't existfunction hasUserGroup {    h=`grep "$1" /etc/group`    echo "h: $h"    if [ -n "$h" ]    thenreturn 1    elsereturn 0    fi}#remove user and home folder#then create then againfunction recreateSystemUserAndFolder {    hasUser $1    r=$?        if [ $r -eq 1 ]    thenecho "$1 exits already,remove it..."userdel -r $1    elseecho "$1 doesn't exist,create it..."    fi    adduser --home /home/$1 --system --shell /bin/bash $1  }#remove user group #then create it againfunction recreateUserGroup {    hasUserGroup $1    r=$?    if [ $r -eq 1 ]    thenecho "$1 exists already, remove it..."delgroup $1    elseecho "$1 doesn't exist, create it..."    fi    groupadd $1}

The create_deploy script creates the deploy directory, generates jar packages and dependency packages using the MVN command, copies these packages to the deploy directory, and has three other files:

Config. XML is the configuration file, which must be

Install. Sh is the script to install the email-1.0.jar as a service

Email is the script to be placed under/etc/init. d /.

After creating deploy, go to the deploy directory and run the install. Sh file. The installation is successful. Start the service.

Check the content of the install. Sh file:

#!/bin/bashsource ./tool.shcreateFolder /home/dist/email/cp -r ./dependency /home/dist/email/copyFile email-1.0.jar $PWD /home/dist/email/copyFile config.xml $PWD /home/dist/email/copyFile email $PWD /etc/init.dchmod +x /etc/init.d/emailupdate-rc.d email defaultsservice email start

Let's take a look at the email script content:

!/bin/sh### BEGIN INIT INFO# Provides:     email server# Required-Start:# Required-Stop:# Default-Start:        2 3 4 5# Default-Stop:         0 1 6# Short-Description: email# Description: email server### END INIT INFO. /lib/lsb/init-functionsexport JAVA_HOME=/usr/jdk1.6export PATH="$JAVA_HOME/bin:$PATH"case "$1" in  start)     log_begin_msg "Starting email server"     java -cp /home/dist/email/dependency/ -jar /home/dist/email/email-1.0.jar /home/dist/email/config.xml &     log_end_msg 0     ;;  stop)     PID=`ps -ef | grep 'email-1.0.jar' | grep -v grep | awk '{print $2}'`     log_begin_msg "Stopping email server"     if [ ! -z "$PID" ]; then        kill -15 $PID     fi     log_end_msg 0     ;;  restart)     $0 stop     $0 start     ;;  *)     log_success_msg "Usage: service email {start|stop|restart}"     exit 1esacexit 0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.