Is there any way to import a plugin without applying it

Actually I had this question for a long time. Let me explain.

For example, I have a multiple sub-projects project. And I like to configure every sub-project in the root project’s build config, instead of copy the configuration every time, which looks pretty reasonable. Now, I want to configure sonar plugin, only for sub-projects, and configure it. But when I use apply false after the id statement, here comes the problem. The sonarqube block will throws exception, the gradle thinks there are no such function.

So I can not diable applying for plugin, if I want to configure it. I am kind of understanding this. When gradle run configuration under root project, it will only using the plugin that applied to root. Even I used apply(plugin="sonar"), it will not found the plugin, since the configuration is already running.

And I do know, I can use task<T> to configure the task that I want, without accutally applying it. But there are some plugins, which’s extension not fully equals to their task’s paramter. Because I have already write such one for our group. :rofl:

You cannot only configure tasks by name / type also extensions.

But you indeed should not cross-configure projects like you describe. This is discouraged bad practice that works against some of the optimizations and more sophisticated features of Gradle.

To DRY and centralize the build logic, better consider using convention plugins, for example in buildSrc or an included build like gradle/build-logic, for example implemented as precompiled script plugin.