Hi! Using gradle 6.8.3 in a multi project build, I am trying to resolve a plugin using the plugins c...
j
Hi! Using gradle 6.8.3 in a multi project build, I am trying to resolve a plugin using the plugins closure rather than the buildscript closure but cannot get it to resolve to the correct artifact. Previously this worked:
Copy code
# root/build.gradle:
buildscript { 
  repositories {
    maven {
      url "$url"
      credentials { ... }
    }
  }
  dependencies {
    classpath "my.org.foo:my-plugin:$version"
  }
}
And then I could simply:
apply plugin: 'my.org.foo'
Now I try to replace this by what appears to be the "modern" approach:
Copy code
# root/settings.gradle
pluginManagement {
    repositories {
        maven {
            url "$url"
            credentials { ... }
        }
    }
}

# root/subModule/build.gradle
plugins {
  id 'my.org.foo' version '...'
}
Now, when doing this gradle tries to resolve this to
'my.org.foo:my.org.foo.gradle.plugin:$version'
. How do I make gradle understand how to locate the proper artifact? Thanks!
t
you could use
pluginManagment
in
settings.gradle.kts
, for example for android plugin:
Copy code
pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
    }
    resolutionStrategy {
        eachPlugin {
            when (requested.id.id) {
                "com.android.application" ->
                    useModule("com.android.tools.build:gradle:${requested.version}")
            }
        }
    }
}
j
Thanks. That actually worked! 🙏 It would be fancier if the plugins closure itself allowed you to specify the artifact and not just id. But I'm sure there are good reasons for this. 🙂
t
you could also add all deps in
buildSrc/build.gradle.kts
old way 🤷‍♂️
v
For the ID to magically work you need to have the marker artifact in your repository. If you use the Gradle Plugin Development Plugin when publishing your plugin it will automatically also publish this marker artifact. If the marker artifact is missing, then you need to compensate that in the settings script. Specifying the mapping in the
plugins
block would be unnecessary clutter as you then need to specify it on every usage instead of one time centrally. The marker artifacts are the default way how a plugin id is translated to the actual dependency. Imagine you have an artifact
my-plugins.jar
that contains the plugins
my-a
,
my-b
and
my-c
. Then you will have three "marker artifacts" that follow the naming convention you have seen and then depend on the
my-plugins.jar
.
j
Thanks a lot! Works like a charm. Next step: figure out how to make com.jfrog.artifactory play nice with java-gradle-plugin. 🙂
Ahh. But of course. We want to publish xPluginMarkerMaven. The plugin artifact itself was named something like xPlugin so the artifact was actually name xPluginPluginMarkerMaven. 🍻 Thanks again for your help! Much appreciated.