Referencing files included in convention plugin

Hello,

I have setup a convention plugin for my my projects. Among other things, I want the plugin to apply the PMD plugin to my projects using a common rulesets file included in the convention plugin (not the actual project). I need help with how I can do that.

In my convention plugin project I have:

├── build.gradle
└── src
    └── main
        ├── groovy
        │   └── my-java-convention.gradle
        └── resources
            └── rulesets.xml

In my-java-convention.gradle, I have:

plugins {
    id 'java'
    id "io.freefair.lombok"
    id "pmd"
}

java {
    toolchain.languageVersion.set(JavaLanguageVersion.of(8))
}

pmd {
    consoleOutput = true
    ruleSetFiles += files("rulesets.xml") // this where I want to reference the file included in the plugin
}

test {
    useJUnitPlatform()
}

I am able to apply the convention plugin to my project and it is adding the pmd tasks to my project build. However, when pmd runs it complains because it’s looking for rulesets.xml in the my project directory (because filles("rulesets.xml) looks in the current project).

How can reference the rulesets.xml file included with the plugin not with the project. I know the file is available on the classpath because I can get to the file with getClass().getResource(“rulesets.xml”) which gives a URI object, but how to pass this as a file collection?

Please let me know if you know how to accomplish this. I plan to use the same approach for using spotless plugin where I want to include the java formatting rules file in the convention plugin.

Thanks in advance!

You probably need to use something like resources.text.fromUri(...).asFile()

Thank you! This works for me.

A related question… I dug through the code and it appears that it creates a temporary file with the txt extension when the URI is not a regular file, copies the content to it and returns that file. In my case, the URI will be a jar URI, so it loads from the classpath and writes to ~/.gradle/.tmp/resource/wrappedInternalText3576852723262505814.txt (the numbers in the file name change).

It seems to create a temporary file of the referenced file every time my plugin is applied. So one for each project that uses the plugin every time the build script is evaluated. Not a big deal except that nothing seems to be cleaning up these files. My question is whether you are aware of any process that cleans up ~/.gradle/.tmp?

I’m working around this by calling deleteOnExit() for the file returned from asFile(), but I’m curious about how the gradle temp directory cleanup is handled.

Thanks again!

There are various cleanups for things.
But whehter there is one for .tmp I cannot tell you.