背景:有时候以源码编译出二进制包,想以此制作 rpm包提供给别人安装使用
1. 安装rpm制作工具包,执行命令在 ~ 目录下创建rpmbuild目录结构
#yum install rpmdevtools,rpmbuild
#rpmdev-setuptree
├── BUILD
├── BUILDROOT
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS
2. 将源代码打成 tar.gz 包,放在 rpmbuild/SOURCES下,源码目录命名方式 xxx-版本号
tar czvf xxx-1.0.tar.gz xxx-1.0/
cp xxx-1.0.tar.gz ~/rpmbuild/RESOUCES
3. 进入rpmbuild/SPECS,创建文件 xx.spec,文件名随意。里面具体的每个字段意思自行 Google,还使用了些内置的环境变量。build 和 install 需根据具体的情况进行改写。最终的文件安装在 /usr/bin/gohdfs。buildreuires处写执行该二进制需要依赖的包
Name:hdfs-ultima
Version:1.0
Release: 1%{?dist}
Summary:gohdfs for ultima with kerberos
Group:Development/Tools
BuildRoot: %{_topdir}/BUILDROOT
License:GPL
URL:https://github.com/colinmarc/hdfs
Source0:%{name}-%{version}.tar.gz
#patch0:a.patch
BuildRequires:golang,automake
Requires:golang >= 1.11.3
%description
gohdfs for ultima with kerberos
%prep
cd $RPM_BUILD_DIR
pwd
tar xzvf ../SOURCES/%{name}-%{version}.tar.gz
%setup -q
#%patch0 -p1
%build
cd %{_topdir}/SOURCES/hdfs-ultima-1.0
make
%install
rm -rf $RPM_BUILD_ROOT/*
mkdir -p $RPM_BUILD_ROOT/usr/bin/
cp hdfs $RPM_BUILD_ROOT/usr/bin/gohdfs
%clean
rm -rf %{buildroot}
%files
/usr/bin/gohdfs
%define __debug_install_post \
%{_rpmconfigdir}/find-debuginfo.sh %{?_find_debuginfo_opts} "%{_builddir}/%{?buildsubdir}"\
%{nil}
如果你是mvn工程,可类似如下编写 build 、 install 和 file 部分。需将二进制和依赖的jar包,放在系统环境能读到的地方
%build
cd $RPM_BUILD_DIR/profile-1.0
mvn clean package
%install
ARTHAS_VERSION=3.0.6-SNAPSHOT
rm -rf $RPM_BUILD_ROOT/*
mkdir -p $RPM_BUILD_ROOT/usr/bin/
mkdir -p $RPM_BUILD_ROOT/usr/lib/arthas
cd $RPM_BUILD_DIR/profile-1.0/packaging/target
unzip arthas-${ARTHAS_VERSION}-bin.zip
cp *.jar $RPM_BUILD_ROOT/usr/lib/arthas/
cp as.sh $RPM_BUILD_ROOT/usr/bin/arthas
%clean
# rm -rf %{buildroot}
%files
/usr/bin/arthas
/usr/lib/arthas/arthas-agent.jar
/usr/lib/arthas/arthas-boot.jar
/usr/lib/arthas/arthas-client.jar
/usr/lib/arthas/arthas-core.jar
/usr/lib/arthas/arthas-demo.jar
/usr/lib/arthas/arthas-spy.jar
4. 编写好spec文件后,执行编译命令,在RPMS下去找最终的 rpm 包
rpmbuild -bb xxx.spec
rpmbuild
-ba 既生成src.rpm又生成二进制rpm
-bs 只生成src的rpm
-bb 只生二进制的rpm
-bp 执行到pre
-bc 执行到 build段
-bi 执行install段
-bl 检测有文件没包含
5. 找到 编译后的 rpm包,在需要安装的机器上只需执行 rpm -ivh xxx.rpm 即可安装
参考文献
https://blog.csdn.net/u012373815/article/details/73257754
https://blog.51cto.com/laoguang/1103628