Hi folks, I'm trying to modify a binary plugin wit...
# plugin-development
k
Hi folks, I'm trying to modify a binary plugin with a fairly simple goal: copy a resource from the plugin jar into the resources of the target project. I want to use the under-utilized TextResource type instead of doing classloader introspection. The code would look something like this:
Copy code
URL 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.
v
You really should not use
protectionDomain.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/28075
k
Thanks for the examples Björn!
👌 1