JBoss EAP 6:jboss-as-maven-plugin undeploy不存在WAR时失败求助
解决JBoss EAP 6中undeploy步骤不存在时失败的问题
我之前在用jboss-as-maven-plugin部署WAR包时也碰到过一模一样的问题——undeploy步骤在应用没提前部署的情况下直接中断构建,特别影响效率。这里给你几个实用的解决方案,按需选择就行:
方案1:让undeploy失败时不终止构建
这是最直接的办法,利用插件自带的failOnError参数,把它设为false。这样就算WAR包没部署过,undeploy步骤报错也不会打断后续的部署流程。
在你的pom.xml插件配置里加上这个参数:
<plugin> <groupId>org.jboss.as.plugins</groupId> <artifactId>jboss-as-maven-plugin</artifactId> <version>7.9.Final</version> <!-- 替换成你实际用的插件版本 --> <configuration> <hostname>your-jboss-host</hostname> <port>9999</port> <!-- JBoss管理端口 --> <failOnError>false</failOnError> <!-- 关键配置 --> <name>your-app.war</name> </configuration> <executions> <execution> <id>undeploy</id> <phase>prepare-package</phase> <goals> <goal>undeploy</goal> </goals> </execution> <execution> <id>deploy</id> <phase>install</phase> <goals> <goal>deploy</goal> </goals> </execution> </executions> </plugin>
方案2:先检查应用是否存在再执行undeploy
如果不想看到undeploy的报错日志,可以先通过JBoss CLI检查目标WAR是否已部署,只有存在的时候才执行undeploy步骤。这里结合Maven的exec-maven-plugin来实现:
首先添加exec插件配置,用来检查部署状态:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>check-deployment</id> <phase>prepare-package</phase> <goals> <goal>exec</goal> </goals> <configuration> <!-- Windows环境替换成jboss-cli.bat --> <executable>jboss-cli.sh</executable> <arguments> <argument>-c</argument> <argument>deployment-info --name=your-app.war</argument> </arguments> <!-- 检查失败时不终止构建 --> <successCodes>0,1</successCodes> <!-- 将执行返回码保存为属性,后续判断用 --> <outputProperty>deployment.exists</outputProperty> </configuration> </execution> </executions> </plugin>
然后修改jboss-as-maven-plugin的undeploy执行逻辑,只有当deployment.exists为0(应用存在)时才执行:
<plugin> <groupId>org.jboss.as.plugins</groupId> <artifactId>jboss-as-maven-plugin</artifactId> <!-- 其他基础配置不变 --> <executions> <execution> <id>undeploy</id> <phase>prepare-package</phase> <goals> <goal>undeploy</goal> </goals> <configuration> <!-- 返回码不为0时跳过undeploy --> <skip>${maven.exec.exit.code != 0}</skip> </configuration> </execution> <!-- deploy执行配置保持不变 --> </executions> </plugin>
方案3:直接用force部署跳过单独undeploy
其实jboss-as:deploy目标有个force参数,开启后会自动先卸载已存在的同名部署,再重新部署;如果目标部署不存在,就直接执行部署操作。这样你可以完全去掉单独的undeploy步骤,一步到位:
<plugin> <groupId>org.jboss.as.plugins</groupId> <artifactId>jboss-as-maven-plugin</artifactId> <version>7.9.Final</version> <configuration> <hostname>your-jboss-host</hostname> <port>9999</port> <force>true</force> <!-- 关键配置 --> <name>your-app.war</name> </configuration> <executions> <execution> <id>deploy</id> <phase>install</phase> <goals> <goal>deploy</goal> </goals> </execution> </executions> </plugin>
我个人更推荐方案3,既简化了配置,又避免了不必要的步骤,用起来特别省心。
内容的提问来源于stack exchange,提问作者Dharan
相关产品推荐
相关产品推荐

