English 中文(简体)
使用 Gradle 插件中的资源文件
原标题:Use resource file from Gradle plugin
I m currently developing a small shared plugin for few projects and the cleanest solution would be if I could read and process a resource file provided by the plugin. However, Java/Kotlin style this.javaClass.getResouce(...) always returns null |- src/main/kotlin/myplugin.gradle.kts |- src/main/resources/file.txt |- build.gradle.kts |- settings.gradle.kts myplugin.gradle.kts tasks { val mytask by registering(DefaultTask::class) mytask { doLast { println(this.javaClass.getResource("file.txt")) // Always returns `null` } } } Is there a clean simple way to access/read a resource file provided by the plugin it self? PS: The this.javaClass is org.gradle.api.DefaultTask_Decorated which does explain why the file is not found. However, it does not help me with reading the actual resource file.
问题回答
You are writing a plugin in a script file and intending to use it in other projects? I think that is unusual and I am not sure there is a straightforward way to publish such a plugin, let alone a defined way to access resource files. Instead, write your shared plugin in a regular class file inside its own Gradle project, which Gradle call a binary plugin. With this approach, your plugin is defined in a class that implements the Plugin interface. Then, once you have a class inside a regular compilation you are writing regular code on the JVM and you can access resources included on the classpath in the normal way. Also, if you are using Kotlin, write this class inside a project with a build.gradle.kts which applies the kotlin-dsl plugin and you will have access to many of the Kotlin features you expect in a build script. This plugin also applies the Java Gradle Plugin, giving you the features you need to easily package your plugin.




相关问题
jQuery - ScrollTo and Serial Scroll not working together

I have tested the scrollTo() plugin, but I need a way to stop the scrolling animation, so I am looking at serialScroll(). Here is what I used with scrollTo: $( #scroller ).scrollTo( 1000px , 3000); ...

jQuery deconstructors for plugins?

I m using the Galleria plugin inside an Accordion plugin to display images in a super cool way. A problem occurs, however, when I open up a new tab (in the accordion) then come back to the gallery tab....

XPCOM Security issues

I m developing a Firefox plugin using XPCOM, I ve not yet read all the docs, but as far as I can see, A plugin is simply a DLL that provides services via a XPCOM interface and interacts with the ...

Ruby on Rails plugin development process

I m considering developing aspects of a website as Rails plugins for reuse. My question is about the development process. Since each of these plugins will provide a "slice" of functionality, should ...

help mongrel wont start

I have decided to switch from Restful authentication to authlogic.. so what I did was delete every file and folder that got installed when I downloaded the plugin.. now when I try to "ruby script/...

Basic MEF workflow/usage

I m looking to a framework which will allow me to have a simple plugin system in my .NET application. It seems MEF is the framework which Microsoft is endorsing, and will become part of .NET 4 (it ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

java plugin cache and dynamic IP host

I m trying to use Amazon S3 and Amazon Cloudfront CDN to deliver the jar files of my applet application. I m seeing several cache misses of my jars by the java plugin. This is a show-stopper for me, ...

热门标签