通过 rpmbuild 打包 rpm

本文介绍如何使用rpmbuild工具从源代码构建RPM包,包括编译、打包、安装等步骤,适用于在Linux环境下进行软件部署。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过 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:       abc@xxxx.com
    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/*

 

### 使用 `rpmbuild` 打包 Nginx 的教程 #### 准备工作 为了使用 `rpmbuild` 工具来构建 Nginx RPM 包,需要先安装必要的工具并创建所需的目录结构。 ```bash sudo yum install rpm-build gcc make wget tar pcre-devel zlib-devel openssl-devel mkdir -pv ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros ``` #### 下载 Nginx 源码 获取最新的稳定版本的 Nginx 压缩包,并将其放置到 `SOURCES` 文件夹中: ```bash cd ~ wget http://nginx.org/download/nginx-1.20.1.tar.gz mv nginx-1.20.1.tar.gz rpmbuild/SOURCES/ ``` #### 编写 SPEC 文件 SPEC 文件定义了如何编译和打包软件。这里提供了一个简单的模板作为起点[^1]。 ```spec Name: nginx Version: 1.20.1 Release: 1%{?dist} Summary: A high performance web server and reverse proxy server. License: BSD-like URL: https://www.nginx.com/ Source0: %{name}-%{version}.tar.gz BuildRequires: gcc, make, pcre-devel, zlib-devel, openssl-devel %description Nginx (engine x) is a HTTP server which can be used as a reverse proxy, load balancer, mail proxy and HTTP cache. %prep %setup -q %build ./configure \ --prefix=/usr/share/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/lock/subsys/nginx \ --user=nginx \ --group=nginx \ --with-file-aio \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_xslt_module=dynamic \ --with-http_image_filter_module=dynamic \ --with-http_geoip_module=dynamic \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_degradation_module \ --with-http_slice_module \ --with-mail \ --with-mail_ssl_module \ --with-stream \ --with-stream_ssl_module \ --with-stream_realip_module \ --with-stream_geoip_module=dynamic \ --with-debug \ --with-pcre-jit \ --with-compat \ --add-dynamic-module=njs/src/njs_module.c make %{?_smp_mflags} %install rm -rf $RPM_BUILD_ROOT make DESTDIR=$RPM_BUILD_ROOT install %files /usr/sbin/nginx /etc/nginx/* /var/log/nginx/*.log ... %changelog * Thu Jan 01 2023 Your Name <your.email@example.com> - 1.20.1-1 - Initial build. ``` 保存上述内容至 `~/rpmbuild/SPECS/nginx.spec` 文件内。 #### 构建 RPM 包 完成以上准备工作之后,在终端执行如下命令即可开始构建过程: ```bash rpmbuild -ba ~/rpmbuild/SPECS/nginx.spec ``` 这将会读取 SPEC 文件中的指令来进行源代码解压、配置、编译以及最终生成二进制形式的 `.rpm` 文件存放在 `/root/rpmbuild/RPMS/x86_64/` 路径下;同时也会产出 SRPM 存储于 `/root/rpmbuild/SRPMS/` 中。 #### 签名 RPM 包 如果希望给新制作好的 RPM 添加 GPG 数字签名,则可以在 `~/.rpmmacros` 配置文件里加入相应的选项[^2]: ```plaintext %_signature gpg %_gpg_path /root/.gnupg/ %_gpg_name rpmbuild %_gpgbin /usr/bin/gpg ``` 接着通过下面两种方法之一为已有的 RPM 加密签名: - 方法一:单独对某个特定的 RPM 进行签名操作: ```bash rpm --addsign ~/rpmbuild/RPMS/x86_64/nginx-*.rpm ``` - 方法二:在实际构建过程中直接加上签名功能: ```bash rpmbuild --sign --bb ~/rpmbuild/SPECS/nginx.spec ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值