0

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>

4

1 に答える 1

1

私も少し前まで同じように悩んでいました。maven で適切に実行できたことを覚えていますが、この構成はありません。なんで?CodeNarc は、一部のルールを実行するためにソースをコンパイルする必要があるためです。しかし、codenarc maven プラグインはクラスパスを渡さず、コンパイルに失敗していました。

そこで、ant タスクを使用して CodeNarc をテスト ソースとして実行する別のアプローチを採用しました。次のようになります。

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)
                    }
                }
    }
}

そのためにスポックを使用する必要はありませんSpecification。どのテストランナーでも構いません。Maven 側では、スコープtestで構成された CodeNarc 依存関係を作成するだけで十分です。

于 2014-09-25T14:12:05.560 に答える