將java的jar包,打包為rpm 安裝包


一、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
******************


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM