<!-- 配置服务器上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>
yum install rpm-build
mvn clean package -Dmaven.test.skip
安装:
rpm -ivh demo-xxx.rpm
卸载:
rpm -e demo-xxx
安装后起停项目与普通tomcat的web项目一致
<!--
<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>
yum install rpm-build
mvn clean package -Dmaven.test.skip
安装:
rpm -ivh demo-xxx.rpm
卸载:
rpm -e demo-xxx
使用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