深度学习煤矿输送带异物检测系统(设计源文件+万字报告+讲解)(支持资料、图片参考_相关定制)_文章底部可以扫码
2026/1/16 19:42:18
.spec文件是 RPM 包构建的核心配置文件,包含构建、安装和卸载的所有指令。
# 1. 导言(Preamble) - 元数据部分 Name: package-name Version: 1.0.0 Release: 1%{?dist} Summary: 简短描述 License: GPLv3+ URL: https://example.com Source0: %{name}-%{version}.tar.gz Source1: additional-file.txt Patch0: some-fix.patch # 2. 描述(Description) %description 详细的多行描述,可以跨越多行。 # 3. 准备阶段(%prep) %prep %setup -q # 4. 构建阶段(%build) %build # 5. 安装阶段(%install) %install # 6. 清理阶段(%clean) %clean # 7. 文件列表(%files) %files # 8. 脚本片段(可选) %pre %post %preun %postun # 9. 更改日志(%changelog) %changelogName: package-name # 包名称(小写,无空格) Version: 1.2.3 # 上游版本号 Release: 1%{?dist} # 发行号(每次构建递增) # %{?dist} 是发行版标识符(如 .el7, .fc38) Summary: Brief description # 简短摘要(单行) Group: Applications/System # 软件组别(已弃用,但某些地方仍用) License: GPLv3+ # 许可证(SPDX格式) URL: https://example.com # 项目主页# 源代码文件(支持多个,从0开始编号) Source0: %{name}-%{version}.tar.gz Source1: config-file.conf Source2: http://example.com/files/extra.tar.bz2 # 补丁文件 Patch0: 0001-fix-bug.patch Patch1: security-fix.patch # 源文件位置可以是: # - 本地文件(在 SOURCES 目录) # - URL(构建时会自动下载) # - 带通配符(Source: *.tar.gz)# 构建依赖(BuildRequires) BuildRequires: gcc BuildRequires: make BuildRequires: pkgconfig(openssl) # 使用pkgconfig查询 BuildRequires: cmake >= 3.10 BuildRequires: perl(Test::More) # Perl模块 # 运行时依赖(Requires) Requires: bash Requires: /usr/bin/python3 # 文件依赖(自动解析) Requires: libssl.so.1.1()(64bit) # 库依赖 Requires: systemd # 推荐依赖(Recommends) - 可选但推荐 Recommends: some-optional-tool # 建议依赖(Suggests) - 可选的 Suggests: optional-plugin # 冲突依赖(Conflicts) Conflicts: old-package < 2.0 # 替换/废弃(Obsoletes) Obsoletes: deprecated-package # 提供(Provides) Provides: alternative-name = %{version} Provides: bundled(some-lib) = 1.0 # 捆绑库声明%prep # 解压源代码并应用补丁 %setup -q -n %{name}-%{version} # -q 安静模式,-n 指定目录名 %patch0 -p1 # 应用补丁,-p 表示strip的层级 # 自定义准备步骤 rm -rf vendor/bundled-lib # 删除捆绑库 cp %{SOURCE1} . # 复制额外文件 sed -i 's/old/new/' config.txt # 修改配置文件%build # 配置步骤(可选) %configure \ --prefix=%{_prefix} \ --sysconfdir=%{_sysconfdir} \ --disable-static # 或者使用 CMake %cmake -DCMAKE_BUILD_TYPE=Release # 或者手动配置 ./configure --prefix=/usr # 编译 make %{?_smp_mflags} # 支持并行编译 # 构建其他部分 %make_build doc # 构建文档%install # 清理构建根目录 rm -rf %{buildroot} # 安装到构建根目录 make install DESTDIR=%{buildroot} # 或手动安装 install -d %{buildroot}%{_bindir} install -m 755 program %{buildroot}%{_bindir}/ # 处理文档 mkdir -p %{buildroot}%{_docdir}/%{name} cp -r docs/* %{buildroot}%{_docdir}/%{name}/ # 处理配置文件(noreplace防止覆盖用户配置) install -m 644 -D config.conf %{buildroot}%{_sysconfdir}/%{name}/%{name}.conf echo "%config(noreplace) %{_sysconfdir}/%{name}/%{name}.conf" >> filelist.txt%check # 运行测试套件 make test # 或使用 %make_check %make_check%clean rm -rf %{buildroot}%files # 文件权限宏 %defattr(-,root,root,-) # 默认属性(文件模式,所有者,组,目录模式) # - 表示保持原样 # 目录 %dir /some/directory # 标记为空目录 # 配置文件 %config(noreplace) /etc/config.conf # 不覆盖现有配置 %config /etc/another.conf # 允许覆盖 # 文档文件 %doc README LICENSE %doc docs/* # 许可证文件 %license COPYING # 重要文件标记 %ghost /var/lib/%{name}/data.db # 文件由包创建,但内容不包含在RPM中 %attr(755, root, root) /usr/bin/program # 自定义属性 # 通配符 /usr/bin/* /usr/lib/%{name}/*.so%files %defattr(-,root,root,-) # 主程序文件 %{_bindir}/program %{_sbindir}/daemon # 库文件 %{_libdir}/%{name}/lib*.so # 配置文件 %config(noreplace) %{_sysconfdir}/%{name}/%{name}.conf # 文档 %doc README.md CHANGELOG %doc %{_docdir}/%{name}/ # 手册页 %{_mandir}/man1/program.1.gz %{_mandir}/man8/daemon.8.gz # Systemd 单元文件 %{_unitdir}/%{name}.service # 许可证 %license LICENSE%files devel # 开发子包 %defattr(-,root,root,-) %{_includedir}/%{name}/ %{_libdir}/*.so %{_libdir}/pkgconfig/*.pc %files docs # 文档子包 %doc %{_docdir}/%{name}/%pre # 安装前执行 %post # 安装后执行 %preun # 卸载前执行 %postun # 卸载后执行%pre # 检查用户是否存在,不存在则创建 getent group %{name} >/dev/null || groupadd -r %{name} getent passwd %{name} >/dev/null || \ useradd -r -g %{name} -d /var/lib/%{name} -s /sbin/nologin %{name} %post # 安装后配置 systemctl daemon-reload >/dev/null 2>&1 || : %preun # 卸载前检查是否在运行 if [ $1 -eq 0 ]; then # 最后一个版本将被移除 systemctl stop %{name}.service >/dev/null 2>&1 || : fi %postun # 清理工作 if [ $1 -ge 1 ]; then # 升级时 systemctl try-restart %{name}.service >/dev/null 2>&1 || : fi# 标准目录宏 %{_bindir} # /usr/bin %{_sbindir} # /usr/sbin %{_libdir} # /usr/lib 或 /usr/lib64 %{_libexecdir} # /usr/libexec %{_includedir} # /usr/include %{_datadir} # /usr/share %{_mandir} # /usr/share/man %{_docdir} # /usr/share/doc %{_sysconfdir} # /etc %{_localstatedir} # /var %{_prefix} # /usr %{_unitdir} # /usr/lib/systemd/system # 构建目录宏 %{buildroot} # 构建根目录 %{_builddir} # 构建目录 %{_sourcedir} # 源码目录 %{_specdir} # spec文件目录 %{_rpmdir} # RPM输出目录 %{_srcrpmdir} # SRPM输出目录%if 0%{?rhel} >= 8 BuildRequires: new-dependency %else BuildRequires: old-dependency %endif %ifarch x86_64 Requires: x64-libs %endif %ifnarch noarch Requires: arch-specific %endif # 使用 bcond_with/without %bcond_with tests # 默认不带tests %if %{with tests} BuildRequires: gtest-devel %endif# 主包包含什么 %files # 开发子包 %package devel Summary: Development files for %{name} Requires: %{name} = %{version}-%{release} %description devel This package contains development files. %files devel # 文档子包 %package doc Summary: Documentation for %{name} Requires: %{name} = %{version}-%{release} %description doc This package contains documentation. %files doc # 子包特定依赖 %package -n separate-name Summary: Separate package Requires: some-dependency%changelog * Fri Jan 12 2024 John Doe <john@example.com> - 1.2.3-2 - Rebuild for new openssl version - Fix security issue CVE-2023-XXXX * Mon Nov 20 2023 Jane Smith <jane@example.com> - 1.2.3-1 - Initial package - Added man pages - Fixed log rotation config格式要求:
每行以*开头
日期格式:星期 月 日 年
打包者姓名和邮箱
版本号
更改条目(以-开头)
%triggerin -- old-package # 当安装old-package时触发 %triggerun -- old-package # 当卸载old-package时触发 %triggerpostun -- old-package # 卸载old-package后触发%filetriggerin -- /etc/cron.daily # 当文件添加到该目录时触发 %filetriggerun -- /etc/cron.daily # 当文件从该目录移除时触发%files -f filelist.txt # 从文件读取文件列表# 自动查找依赖 AutoReqProv: yes # 默认开启 AutoReq: yes # 自动添加Requires AutoProv: yes # 自动添加ProvidesName: myapp Version: 2.1.0 Release: 1%{?dist} Summary: A sample application License: GPLv3+ URL: https://github.com/example/myapp Source0: https://github.com/example/%{name}/archive/v%{version}.tar.gz Patch0: fix-build.patch BuildRequires: gcc BuildRequires: make BuildRequires: pkgconfig(openssl) Requires: bash Requires: systemd %description MyApp is a sample application that demonstrates RPM packaging. %prep %setup -q -n %{name}-%{version} %patch0 -p1 %build %configure --disable-static make %{?_smp_mflags} %install rm -rf %{buildroot} make install DESTDIR=%{buildroot} # 安装systemd服务文件 install -d %{buildroot}%{_unitdir} install -m 644 %{name}.service %{buildroot}%{_unitdir}/ %clean rm -rf %{buildroot} %files %defattr(-,root,root,-) %license LICENSE %doc README.md %{_bindir}/myapp %{_sysconfdir}/myapp.conf %config(noreplace) %{_sysconfdir}/myapp.d/*.conf %{_unitdir}/myapp.service %changelog * Fri Jan 12 2024 Packager <packager@example.com> - 2.1.0-1 - Initial RPM package# 语法检查 rpmlint myapp.spec # 仅准备阶段(测试解压和补丁) rpmbuild -bp myapp.spec # 构建到安装阶段 rpmbuild -bi myapp.spec # 完整构建 rpmbuild -ba myapp.spec # 不执行 %clean(便于调试) rpmbuild -ba --noclean myapp.spec # 使用其他构建根目录 rpmbuild -ba --buildroot=/tmp/mybroot myapp.spec使用宏:提高可移植性
清理 buildroot:确保干净构建
标记配置文件:使用%config(noreplace)保护用户配置
版本控制:Release 字段随每次构建递增
详细描述:%description 提供有用信息
测试脚本:包含 %check 阶段
处理系统用户:在 %pre 脚本中创建
支持重定位:使用变量而非硬编码路径
通过熟练掌握.spec文件的各个部分,可以创建高质量、可维护的 RPM 软件包。