Maven 打包为rpm包

maven引入 插件并配置

以Spring Boot 为例

<build>
        <finalName>${artifactId}</finalName>
        <plugins>
			<!-- springboot的打包插件必须加上 否则找不到main方法 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>rpm-maven-plugin</artifactId>
                <version>2.1.5</version>
                <extensions>true</extensions>
                <executions>
                    <execution>
                        <goals>
                            <goal>rpm</goal>
                        </goals>
                    </execution>
                </executions>

                <configuration>
                    <!-- 最后打包后的rpm名字为 
                    ${rpm.install.path}-${project.version}-release-noarch.rpm 
                    -->
                    <!-- 打包后的路径  -->
                    <prefix>${rpm.install.path}</prefix>

                    <!-- 描述信息 -->
                    <distribution>maven package rpm test</distribution>

                    <!-- 集团信息 -->
                    <group>${groupId}</group>

                    <!-- 打包作者信息(作者|公司名) -->
                    <packager>gudian</packager>

                    <!-- 版本信息  -->
                    <version>${project.version}</version>
                    <!-- 小版本(可选参数,不填默认为1) -->
                    <release>${rpm.install.release}</release>

                    <!-- 是否自动解决依赖 -->
                    <autoRequires>false</autoRequires>

                    <!-- 依赖其他的rpm包 -->
                    <!--<requires>
                        <require>java-1.8.0 >= 1.8</require>
                    </requires>-->

                    <mappings>

                        <!-- 将相关文件放入rpm包内 -->
                        <mapping>
                            <!-- 安装rpm时指向的服务器安装目录  -->
                            <directory>${rpm.install.path}/${project.artifactId}</directory>
                            <filemode>755</filemode>
                            <username>root</username>
                            <groupname>root</groupname>
                            <sources>
                                <source>
                                    <location>target/${project.artifactId}.jar</location>
                                </source>
                            </sources>
                        </mapping>

                        <mapping>
                            <!-- 安装rpm时指向的服务器安装目录  -->
                            <directory>${rpm.install.path}/${project.artifactId}/resources</directory>
                            <filemode>755</filemode>
                            <username>root</username>
                            <groupname>root</groupname>
                            <sources>
                                <source>
                                    <location>src/main/resources/*</location>
                                </source>
                            </sources>
                        </mapping>

                        <!-- 复制安装相关脚本命令 根据具体项目需要决定是否使用-->
                        <mapping>
                            <directory>${rpm.install.path}/${project.artifactId}/bin</directory>
                            <filemode>750</filemode>
                            <username>root</username>
                            <groupname>root</groupname>
                            <sources>
                                <source>
                                    <location>bin</location>
                                </source>
                            </sources>
                        </mapping>

                        <!-- 软链接启动脚本 -->
                        <mapping>
                            <directory>/etc/init.d</directory>
                            <filemode>750</filemode>
                            <username>root</username>
                            <groupname>root</groupname>
                            <sources>
                                <softlinkSource>
                                    <location>${rpm.install.path}/${project.artifactId}/bin/${project.artifactId}.sh</location>
                                    <!-- 软连接的名称 -->
                                    <destination>${project.artifactId}</destination>
                                </softlinkSource>
                            </sources>
                        </mapping>
                        <!--配置软连接注册服务起停脚本
                            启动: systemctl start bootyum
                            停止: systemctl stop bootyum
                            重启: systemctl restart bootyum
                            查看日志: journalctl -u bootyum -->
                        <mapping>
                            <directory>/usr/lib/systemd/system</directory>
                            <filemode>750</filemode>
                            <username>root</username>
                            <groupname>root</groupname>
                            <sources>
                                <softlinkSource>
                                    <location>${rpm.install.path}/${project.artifactId}/bin/${project.artifactId}.service</location>
                                    <!-- 软连接的名称 -->
                                    <destination>${project.artifactId}.service</destination>
                                </softlinkSource>
                            </sources>
                        </mapping>

                    </mappings>


                    <!-- 安装前操作 -->
                    <preinstallScriptlet>
                        <script>echo " removing ${rpm.install.path}/${project.artifactId}/${project.artifactId}.jar"</script>
                        <script>rm -rf ${rpm.install.path}/${project.artifactId}/${project.artifactId}jar</script>
                        <script>echo "installing ${project.name} now"</script>
                    </preinstallScriptlet>

                    <!-- 安装后操作 -->
                    <postinstallScriptlet>
                        <script>echo "install successful!"</script>
                        <script>systemctl daemon-reload</script>
                    </postinstallScriptlet>

                    <!-- 卸载前后操作 -->
                    <preremoveScriptlet>
                        <script>echo "uninstalling ${project.name} success";</script>
                        <script>systemctl daemon-reload</script>
                        <!-- 引用脚本方式
                        <scriptFile>src/main/scripts/preremove</scriptFile>
                        <fileEncoding>utf-8</fileEncoding>
                        -->
                    </preremoveScriptlet>
                </configuration>
            </plugin>
        </plugins>
    </build>

相关文件

bootyum.service 注册到 systemctl内用

[Unit]
Documentation=bootyum

[Service]
#设置javaHome(一定要设置,绝对路径,否则java命令找不到),systemctl运行时不会携带系统的环境变量
Environment=JAVA_HOME=/app/jdk/jdk1.8.0_161
User=root
Group=root
Type=forking
Restart=no
KillMode=process

ExecStart=/etc/rc.d/init.d/bootyum start
ExecStop=/etc/rc.d/init.d/bootyum stop

[Install]
WantedBy=multi-user.target

bootyum.sh 注册到 systemctl内用

#!/bin/bash
#设置 环境变量
export PATH=$JAVA_HOME/bin:$PATH

PID_PATH=/application/data/bootyum/bootyum.pid
Service_Name=bootyum

case "$1" in
start)
/application/data/bootyum/bin/start.sh
;;
stop)
kill $(cat ${PID_PATH})
rm ${PID_PATH}
;;
restart)
$0 stop
$0 start
;;
status)
if [ -e ${PID_PATH} ];then
echo "${Service_Name} is running, pid=$(cat ${PID_PATH})"
else
echo "${Service_Name} is NOT running"
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0

start.sh 项目真实的启动脚本

#!/bin/bash

BASE_PATH=/application/data/bootyum
Service_Name=bootyum

echo " start app";

#启动命令
nohup java -jar $BASE_PATH/$Service_Name.jar >> $BASE_PATH/bootyum.log 2>&1 &

#将pid写进文件
echo $! > $BASE_PATH/bootyum.pid

打包

linux 中安装 rpm-build插件

yum install rpm-build

进入项目根路径打包

mvn clean package -Dmaven.test.skip

最后会在 target/rpm/项目名称/RPMS/noarch下生成一个rpm包

执行安装命令

#安装:
rpm -hiv xxx.rpm
#查看信息
rpm -qa xxx
#卸载:
rpm -e xxx

安装成功后执行即可启动

#启动
systemctl start 项目名称
#加入开启自启动
systemctl enable 项目名称
#停止
systemctl stop 项目名称
#重启
systemctl restart 项目名称

更多参考文档:
maven使用rpm-maven-plugin构建RPM包

systemctl java jar 添加jar文件开机启动项

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值