How to apply a plugin in settings.grade


(in.srv.ptc) #1

I read some topics about applying a plugin in settings.gradle but none of them seems to work for me.

In settings.gradle I have this four lines of code and I would like to outsource them to a gradle plugin which I can then apply in settings.gradle.

rootProject.children.each { project ->
                                            String fileBaseName = project.name.replaceAll("\p{Upper}") { "-${it.toLowerCase()}" }
    project.buildFileName = "${fileBaseName}.gradle"
}

I tried applying this plugin by the name:

Plugin with id 'plugin-to-apply' not found.

By the classname:

Could not find property 'com' on settings 'my-project'

Is there any other way to import a plugin to settings.gradle or is it a fail configuration?


(René Groeschke) #2

Hello, where did you declare the plugin?

For a plugin that is applied in the settings.gradle file you have three options:

  • declare the plugin class directly in the settings.gradle file (not that useful for production but good for testing) * declare the plugin in the buildSrc project * resolve the plugin as a third party dependency

To resolve a plugin as a thirdparty dependency you need to declare a buildscript block and declare the dependency (& repository) to resolve the plugin. Be aware that the buildscript block in your build.gradle does not work for this as the build.gradle file is evaluated AFTER the settings.gradle file. You have to declare the buildscript block in your settings.gradle file. example:

buildscript{
 repositories{
  mavenCentral();
 }
 dependencies{
  classpath "org.acme:settingsplugin:1.0"
 }
}
  apply plugin 'settingsplugin'
  // declare some projects
include 'shared'
include 'api'
include 'services:webservice'
  rootProject.name = 'settingsplugin'

We have plans and already working on reducing the boilerplate for declaring plugins but this is currently WIP.

hope that helps!

cheers, René


(in.srv.ptc) #3

Hello Rene,

thanks for your answer.

I declared my plugin directly in settings.gradle and tried to specify the plugin name like so:

apply plugin: 'plugin-to-apply'

META-INF/gradle-plugins/plugin-to-apply.properties:

implementation-class=com.plugins.MyPlugin

and got the error like id not found. See the error message in the first post.

My second try was to declare in settings.gradle the plugin by its classname like so:

apply plugin: com.plugins.MyPlugin

and got again an error message:

Could not find property 'com' on settings 'my-project'

I’m fine to go with the plugin classname in settings.gradle but can’t get it to run.

Do you have any suggestions?


(René Groeschke) #4

Can you show me how you declare the classpath to point to your plugin implementation?


(in.srv.ptc) #5

I have in my gradle distribution in the directory init.d resolveplugins.gradle file with this content:

initscript{
                                                                             repositories{
                                                                           maven{
                                                                                  url = "http://someCompany.com/nx/ct/gr/pub"
                 }
                                                                       }
                                                                               dependencies{ classpath "com.someCompany:my.plugins:@VERSION@" }
}
                                                                                                                                                                 allprojects {
                                                                           buildscript{
                                                                            repositories{
                                                                           maven{
                                                                                  url = "http://someCompany.com/nx/ct/gr/pub"
                         }
                                                                       }
                                                                               dependencies{ classpath "com.someCompany:my.plugins:@VERSION@" }
        }
                                                                               apply plugin:com.someCompany.plugins.BasePlugin
           }