一、rpm包 介绍
RPM Package Manager (RPM) 是一个强大的命令行驱动的软件包管理工具,用来安装、卸载、校验、查询和更新 Linux 系统上的软件包
二、环境安装
- 一台centos7系统
- 准备一个spring boot 项目jar 包 ,举个栗子:spring-starter-test-0.1.0.jar
- 开始安装依赖工具包
- 1、rpmbuild-用于生成rpm的实用程序
- 2、rpmdev-开发实用程序以生成rpm。
- 3、rpmlint-用于检查rpm包中常见错误的工具。
[root@localhost ~]# sudo yum install rpm-build
Loaded plugins: fastestmirror, langpacks
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirror.bit.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package rpm-build.x86_64 0:4.11.3-43.el7 will be installed
--> Processing Dependency: rpm = 4.11.3-43.el7 for package: rpm-build-4.11.3-43.el7.x86_64
--> Processing Dependency: system-rpm-config for package: rpm-build-4.11.3-43.el7.x86_64
--> Processing Dependency: perl(Thread::Queue) for package: rpm-build-4.11.3-43.el7.x86_64
--> Running transaction check
************************************************************
Installed:
rpm-build.x86_64 0:4.11.3-43.el7
Dependency Installed:
dwz.x86_64 0:0.11-3.el7 perl-Thread-Queue.noarch 0:3.02-2.el7 perl-srpm-macros.noarch 0:1-8.el7 python-srpm-macros.noarch 0:3-32.el7 redhat-rpm-config.noarch 0:9.1.0-88.el7.centos
Dependency Updated:
rpm.x86_64 0:4.11.3-43.el7 rpm-build-libs.x86_64 0:4.11.3-43.el7 rpm-libs.x86_64 0:4.11.3-43.el7 rpm-python.x86_64 0:4.11.3-43.el7
Complete!
[root@localhost ~]# sudo yum install rpmdevtools
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirror.bit.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package rpmdevtools.noarch 0:8.3-5.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================================================================================
Installing:
rpmdevtools noarch 8.3-5.el7 base 97 k
Transaction Summary
==============================================================================================================================================================================================================
Install 1 Package
Total download size: 97 k
Installed size: 209 k
Is this ok [y/d/N]: y
Downloading packages:
rpmdevtools-8.3-5.el7.noarch.rpm | 97 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : rpmdevtools-8.3-5.el7.noarch 1/1
Verifying : rpmdevtools-8.3-5.el7.noarch 1/1
Installed:
rpmdevtools.noarch 0:8.3-5.el7
Complete!
[root@localhost ~]# sudo yum install rpmlint
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirror.bit.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package rpmlint.noarch 0:1.5-4.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================================================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================================================================================================================================================
Installing:
rpmlint noarch 1.5-4.el7 base 229 k
Transaction Summary
==============================================================================================================================================================================================================================================================================
Install 1 Package
Total download size: 229 k
Installed size: 1.2 M
Is this ok [y/d/N]: y
Downloading packages:
rpmlint-1.5-4.el7.noarch.rpm | 229 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : rpmlint-1.5-4.el7.noarch 1/1
Verifying : rpmlint-1.5-4.el7.noarch 1/1
Installed:
rpmlint.noarch 0:1.5-4.el7
Complete!
- 安装完成!
三、项目设置
1、创建初始rpm文件夹结构。
[root@localhost ~]# cd ~; mkdir rpmbuild; cd ~/rpmbuild; rpmdev-setuptree
[root@localhost rpmbuild]# pwd
/root/rpmbuild
[root@localhost rpmbuild]#
2、将jar项目文件包文件复制到源文件夹中。
[root@localhost ~]# cp /root/package/spring-starter-test-0.1.0.jar SOURCES/
注意:示例服务将由systemd管理,因此我们还必须将服务文件打包到rpm中。
创建vim SOURCES/spring-starter-test.service具有以下内容的文件:
[root@localhost rpmbuild]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug 6 03:35 BUILD
drwxr-xr-x. 2 root root 6 Aug 6 03:35 RPMS
drwxr-xr-x. 2 root root 6 Aug 6 03:35 SOURCES
drwxr-xr-x. 2 root root 6 Aug 6 03:35 SPECS
drwxr-xr-x. 2 root root 6 Aug 6 03:35 SRPMS
[root@localhost rpmbuild]# vim SOURCES/spring-starter-test.service
文件内容为:
[Unit]
Description=Spring Starter
After=network-online.target
[Service]
Type=simple
WorkingDirectory=/var/opt/spring-starter-test
ExecStart=/usr/bin/java -jar /usr/local/spring-starter-test/spring-starter-test.jar
Restart=on-abort
User=spring-starter
Group=spring-starter
[Install]
WantedBy=multi-user.targe
四、开始编写规范文件
规范文件定义了rpm构建过程和安装过程。
为我们的spring starter应用程序创建一个规范文件。
[root@localhost rpmbuild]# vim SPECS/spring-starter-test.spec
并设置以下内容:
##### HEADER SECTION #####
Name: spring-starter-test
Version: 0.1.0
Release: 0
Summary: Rpm package for Spring Starter
License: ASL 2.0
URL: https://spring.io
Source0: spring-starter-test-%{version}.jar
Source1: %{name}.service
Requires: shadow-utils,bash
BuildRequires: systemd
%{?systemd_requires}
BuildArch: noarch
%description
%{summary}
# disable debuginfo, which is useless on binary-only packages
%define debug_package %{nil}
# do not repack jar files
%define __jar_repack %{nil}
##### PREPARATION SECTION #####
%prep
# empty section
##### BUILD SECTION #####
%build
# empty section
##### PREINSTALL SECTION #####
%pre
# create Spring Starter service group
getent group spring-starter >/dev/null || groupadd -f -g 30000 -r spring-starter
# create Spring Starter service user
if ! getent passwd spring-starter >/dev/null ; then
if ! getent passwd 30000 >/dev/null ; then
useradd -r -u 30000 -g spring-starter -d /home/spring-starter -s /sbin/nologin -c "Spring Starter service account" spring-starter
else
useradd -r -g spring-starter -d /home/spring-starter -s /sbin/nologin -c "Spring Starter service account" spring-starter
fi
fi
exit 0
##### INSTALL SECTION #####
%install
app_dir=%{buildroot}/usr/local/spring-starter-test
data_dir=%{buildroot}/var/opt/spring-starter-test
service_dir=%{buildroot}/%{_unitdir}
# cleanup build root
rm -rf %{buildroot}
mkdir -p %{buildroot}
# create app folder
mkdir -p $app_dir
# create data folder
mkdir -p $data_dir
# create service folder
mkdir -p $service_dir
# copy all files
cp %{SOURCE0} $app_dir/spring-starter-test.jar
cp %{SOURCE1} $service_dir
##### FILES SECTION #####
%files
# define default file attributes
%defattr(-,spring-starter,spring-starter,-)
# list of directories that are packaged
%dir /usr/local/spring-starter-test
%dir %attr(660, -, -) /var/opt/spring-starter-test
# list of files that are packaged
/usr/local/spring-starter/spring-starter-test.jar
/usr/lib/systemd/system/%{name}.service
##### POST INSTALL SECTION #####
%post
# ensure Spring Starter service is enabled and running
%systemd_post %{name}.service
%{_bindir}/systemctl enable %{name}.service
%{_bindir}/systemctl start %{name}.service
##### UNINSTALL SECTION #####
%preun
# ensure Spring Starter service is disabled and stopped
%systemd_preun %{name}.service
%postun
case "$1" in
0) # This is a package remove
# remove app and data folders
rm -rf /usr/local/spring-starter-test
rm -rf /var/opt/spring-starter-test
# remove Spring Starter service user and group
userdel spring-starter
;;
1) # This is a package upgrade
# do nothing
;;
esac
# ensure Spring Starter service restartet if an upgrade is performed
%systemd_postun_with_restart %{name}.service
##### CHANGELOG SECTION #####
%changelog
* August 6, 2020 date liuxd <liuxd1314@sina.com> - 0.1.0-0
- First spring-starter-test package
- 此处有一个注意点是:%define __jar_repack %{nil}定义。此选项禁用.jar文件压缩。如果jar文件被压缩,它将变得无法执行。
五、开始构建进行运行
- 构建
开始验证文件内容正确性
# Rpmlint之后会有很多错误及警告,大多数警告可以忽略。
[root@localhost rpmbuild]# rpmbuild -ba SPECS/spring-starter.spec
- 正式开始构建
[root@localhost rpmbuild]# rpmbuild -ba SPECS/spring-starter-test.spec
- 命令说明:
1、 完全打包
rpmbuild -ba xxx.spe
2、只需要生成完整的源文件
rpmbuild -bp xxx.spec - 已经完成打包,我们开始安装成果
#生成后的安装包是在: RPMS/noarch/spring-starter-test-0.1.0-0.noarch.rpm
[root@localhost noarch]# pwd
/root/rpmbuild/RPMS/noarch/spring-starter-test-0.1.0-0.noarch.rpm
[root@localhost rpmbuild]# rpm -ivh RPMS/noarch/spring-starter-test-0.1.0-0.noarch.rpm
六、安装完成,我们可以开始访问项目接口了
[root@localhost rpmbuild]# systemctl status spring-starter-test
[root@localhost rpmbuild]# curl http://127.0.0.1:8080/test/user/login?username=test001&password=123456
******************