如果java异常,如何使Jenkins作业失败?

时间:2013-11-10 17:18:11

标签: java maven jenkins hudson

我有詹金斯的工作。我按如下方式运行maven java-executor插件:

编译exec:java

这是pom.xml

<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>
    <groupId>com.proj.utills</groupId>
    <artifactId>db-upgrade</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>com.proj.db.RunMe</mainClass>
                    <arguments>
                        <argument>
                            ${TARGET_ENV}
                        </argument>
                    </arguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>3.0</version>
        </dependency>
    </dependencies>
</project>

当我在Java代码中遇到异常时,我希望Jenkins作业失败。现在即使在例外的情况下它也是成功的。我该怎么做? 感谢

4 个答案:

答案 0 :(得分:1)

您可以使用log parser plugin

编辑:摘自插件的文档

Job Configuration
Go to menu: Jenkins -> job name -> Configure
Go to section : Post-build Actions
Check the "Console output (build log) parsing" checkbox.
"Mark build Unstable on Warning" option: check to have parsed warnings mark the build 'unstable'.
"Mark build Failed on Error" option : check to have parsed errors mark the build 'failed'.
"Select Parsing Rules" : select the set of rules to use for parsing the logs of this job's builds
(Notice that this list is derived from the global configuration )

答案 1 :(得分:1)

我使用Log Parser Plugin这样的情况。只需配置您要查找的内容。在你的情况下,它可能就像寻找'异常'一样简单。我曾经解析shell脚本的输出,找到sql错误(但仍然忽略了一些我不关心的事情)。

答案 2 :(得分:0)

您可以尝试Jenkins Text Finder plugin,在满足正则表达式设置时将BUILD状态从成功降级为UNSTABLE / FAIL。更多细节请参考上面的链接。

答案 3 :(得分:0)

我遇到了完全相同的问题,并且不喜欢这种解析日志解决方案。 对我来说-B(启用批处理模式)做得很好

内部版本:   -mvn -B部署

相关问题