为Gradle CodeNarc插件配置额外的文件扩展名。

huangapple go评论53阅读模式
英文:

Configure additional filename extensions for Gradle CodeNarc plugin

问题

我目前正在尝试在我的项目中使CodeNarc插件适用于Gradle 8.0.2。

由于某些原因,Groovy文件具有不同的扩展名。目前,该插件似乎仅适用于*.groovy文件。

假设我有一个像这样的build.gradle文件:

plugins {
    id 'groovy'
    id 'java'
    id 'codenarc'
}

repository {
    mavenCentral()
}

dependencies {
    implementation 'org.apache.groovy:groovy-all:4.0.10'
}

sourceSets {
    main {
        groovy {
            srcDirs = ['directory1', 'directory2']
        }
    }
}

现在我有以下文件:

  • directory1/utils.groovy
  • directory1/something.groovy
  • directory2/another.myextension

运行./gradlew codenarcMain --info不会打印:

No matching files found for FileSet with basedir [/home/path/to/project/directory2]

添加以下内容:

codenarcMain {
    include('**/*.myextension', '**/*.groovy')
}

似乎没有改变任何东西。

从Gradle中注册CodeNarc插件的自定义扩展名的正确方法是什么?

英文:

I am currently trying to get the CodeNarc plugin for Gradle 8.0.2 to work in my project.

Due to some reasons, the Groovy files have different extensions. At the moment, the plugin only seems to run for the *.groovy files.

Let's say I have a build.gradle file like this:

plugins {
    id 'groovy'
    id 'java'
    id 'codenarc'
}

repository {
    mavenCentral()
}

dependencies {
    implementation 'org.apache.groovy:groovy-all:4.0.10'
}

sourceSets {
    main {
        groovy {
            srcDirs = ['directory1', 'directory2']
        }
    }
}

Now I have the following files:

  • directory1/utils.groovy
  • directory1/something.groovy
  • directory2/another.myextension

Running ./gradlew codenarcMain --info will not print:

No matching files found for FileSet with basedir [/home/path/to/project/directory2]

Adding

codenarcMain {
    include('**/*.myextension', '**/*.groovy')
}

does not seem to change anything.

What is the correct approach to register custom extensions for the CodeNarc plugin from within Gradle?

答案1

得分: 1

我无法使Gradle内置的codenarc插件识别除.groovy以外的扩展名,即使在配置Groovy编译器以识别新扩展名后也无法。然而,根据示例中的内容,我通过创建基于ant codenarc任务的自定义codenarc Gradle任务成功实现了这一点。该任务配置为处理特定的源文件夹(示例中为src/test/groovy)和具有特定扩展名的文件(示例中为.groovy.dummy)。

configurations {
    codenarc
}

dependencies {
    String groovyVersion = '3.0.18';

    codenarc group: 'org.codehaus.groovy', name: 'groovy-templates', version: groovyVersion
    codenarc group: 'org.codehaus.groovy', name: 'groovy-xml', version: groovyVersion
    codenarc group: 'org.slf4j', name: 'slf4j-api', version: '1.7.35'
    codenarc group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.12'
    codenarc group: 'org.codenarc', name: 'CodeNarc', version: '3.2.0'
}

tasks.register('codenarcTest') {
    String reportsFile = 'build/reports/codenarc.html';
    inputs.files(fileTree('src/test/groovy'));
    outputs.file("$projectDir/$reportsFile");

    doLast {
        ant.taskdef(name:'codenarc', 
                    classname:'org.codenarc.ant.CodeNarcTask', 
                    classpath: configurations.codenarc.asPath)
        ant.codenarc(
           ruleSetFiles: 'rulesets/imports.xml,rulesets/unused.xml',
           maxPriority1Violations: 0, maxPriority2Violations: 0,
           maxPriority3Violations: 0,
        ) {
           fileset(dir:'src/test/groovy') {
               include(name: '**/*.groovy')
               include(name: '**/*.dummy')
           }

           report(type:'html') {
               option(name: 'outputFile', value: reportsFile)
           }
        }
    }
}

希望这可以帮助你解决问题。

英文:

I wasn't able to get gradle's in-built codenarc plugin recognize an extension other than .groovy even after configuring the groovy compiler to recognize the new extension. However, following an example in the codenarc website, I was able to achieve this by creating a custom codenarc gradle task based on the ant codenarc task. This task is configured to process specific source folder(s)(src/test/groovy in the example) and files with specific extensions(.groovy and .dummy in the example).

configurations {
    codenarc
}

dependencies {
    String groovyVersion = '3.0.18'

    codenarc group: 'org.codehaus.groovy', name: 'groovy-templates', version: groovyVersion
	codenarc group: 'org.codehaus.groovy', name: 'groovy-xml', version: groovyVersion
	codenarc group: 'org.slf4j', name: 'slf4j-api', version: '1.7.35'
	codenarc group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.12'
    codenarc group: 'org.codenarc', name: 'CodeNarc', version: '3.2.0'
}

tasks.register('codenarcTest') {
    String reportsFile = 'build/reports/codenarc.html'
    inputs.files(fileTree('src/test/groovy'))
    outputs.file("$projectDir/$reportsFile")

    doLast {
        ant.taskdef(name:'codenarc', 
        			classname:'org.codenarc.ant.CodeNarcTask', 
        			classpath: configurations.codenarc.asPath)
        ant.codenarc(
           ruleSetFiles: 'rulesets/imports.xml,rulesets/unused.xml',
           maxPriority1Violations: 0, maxPriority2Violations: 0,
           maxPriority3Violations: 0,
        ) {
           fileset(dir:'src/test/groovy') {
               include(name: '**/*.groovy')
               include(name: '**/*.dummy')
           }

           report(type:'html') {
               option(name: 'outputFile', value: reportsFile)
           }
        }
    }
}

huangapple
  • 本文由 发表于 2023年3月15日 20:53:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定