国产服务器需要 java生成jar到rpm打包说明
说明:目前项目需要部署到国产服务器上面,无法联网,拷贝东西只能使用光盘的那种,国产服务器对于开机启动不可以通过更改vi /etc/rc.d/rc.local 进行,无权限修改,无法执行。只能通过rpm包授权.service文件的方式做到开机启动。
第一次把jar文件打包为rpm文件
一、更改yum源,改为阿里的路径
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak
curl -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
curl -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
查看当前yum源
yum repolist

更改源完成
二、安装rpmbuild-用于生成rpm的实用程序、rpmdev-开发实用程序以生成rpm。
sudo yum install rpm-build
出现完成结束

# 安装制作rpm包的文件
yuminstall rpm-build rpmdevtools
出现完成结束
三、配置rpm需要的项目配置文件
1、创建初始rpm文件夹结构
执行以下命令:
cd ~
mkdir rpmbuild
cd ~/rpmbuild
rpmdev-setuptree
查看所在目录:出现如下5个文件夹
pwd
/root/rpmbuild

2、将jar项目文件包文件复制到源文件夹中
在/root/rpmbuild 目录下:
cp /home/run.jar SOURCES/
3、创建编写rpm规范文件
在/root/rpmbuild 目录下:
vim SPECS/run.spec
Name:run
Version:0.1
Release:1%{?dist}
Summary:yiyuduoyi jar 打包 rpm
Group:yiyuduoyi
License:GPL
#URL:
AutoReqProv: no
Source0:run.jar
#BuildRequires:
#Requires:
#Requires:
%description
yiyuduoyi softwoare 是科大讯飞开发的一款多语种智能辅助翻译软件
# disable debuginfo, which is useless on binary-only packages
# %define debug_package %{nil}
# do not repack jar files
# %define __jar_repack %{nil}
%install
app_dir=%{buildroot}/data/yydy_test_rpm
rm -rf %{buildroot}
mkdir -p %{buildroot}
mkdir -p $app_dir
# copy all files
cp %{SOURCE0} $app_dir/run.jar
%files
# define default file attributes
%defattr(-,root,root,-)
%doc
%dir /data/yydy_test_rpm
/data/yydy_test_rpm/run.jar
%post
function iestart(){
cd /data/yydy_test_rpm
nohup /usr/local/jdk1.8.0_181/bin/java -Xms2048m -Xmx2048m -XX:PermSize=1024M -XX:MaxPermSize=1024M -jar /data/yydy_test_rpm/run.jar --server.port=8080 > /data/yydy_test_rpm/temp.txt &
sleep 10
cat /data/yydy_test_rpm/temp.txt
}
pid=`sudo netstat -anp|grep 7711|awk '{print $7}'|awk -F "/" '{print $1}'`
echo $pid
if [ -z $pid ];then
echo "null"
iestart
else
echo "isnot null"
kill -9 $pid
iestart
fi
# 卸载后执行,删除文件目录
%postun
case "$1" in
0) # This is a package remove 杀死进程,再删除文件
pid=`sudo netstat -anp|grep 8080|awk '{print $7}'|awk -F "/" '{print $1}'`
echo $pid
if [ -z $pid ];then
echo "null"
else
echo "isnot null"
kill -9 $pid
fi
# remove app folders
rm -rf /data/yydy_test_rpm
;;
1) # This is a package upgrade
# do nothing
;;
esac
四、开始构建进行运行
1、构建rpm包
rpmbuild -ba SPECS/run.spec
这里因spec文件在外面编写,在构建时一直提示
一直报错 \r 换行符号问题

其实log报错已经非常明确了,是linux无法解析$'\r'。这其实是windows与linux系统的差异导致的。
因为linux上的换行符为\n,而windows上的换行符为\r\n。所以脚本到linux上就无法解析了。
dos2unix SPECS/run.spec
### 如果报command not found 是因为你没有安装,安装dos2unix插件即可。
yum -y install dos2unix*
dos2unix SPECS/run.spec
重新构建
rpmbuild -ba SPECS/run.spec
完成
查看生成的rpm包
cd /root/rpmbuild/RPMS

2、运行rpm包
查看rpm包内目录:
rpm -qpl rpmbuild/RPMS/x86_64/run-0.1-1.el7.centos.x86_64.rpm
rpm -qpl rpmbuild/RPMS/x86_64/runyydy-0.1-1.el7.centos.x86_64.rpm

cd /root/rpmbuild
rpm -ivh RPMS/x86_64/run-0.1-1.el7.centos.x86_64.rpm
通过netstat -tlnp 查看本jar的端口是否正常运行
运行ok
spec中有很多配置说明,需要用户自己百度了,还有很多宏变量可以通过以下命令进行查看实际目录:
rpm --eval "%{buildroot}"

rpm --eval "%{_datadir}"

但是未解决开机启动,需要进行systemd的service配置,下篇进行配置