Internally expose required import statements

(Betty Sanchez) #1


Is there a way to internally add/expose import statements when a Plugin is applied instead of requesting the user to explicitly writing them in their build script? At least for specific packages that should be exposed by default for the plugin.

For example, I am developing a plugin in Java that provides several tasks. An example task might be To be able to invoke my tasks after applying my plugin in the build.gradle script (plugins { id 'myplugin' }) I need to call import,project.task.* before invoking any task by their name exclusively (e.g. task(myTask)). Otherwise I need to provide the fully qualified name for the task (e.g. task(org.compay.project.task.myTask)).

Is it possible to expose the required or main imports from the plugin application? Is this recommended?


(Chris Doré) #2

Some plugins do this by creating a property on the project whose value is the task class. For example,

(Betty Sanchez) #3

Hi Chris,

Thanks for your reply.

I tried the following in the apply method of the Plugin

project.getProperties().put("MyTask", MyTask.class);

but it seems that the put method is not happy taking classes as property values. It didn’t work either passing the fully qualified class as a string value. Any ideas?

(Chris Doré) #4

Try this:

project.getExtensions().getExtraProperties().set( "MyTask", MyTask.class )

(Betty Sanchez) #5

Thank you! It worked :slight_smile:

(Paul Merlin) #6

Note that this will only work in Groovy Gradle scripts. It won’t in Kotlin Gradle scripts.

Allowing plugins to contribute to the script default imports would be more correct and interoperable. It sounds like an good feature request. @beatrizsanchez, could you please open an issue ?

(Betty Sanchez) #7