1. 程式人生 > >製作 JAVA WEB + TOMCAT7.0 RPM 安裝包 for centos

製作 JAVA WEB + TOMCAT7.0 RPM 安裝包 for centos

安裝rpmbuild,並配置環境

1.1 首先,你應該檢查rpmbuild是否已經被安裝在你的系統上。你將會利用這個工具從 spec 檔案或SRPM 元件建立 RPM。要檢查它是否被安裝了,請執行rpmbuild --showrc這個指令。它將會顯示一大組資料,把rpmbuild所採用的建立環境的細節列明。假若系統輸出:$ rpmbuild: command not found,便意味著rpmbuild未被安裝。你可以用root身份執行下列指令,通過yum來安裝它:

[[email protected] ~]# yum install rpm-build

1.2要建立一個建立 RPM
的環境,這執行下列兩個指令:
[[email protected] ~]$ mkdir -p /usr/src/redhat/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

一般這些目錄結構會在/root目錄下自動建立

2  RPM相關知識點說明

rpmbuild安裝擡會在/root的目錄下生成一個rpmbuild的目錄,共內部結構如下:

  • /root/rpmbuild/BUILD: The directory where the sources are built.
  • /root/rpmbuild/RPMS: Contains the directories, one for each architecture, that will receive the binary packages after.
  • /root/rpmbuild/RPMS/i586: The directory where rpm packages for i586 processors will be stored.
  • /root/rpmbuild/RPMS/x86_64: The directory where rpm packages for AMD64 processors will be stored.
  • /root/rpmbuild/RPMS/noarch: Idem for noarch (processor-independant) packages.
  • /root/rpmbuild/SOURCES: The source files (mypackage.tar.bz2;.tar.gz 
     for example).
  • /root/rpmbuild/SPECS: Thespec files we will have to construct.
  • /root/rpmbuild/SRPMS: The source rpm after building.

 Insidethe spec file,請參考:http://wiki.mandriva.com/en/Development/Howto/RPM#Inside_the_spec_file  http://blog.csdn.net/redguardtoo/article/details/1373885

3 準備必要的檔案

[[email protected] ~]# mkdir tomcat-myweb-1.0
[[email protected] ~]# mv /root/tomcat /root/tomcat-myweb-1.0

製作.spec檔案, 例如:tomcat-myweb-1.0.spec
[[email protected] ~]# vi tomcat-myweb-1.0.spec
Summary:myweb
Name:tomcat-myweb
Version:1.0
Release:0
BuildRoot:%{_tmppath}/%{name}-%{version}-root
Source0:tomcat-myweb-1.0.tar.gz
License:GPL
Group:Applications/Productivity/vbox
%description
losoel myweb RPM package
%prep
rm -rf $RPM_BUILD_DIR/%{name}-%{version}
%setup -q

%build

%install
rm -fr $BuildRoot
mkdir -p    $RPM_BUILD_ROOT/usr/local/myweb
cp    -rf * $RPM_BUILD_ROOT/usr/local/myweb
if [ ! -d  /usr/local/myweb ]
then
{
  mkdir -p  /usr/local/myweb
}
fi
chmod 755 /usr/local/myweb  -R
cp -rf * /usr/local/myweb

%files
%defattr(-,root,root)
/usr/local/myweb

%pre

%post
if [ -d  /usr/local/myweb/ ]
then
{
  bash /usr/local/myweb/preinst
}
fi

%preun  
if [ -d  /usr/local/myweb/ ]
then
{
  bash /usr/local/myweb/prerm
}
fi

%clean
rm -fr $BuildRoot

其它輔助指令碼: preinst

