maven使用rpm-maven-plugin构建RPM包

郭华美
2023-12-01

rpm-maven-plugin 插件使用说明

1.web项目打rpm包

  • 说明:
    1. mvn package -Dmaven.test.skip=true 打包生成rpm包
    2. 安装rpm包:将编译后代码安装指定的tomat目录下(tomcat_home配置)
    3. 卸载rpm包:自动清除已经安装的文件

1.1 在pom 文件添加插件配置

    <!-- 配置服务器上tomcat的安装根目录 
    <properties>
        <tomcat_home>/usr/soft/tomcat</tomcat_home>
    </properties>
    -->
    <!-- 打包RPM包 mvn package -Dmaven.test.skip=true   -->
    <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>
            <prefix>${tomcat_home}</prefix><!-- tomcat目录  -->
            <copyright>2018, wwww.myron.com</copyright>
            <distribution>myron</distribution>
            <group>myron.com</group>
            <packager>myron</packager>
            <version>${project.version}</version>
            <autoRequires>true</autoRequires>
            <release>3</release>
            <requires>
                <require>java-1.7.0 >= 1.7</require>
            </requires>
            <mappings>
                <mapping>
                    <!-- 安装rpm后指向的web安装目录   -->
                    <directory>${tomcat_home}/webapps/${project.artifactId}</directory>
                    <filemode>755</filemode>
                    <username>root</username>
                    <groupname>root</groupname>
                    <sources>
                        <source>
                            <location>target/${project.artifactId}-${project.version}</location>
                        </source>
                    </sources>
                </mapping>
            </mappings>
            <!-- rpm安装后执行的脚本-->
            <postinstallScriptlet>
                <script>echo "install success!"</script>
            </postinstallScriptlet>
        </configuration>
    </plugin>

1.2 linux中rpm-build的安装

yum install rpm-build

1.3 打包项目

mvn clean package -Dmaven.test.skip

1.4 rpm 安装与卸载

  • rpm文件位置:target/rpm/demo-xxx/RPMS/noarch/demo-xxx.rpm
安装:
rpm -ivh demo-xxx.rpm
卸载:
rpm -e demo-xxx

1.5 启动/停止

安装后起停项目与普通tomcat的web项目一致

2.非web项目打rpm包(针对可执行jar包)

  • 说明:
    1. 安装jar至服务器指定位置(其实就是复制jar)
    2. 配置软链接 注册服务 实现 systemctl start/stop/restart myapp 起停服务(linux 7)
    3. 安装相关脚本或插入服务相关脚本(根据实际需要)

1. pom文件补充下面配置

    <!--
    <properties>
        <java.version>1.8</java.version>
        <rpm.install.path>/usr/soft/app</rpm.install.path>
        <rpm.prefix>/usr/soft/app</rpm.prefix>
    </properties>
    -->
<build>
    <plugins>
        <!-- 打包RPM包: mvn package -Dmaven.test.skip=true   -->
        <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>
                <prefix>${rpm.prefix}</prefix>
                <copyright>2018, wwww.myron.com</copyright>
                <distribution>myron</distribution>
                <group>myron.com</group>
                <packager>hello</packager>
                <version>${project.version}</version>
                <autoRequires>true</autoRequires>
                <release>3</release>
                <requires>
                    <require>java-1.7.0 >= 1.7</require>
                </requires>
                <mappings>
                    <mapping>
                        <!-- 安装rpm后指向的服务器安装目录  -->
                        <directory>${rpm.install.path}/${project.artifactId}</directory>
                        <filemode>755</filemode>
                        <username>root</username>
                        <groupname>root</groupname>
                        <sources>
                            <source>
                                <location>target/${project.artifactId}-${project.version}.jar</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>src/bin</location>
                            </source>
                        </sources>
                    </mapping>

                    <!--配置软连接注册服务起停项目,相当于:ln -sf myapp.jar /etc/init.d/myapp)
                        启动: systemctl start myapp
                        停止: systemctl stop myapp
                        重启: systemctl restart myapp
                        查看日志: journalctl -u myapp-->
                    <mapping>
                        <directory>/etc/init.d</directory>
                        <filemode>750</filemode>
                        <username>root</username>
                        <groupname>root</groupname>
                        <sources>
                            <softlinkSource>
                                <location>${rpm.install.path}/${project.artifactId}/${project.artifactId}-${project.version}.jar</location>
                                <destination>${project.artifactId}</destination>
                            </softlinkSource>
                        </sources>
                    </mapping>
                </mappings>
                <preinstallScriptlet>
                    <script>echo "installing ${project.name} now"</script>
                </preinstallScriptlet>
                <postinstallScriptlet>
                    <!-- 通过软链接 配置"service demo-swagger2 " 相关操作命令启动-->
                    <!-- 使用上面softlinkSource配置替代
                    <script>
                        rm -f /etc/init.d/${project.artifactId};
                        ln -sf ${rpm.install.path}/${project.artifactId}/bin/startup.sh /etc/init.d/demo-swagger2;
                    </script>
                    -->
                </postinstallScriptlet>
                <preremoveScriptlet>
                    <script>
                        <!--rm -f /etc/init.d/${project.artifactId};-->
                        echo "uninstalling ${project.name} success";
                    </script>
                    <!-- 引用脚本方式
                    <scriptFile>src/main/scripts/preremove</scriptFile>
                    <fileEncoding>utf-8</fileEncoding>
                    -->
                </preremoveScriptlet>
            </configuration>
        </plugin>
    </plugins>
</build>

2.2 linux中rpm-build的安装

yum install rpm-build

2.3 打包项目

mvn clean package -Dmaven.test.skip

2.4 rpm 安装与卸载

  • rpm文件位置:target/rpm/demo-xxx/RPMS/noarch/demo-xxx.rpm
安装:
rpm -ivh demo-xxx.rpm
卸载:
rpm -e demo-xxx

2.5 起停操作

使用Linux 7 以后服务新的启动方式

启动
systemctl start myapp
停止
systemctl stop myapp
重启
systemctl restart myapp
查看日志
journalctl -u myapp

参考文档:
1. 《maven之如何打rpm包》:https://blog.csdn.net/zxjxingkong/article/details/65442990
2. 《Maven RPM Plugin - Sample Configuration》 http://www.mojohaus.org/rpm-maven-plugin/example1.html
3. 《RPM包rpmbuild SPEC文件深度说明》 http://hlee.iteye.com/blog/343499

 类似资料: