【问题标题】:How to make CodeNarc force maven build to fail如何使 CodeNarc 强制 maven 构建失败
【发布时间】:2014-09-22 05:09:10
【问题描述】:

我正在尝试将 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 groovy codenarc


    【解决方案1】:

    前段时间我也在苦苦挣扎。我记得可以使用 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。任何测试运行者都可以。在 maven 方面,使用范围 test 配置 CodeNarc 依赖项就足够了。

    【讨论】:

    • 所以不可能让codenarc使用自定义规则集作为maven插件运行?
    • 据我记得这对我来说是可能的,但是一些增强规则失败了,因为 CodeNarc maven 插件没有传递类路径 jar。不过到现在为止可能会修复。其余规则运行良好。
    • 您是否像我一样引用您的自定义ruelset fie?因为据我所知,它被完全忽略了。
    • 不幸的是我不记得了。那是很久以前的事了。我能发现的是,您在安装时连接 CodeNarc 很晚。它应该发生在早期构建阶段。我不明白为什么它不起作用。也许自定义规则集路径或文件本身无效?
    • 我想我只是把规则的名称弄错了,它现在可以工作了。
    猜你喜欢
    • 2013-10-04
    • 1970-01-01
    • 2011-01-22
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多