Loading default configurations from binary gradle plugin

Hello,
I’d like to create a gradle binary plugin where I’d like to provide some default configs (default files) which are bundled with this plugin.

Let’s say I want to provide a file called license-groups.json which will be stored inside gradle plugin project src/main/resources/my/gradle/project/license-groups.json. Is it then ok-ish to load it like this?

licenseGroups = getProject().getObjects().property(Path.class);
licenseGroups.convention(Path.of(this.getClass().getClassLoader().getResource("my/gradle/project/license-groups.json").getPath()));

Or is there a better approach?

Tnanks

Well, it will not work.
It might work as long as you are testing.
But as soon as you make a JAR out of your plugin, your Resource is not a file on the filesystem on which you can simply use getPath().
If you need it as file, you need to either copy it somewhere to the disk and use that, or use something like resources.text.fromUri(...).asFile(), depending on the concrete situation.

thanks for quick answer,
but even if I use resources.text.fromUri(...).asFile() am I able to store that file as a default config in gradle plugin (jar)? I’ve never created plugin before, so I’m trying to verify what would be the best way to do it.
I’m able to read stored files in standard java apps, but don’t understand how it works here.

Yes, it just works the same.
The fromUri for example you give the result of getResource(...) you used.
It basically does the alternative for you, copying the resource to some file on disk and giving you the reference.

I’ve tried this:

My task class

private Property<File> licenseGroupsFile;

public CheckLicensesTask() {

        licenseGroups = getProject().getObjects().property(LicenseGroups.class);

        licenseGroupsFile = getProject().getObjects().property(File.class);
        licenseGroupsFile.convention(getProject().getResources()
                .getText()
                .fromUri("META-INF/com/mycomp/gradle/sbom-license-plugin/license-groups.json")
                .asFile());

        if (!licenseGroups.isPresent()) {
            try {
                final List<LicenseGroup> licenses = objectMapper.readValue(licenseGroupsFile.get(),
                        new TypeReference<ArrayList<LicenseGroup>>() {
                        });
                licenseGroups.set(LicenseGroups.of(licenses));
            } catch (Exception e) {
                throw new IllegalStateException("Failed to read license groups from file " +
                        licenseGroupsFile.get(), e);
            }
        }
}

however when I execute the task the file is not found in temp directory which is created by execution. What am I doing wrong?

Note: that license-groups.json file is stored in plugin’s META-INF directory so it’s also distributed with it as a jar.

That’s not the URL of the file.
Use getResource on your plugin class to get the URL for the file.

org.gradle.api.Plugin doesn’t have getResource method

ut 20. 5. 2025 o 13:41 Björn Kautler via Gradle Forums <notifications@gradle.discoursemail.com> napísal(a):

Managed to have it working like this (tested also on project)

      try (InputStream inputStream = getClass().getResourceAsStream(DEFAULT_LICENSE_GROUPS)) {
            if (inputStream == null) {
                throw new IllegalStateException("Failed to read default license groups from classpath");
            }
            licenseGroupsFileValue = File.createTempFile("license-groups", ".json");
            FileUtils.copyInputStreamToFile(inputStream, licenseGroupsFileValue);
            licenseGroupsFile.convention(licenseGroupsFileValue);
        }

path in DEFAULT_LICENSE_GROUPS has to start wit /

org.gradle.api.Plugin doesn’t have getResource method

Of course not.
I did not say get it from your plugin, I said get it from your plugin class like you do now, just with manually writing the content to an arbitrary temp file. :wink:
Something like

getProject().getResources()
    .getText()
    .fromUri(getClass().getResource(DEFAULT_LICENSE_GROUPS))
    .asFile()

path in DEFAULT_LICENSE_GROUPS has to start wit /

Just like always when you get a resource from classpath in the JVM.
If you get it from a Class and it is in the same package as the class, just use the name, as it is relative to the class.
If you get it from a Class and it is in a different package, use an absolute path starting with /.
If you get it from a ClassLoader, use an absolute path not starting with /.
Nothing Gralde-specific here.