簡體   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