[[email protected] ~]# vi preinst
#!/bin/bash
function write_log()
{
    dateoftod=`date +%Y%m%d`
    logfilename="log-myweb-$dateoftod"
    if [ ! -d /usr/local/myweb/myweb-logs ]
    then
    {
        mkdir -p /usr/local/myweb/myweb-logs
    }
    fi
    if [ ! -f /usr/local/myweb/myweb-logs/$logfilename ]
    then
    {
        touch /usr/local/myweb/myweb-logs/$logfilename
    }
    fi
    if [ $# -eq 2 ]
    then
        echo "$(date "+%Y-%m-%d %H:%M:%S") [$0:$1] $2" >>/usr/local/myweb/myweb-logs/$logfilename
    elif [ $# -eq 3 ]
    then
        echo "$(date "+%Y-%m-%d %H:%H:%S") [$0:$1] $2 : $3">>/usr/local/myweb/myweb-logs/$logfilename
    else
        echo "record log error."  
    fi    
}


echo "-------------------------------------------------------------------------"
echo "    Welcome install myweb software for cloud platform security system."
echo "-------------------------------------------------------------------------"
write_log $LINENO "INFO:  install process start."

if [ $UID -ne 0 ]
then
{
    write_log $LINENO "FATAL: user privilege error."
    echo "Error:Superuser privilege are required to run this script."
    exit -1
}
fi
write_log $LINENO "INFO: user privilege correct."

#1:check mysql installed

#3:check JDK 1.6 1.7
checkjre=$(java -version  2>&1|grep "java version")
if [ "$checkjre" = "" ]
then
{
    write_log $LINENO "FATAL: Please install jre first."
    echo "Please install jre first."
    exit -1
}
else
{
    jre_version=$(java -version 2>&1 | grep "java version" | awk '{print $3}' | tr -d \" | awk '{split($0, array, ".")} END{print array[1]"."array[2]}')
    cmp_version="1.6"
    result_cmp=$(expr $jre_version \>= $cmp_version)
    if [ $result_cmp -eq 0 ]
    then
    {
         write_log $LINENO "FATAL: Jre version too low! Please install version 1.6 or higher."
    echo "Jre version too low! Please install version 1.6 or higher."
    exit -1
}
    fi
}
fi

其它輔助指令碼:prerm

[[email protected] ~]# mkdir prerm
#!/bin/bash
function write_log()
{
    dateoftod=`date +%Y%m%d`
    logfilename="log-myweb-$dateoftod"
    if [ ! -d /usr/local/myweb/myweb-logs ]
    then
    {
        mkdir -p /usr/local/myweb/myweb-logs
    }
    fi
    if [ ! -f /usr/local/myweb/myweb-logs/$logfilename ]
    then
    {
        touch /usr/local/myweb/myweb-logs/$logfilename
    }
    fi
    if [ $# -eq 2 ]
    then
        echo "$(date "+%Y-%m-%d %H:%M:%S") [$0:$1] $2" >>/usr/local/myweb/myweb-logs/$logfilename
    else
        echo "record log error."  
    fi
}
write_log $LINENO "INFO: start uninstall myweb process."
if [ $UID -ne 0 ]
then
{
    write_log $LINENO "FATAL: user privilege error."
    echo "Error:Superuser privilege are required to run this script."
    echo "error">rmlog
    exit -1
}
fi

#3 del /usr/local/myweb
if [ -d /usr/local/myweb ]
then
{
   write_log $LINENO "delete tomcat-myweb."
   rm -rf /usr/local/myweb
}
fi

preinstprerm兩個檔案移到tomcat-myweb-1.0目錄下:
[[email protected] ~]# mv ./preinst ./tomcat-myweb-1.0
[[email protected] ~]# mv ./prerm ./tomcat-myweb-1.0

打包tomcat-myweb-1.0.tar.gz
[[email protected] ~]#tar –zcvf tomcat-myweb-1.0.tar.gz ./tomcat-myweb-1.0

tomcat-myweb-1.0.tar.gz移到rpmbuildSOURCES目錄下:
[[email protected] ~]#mv tomcat-myweb-1.0.tar.gz ./rpmbuild/SOURCES

tomcat-myweb-1.0.spec檔案移到rpmbuildSPECS目錄下

[[email protected] ~]# mv ./tomcat-myweb-1.0.spec ./rpmbuild/SPECS

4 執行rpmbuild命令,完成RPM包的製作

[[email protected]~]#rpmbuild –ba ./rpmbuild/SPECS/tomcat-myweb-1.0.spec

5 如果成功,則rpm包會儲存在/root/rpmbuild/RPMS子目錄內

注意:

1 如果想在執行安裝rpm時,即(rpm-ivh xxxxxxxxxxx.rpm)與使用者進行提示互動,必須要在.spec檔案的  ’  %post ‘ 階段呼叫shell指令碼:

2 如果想在執行刪除rpm時,即(rpm -e xxxxxxxxxxx )與使用者進行提示互動,必須要在.spec檔案的  ’ %preun‘ 階段呼叫shell指令碼。