RPM是什么?
RPM 是在 Linux 下广泛使用的软件包管理器,由 Red Hat 开发。它的全称是 RPM Package Manager,和 PHP、GNU 一样,是一个递归的缩写。RPM 软件包后缀名是.rpm。
依赖环境
打包需要在Linux上打包,并且需要以下前置依赖
yum install -y rpm-build rpmdevtools
RPM目录说明
- BUILD:源代码解压以后放的位置,只需提供BUILD目录,具体里面放什么,不用我们管,所以真正的制作车间是BUILD目录。
- BUILDROOT:假根,使用install临时安装到这个目录,把这个目录当作根来用的,所以在这个目录下的目录文件,才是真正的目录文件。当打包完成后,在清理阶段,这个目录将被删除。
- RPMS:制作完成后的rpm包存放目录,为特定平台指定子目录(i386,i686,ppc)。
- SOURCES:收集的源文件,源材料,补丁文件等存放位置。
- SPECS:存放spec文件,作为制作rpm包的领岗文件,文件以.spec结尾。
- SRPMS:src格式的rpm包位置 ,既然是src格式的包,就没有平台的概念了
SpringBoot构建RPM
目录结构
添加插件
使用 maven-jar-plugin + maven-assembly-plugin + rpm-maven-plugin三个插件打包构建
maven-jar-plugin:打包工程成为可执行jar包
maven-assembly-plugin: 使配置文件、启动脚本、依赖包分离,便于增量更新
rpm-maven-plugin: 生成构建RPM安装包
pom.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>project-build</artifactId>
<parent>
<artifactId>spring-projects</artifactId>
<groupId>com.zouuo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<properties>
<rpm.install.location>/opt/soft/${project.artifactId}-${project.version}</rpm.install.location>
<rpm.plugin.phase>none</rpm.plugin.phase>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.zouuo.rpm.RpmBuildApplication</mainClass>
<classpathPrefix>lib</classpathPrefix>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>rpm-maven-plugin</artifactId>
<version>2.2.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>make-rpm</id>
<phase>${rpm.plugin.phase}</phase>
<goals>
<goal>rpm</goal>
</goals>
</execution>
</executions>
<configuration>
<prefix>${rpm.install.location}</prefix>
<distribution>panda</distribution>
<group>panda</group>
<packager>panda</packager>
<version>${project.version}</version>
<autoRequires>true</autoRequires>
<release>1</release>
<targetOS>linux</targetOS>
<needarch>x86_64</needarch>
<defaultDirmode>0755</defaultDirmode>
<defaultFilemode>0644</defaultFilemode>
<defaultUsername>root</defaultUsername>
<defaultGroupname>root</defaultGroupname>
<defineStatements>
<defineStatement>_target_os linux</defineStatement>
</defineStatements>
<mappings>
<mapping>
<!-- 安装rpm后指向的服务器 脚本路径 -->
<directory>${rpm.install.location}</directory>
<username>root</username>
<groupname>root</groupname>
<sources>
<source>
<location>target/${project.artifactId}-${project.version}-bin/${project.artifactId}-${project.version}</location>
</source>
</sources>
</mapping>
<mapping>
<directory>/usr/lib/systemd/system</directory>
<directoryIncluded>false</directoryIncluded>
<filemode>0644</filemode>
<username>root</username>
<groupname>root</groupname>
<sources>
<source>
<location>${project.basedir}/src/main/rpm/setup.service</location>
<filter>true</filter>
<destination>${project.name}.service</destination>
</source>
</sources>
</mapping>
</mappings>
<preinstallScriptlet>
<script>echo "installing ${project.name} now"</script>
</preinstallScriptlet>
<postinstallScriptlet>
<scriptFile>${project.basedir}/src/main/rpm/postinst</scriptFile>
<fileEncoding>UTF-8</fileEncoding>
<filter>true</filter>
</postinstallScriptlet>
<preremoveScriptlet>
<script>
echo "uninstall ${project.name} success";
</script>
</preremoveScriptlet>
</configuration>
</plugin>
</plugins>
</build>
</project>
assembly配置
assembly.xml为maven-assembly-plugin插件配置文件,放于src/main/assembly路径下
<assembly>
<id>bin</id>
<formats>
<format>dir</format>
<format>tar.gz</format>
</formats>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<scope>compile</scope>
</dependencySet>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.basedir}/target/</directory>
<outputDirectory>${file.separator}</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.basedir}/src/main/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
<lineEnding>unix</lineEnding>
</fileSet>
<fileSet>
<directory>${project.basedir}/src/main/resources/</directory>
<outputDirectory>config</outputDirectory>
<includes>
<include>*.yaml</include>
</includes>
</fileSet>
</fileSets>
</assembly>
添加开机自启相关配置
注册开机自启相关配置文件,放于src/main/rpm目录下
postinst 后置脚本
# !/bin/bash
/usr/bin/chmod +x ${rpm.install.location}/bin/*
/bin/systemctl preset ${project.name}
/bin/systemctl daemon-reload
/bin/systemctl --force enable ${project.name}
setup.service配置文件
[Unit]
Description= ${project.name} Application Service
After=network.target
[Service]
Type=forking
User=root
Group=root
WorkingDirectory=${rpm.install.location}
EnvironmentFile=-/etc/default/panda
ExecStart=/bin/bash ${rpm.install.location}/bin/startup.sh
KillMode=control-group
Restart=on-failure
RestartSec=10
StartLimitInterval=600
StartLimitBurst=50
TimeoutStopSec=3s
[Install]
WantedBy=multi-user.target
打包命令
在Linux上使用命令mvn clean install -D rpm.plugin.phase=package 打包,生成的RPM安装包在目录target/rpm目录下
评论区