繁体   English   中英

如何使CodeNarc强制Maven构建失败

[英]How to make CodeNarc force maven build to fail

我正在尝试将CodeNarc集成到基于Maven的项目中,但一直遇到问题。 我想使用自定义规则集,并且当违反规则时,我希望我的Maven构建失败。

运行以下命令时,如何配置codenarc以便违反规则会导致失败?

mvn clean install

另外,在POM中配置CodeNarc的文档没有解释如何引用自定义规则集所在的位置。 有关如何设置的任何建议? 谢谢!

当我使用以下配置运行mvn clean install时(根据我的规则集,我有一个带有明显违反规定的groovy文件)

我的构建成功。 :(

我尝试引用自己的规则集,但未产生任何违规情况。 我删除了POM中的rulesetfiles属性,它开始产生违规​​行为。 (但是我不能选择我自己的)

有谁知道如何使其实际读取自定义规则集文件? 我尝试了xml和groovy。

这是我的POM中的规则集和插件配置:

<ruleset xmlns="http://codenarc.org/ruleset/1.0"; 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
    xsi:schemaLocation="http://codenarc.org/ruleset/1.0 http://codenarc.org/ruleset-schema.xsd";
    xsi:noNamespaceSchemaLocation="http://codenarc.org/ruleset-schema.xsd">;

    <description>Dummy rule set</description>

    <rule class='org.codenarc.rule.formatting.SpaceAfterIf'>
        <property name='priority' value='1'/>
    </rule>

    <rule class='org.codenarc.rule.basic.EmptyIfStatement'>
        <property name='priority' value='1'/>
    </rule>
</ruleset>

我在POM中引用了此规则集,如下所示:

 <groupId>org.codehaus.mojo</groupId> <artifactId>codenarc-maven-plugin</artifactId> <version>0.18-1</version> <configuration> <sourceDirectory>${basedir}/src/test/groovy</sourceDirectory> <maxPriority1Violations>0</maxPriority1Violations> <maxPriority2Violations>0</maxPriority2Violations> <maxPriority3Violations>0</maxPriority3Violations> <rulesetfiles>${basedir}/rulesets/ruleset.xml</rulesetfiles> <xmlOutputDirectory>${basedir}/</xmlOutputDirectory> </configuration> <executions> <execution> <id>execution1</id> <phase>install</phase> <goals> <goal>codenarc</goal> </goals> </execution> </executions> 

不久前,我还在努力。 我记得可以用maven正常运行,但是我没有此配置。 为什么? 因为CodeNarc需要编译您的源代码,以用于执行某些规则。 但是Codenarc Maven插件未通过类路径,并且编译失败。

因此,我采用了另一种方法,该方法将CodeNarc作为带有ant任务的测试源运行。 看起来像:

import spock.lang.Specification

class GroovyCodeNarcStaticAnalysisRunner extends Specification {

    private static final GROOVY_FILES = '**/*.groovy'
    private static final ANALYSIS_SCOPE = 'src/main/groovy'
    private static final RULESET_LOCATION = 'file:tools/static-analysis/codenarc.xml'
    private static final HTML_REPORT_FILE = 'target/codenarc-result.html'
    private static final XML_REPORT_FILE = 'target/codenarc-result.xml'

    def 'Groovy code should meet coding standards'() {
        given:
        def ant = new AntBuilder()
        ant.taskdef(name: 'codenarc', classname: 'org.codenarc.ant.CodeNarcTask')

        expect:
        ant.codenarc(
                ruleSetFiles: RULESET_LOCATION,
                maxPriority1Violations: 0,
                maxPriority2Violations: 0,
                maxPriority3Violations: 0)
                {
                    fileset(dir: ANALYSIS_SCOPE) {
                        include(name: GROOVY_FILES)
                    }
                    report(type: 'text') {
                        option(name: 'writeToStandardOut', value: true)
                    }
                    report(type: 'xml') {
                        option(name: 'outputFile', value: XML_REPORT_FILE)
                    }
                    report(type: 'html') {
                        option(name: 'outputFile', value: HTML_REPORT_FILE)
                    }
                }
    }
}

您不需要为此使用Spock的Specification 任何测试跑步者都可以。 在行家方面,足以使CodeNarc依赖项配置为scope test

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM