kyle
11/25/2024, 8:37 PMURL pluginJar = MyPlugin.class.getProtectionDomain().getCodeSource().getLocation();
TextResource bundledTemplate = project.getResources().getText().fromArchiveEntry(pluginJar, "path/to/resource");
var processResourcesTask = project.getTasks().named(JavaPlugin.PROCESS_RESOURCES_TASK_NAME, ProcessResources.class, processResources -> {
processResources.from(bundledTemplate, copy -> {
copy.into("target/project/path");
...
});
});
The hard part is testing. Despite setting org.gradle.java.compile-classpath-packaging=true in the project-under-test's properties, the plugin jar's codeLocation is always the classes dir, not the jar, causing TRF#fromArchiveEntry to fail while running tests.
Are there better ways to do this? I don't control the plugin, so while separating the resource to another JAR would be nice and clean I probably can't do that.Vampire
11/26/2024, 4:22 AMprotectionDomain.codeSource.location for such things in any JVM program, that is always quite a hacky thing that also might or might not work. Better use the normal Class#getResourceAsStrem or ClassLoader#getResourceAsStream.
You could still use `TextResource`: https://github.com/gradle/gradle/issues/28075Vampire
11/26/2024, 4:23 AMkyle
11/26/2024, 1:31 PM