配置了pmd,checkstyle和findbugs的自定义gradle插件

时间:2015-12-15 20:06:33

标签: android android-gradle gradle-plugin

感谢您的建议,在文档中说:

TextResource ruleSetConfig

Note: This property is incubating and may change in a future version of Gradle.
The custom rule set to be used (if any). Replaces ruleSetFiles, except that it does not currently support multiple rule sets. See the official documentation for how to author a rule set. Example: ruleSetConfig = resources.text.fromFile(resources.file("config/pmd/myRuleSets.xml"))

但如果我尝试

setRuleSetConfig(project.resources.text.fromFile(project.resources.file("pmd.xml")))

它说:

Caused by: groovy.lang.MissingMethodException: No signature of method: org.gradle.api.internal.resources.DefaultResourceHandler.file() is applicable for argument types: (java.lang.String) values: [pmd.xml]
Possible solutions: find(), find(groovy.lang.Closure), use([Ljava.lang.Object;), is(java.lang.Object), with(groovy.lang.Closure), wait()
    at com.barista_v.android_quality.MyPmdTask.<init>(MyPmdTask.groovy:20)
    at com.barista_v.android_quality.MyPmdTask_Decorated.<init>(Unknown Source)
    at org.gradle.api.internal.DependencyInjectingInstantiator.newInstance(DependencyInjectingInstantiator.java:48)
    at org.gradle.api.internal.ClassGeneratorBackedInstantiator.newInstance(ClassGeneratorBackedInstantiator.java:36)
    at org.gradle.api.internal.project.taskfactory.TaskFactory$1.call(TaskFactory.java:121)
    ... 82 more

我也是这样尝试::微笑:

class MyPmdTask extends Pmd {

  MyPmdTask() {
    project.extensions.pmd.with {
      reportsDir = project.file("$project.buildDir/outputs/")
    }

    source = project.fileTree('src/main/java').toList()
    ruleSets = []
    reports {
      xml.enabled = false
      html.enabled = true
    }

    setIgnoreFailures(true)
    setRuleSetConfig(ResourceUtils.readTextResource(project, getClass().getClassLoader(), "pmd.xml"))
  }
}


class ResourceUtils {
  static TextResource readTextResource(Project project, ClassLoader classLoader, String fileName) {
    println "reading config file $fileName"
    def inputStream = classLoader.getResourceAsStream(fileName)
    project.resources.text.fromString inputStream.text
  }
}

它设置了配置文件,但它不使用我的规则,不要崩溃但不要使用我的规则。 我尝试过设置

    project.extensions.pmd.with {
  // HERE
    }

当我尝试getRuleSetConfig()时,它会返回null,但setRuleSetConfig()似乎已设置但未使用。

我不想为像https://github.com/MovingBlocks/TeraConfig这样的配置创建另一个仓库。

相关:https://discuss.gradle.org/t/how-can-i-read-setup-resources-from-plugin-jar/13274/4

我该怎么办?

1 个答案:

答案 0 :(得分:1)

我能够使用包装在插件static Vector<ClientHandler> ar = new Vector<>(); static void Broadcast() throws IOException { for (int i = 0; i < ar.size(); i++) { try { DataOutputStream out = new DataOutputStream(ar.get(i).client.getOutputStream()); out.writeUTF(String.valueOf(ar.size())); } catch (Exception ex) { ex.printStackTrace(); } } } 目录中的自定义规则集来创建一个自定义插件,以添加内置类型的PMD任务;我正在使用Kotlin,但您可能会明白:

        public void actionPerformed(ActionEvent e) 
        {
            try
            {
                out.writeUTF(String.valueOf(clientSocket.getLocalPort()));
                String resp = in.readUTF();

                System.out.println(resp); // this only outputs for one client, is it not supposed to output it for every client on the thread, since this piece of code is shared with every other thread, and every other thread is waiting?
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }

        }

这仍在进行中(例如,我不确定在我的src/main/resources中直接解决android扩展是否有影响),但它似乎按预期使用了我的规则集!