通过 rpmbuild 打包 rpm 文件的命令
# 编译 my-test-app
rm -r target
mvn clean install -Dmaven.test.skip=true
cd target
tar -cvzf my-test-app.tar.gz ./my-test-app
# $rpmbuild 为负责打包的机器,因为我开发机为 mac,而部署机为 linux,故需将文件拷贝到 linux 机器上进行打包 rpm,否则 mac 上生成的 rpm 无法在 linux 上正确安装。
# 清理 linux 机器上可能存在的文件,在 linux 上安装 rpm-build
ssh admin@$rpmbuild "cd /home/admin & rm -r my-test-app.spec rpmbuild/SOURCES/my-test-app.tar.gz rpmbuild/RPMS/x86_64/my-test-app-0.0.1.x86_64.rpm rpmbuild/BUILD/my-test-app & mkdir -p /home/admin/rpmbuild/SOURCES & mkdir -p /home/admin/rpmbuild/BUILD/my-test-app & sudo yum install -y rpm-build"
# 拷贝本地文件到 linux 机器相应文件夹
scp my-test-app.tar.gz admin@$rpmbuild:/home/admin/rpmbuild/SOURCES
scp ../src/main/assembly/my-test-app.spec admin@$rpmbuild:/home/admin
# 执行 rpmbuild,生成 rpm 包
ssh admin@$rpmbuild "rpmbuild -ba /home/admin/my-test-app.spec"
spec 文件
### 0. Define section
### {buildroot} is by default ~/rpmbuild/BUILDROOT/{Name}-{Version}-{Release}
### 1. The introduction section
Name: my-test-app
Version: 0.0.1
Release: 1%{?dist}
Summary: my-test-app
Group: my-test-team
License: Commercial
URL: http://xxxx.com
Packager: [email protected]
Source0: my-test-app.tar.gz
# add Prefix and give a dummy value, the RPM will be relocatable, you can override prefix when installing RPM
# sudo rpm -ivh ~/rpmbuild/RPMS/x86_64/my-test-app-0.0.1.x86_64.rpm --prefix=/home/admin
Prefix: /
%description
my-test-app
### 2. The Prepare section
%prep
# This will unzip sources into ~/rpmbuild/BUILD folder
tar -xzvf %{sources}
### 3. The Build Section
%build
# Current folder is ~/rpmbuild/BUILD
cd ./my-test-app
mvn package -Dmaven.test.skip -Denv=release
### 4. Install Section
%install
mkdir -p %{buildroot}/my-test-app
rsync -av --exclude=**/*DS_Store manager/my-test-app/target/my-test-app/ %{buildroot}/my-test-app
### 5. The files to be packaged into RPM
%files
# Current folder is {buildroot}
/my-test-app
# These files will get the below permissions after you installed RPM
%attr(4755, admin, admin) /my-test-app/bin/*
%attr(4755, admin, admin) /my-test-app/script/*