Gradle 8.9 how to convert buildScript to new style

Hi there, first time posting, long time reading and using gradle… :slight_smile:

I have the following in my settings.gradle file. I would like to convert this to use the new style of pluginmanagement { … } with includeBuild. Is there a better way of writing this so that in my build.gradle I can add my gradle plugin into the plugins section?

buildscript {
    repositories {
        mavenCentral()
        maven {
            url 'https://<artifactory_url>/artifactory/<artifactory_repo>'
            credentials {
                username = System.getenv("user_artifactory_repo") ?: "${user_artifactory_repo}"
                password = System.getenv("pass_artifactory_repo") ?: "${pass_artifactory_repo}"
            }
        }
    }
    dependencies { classpath '<maven-group>:<artifact-id>:latest.release' }
}

Just move your repository to the pluginManagement { respositories { ... } } section?

I also highly recommend to switch to Kotlin DSL. By now it is the default DSL, you immediately get type-safe build scripts, actually helpful error messages if you mess up the syntax, and amazingly better IDE support if you use a good IDE like IntelliJ IDEA or Android Studio.

Thank you for the response. I should have mentioned that the artifact is a custom gradle plugin. Since it is not signed, then this is not published into mavenCentral(). So I think I’m stuck using the buildscript to define the custom repository and dependency → classpath before the plugins { ... } block is called. Would this be accurate or is there some new trick I’m not aware of?

Thank you for the response. I should have mentioned that the artifact is a custom gradle plugin.

Yes, I got that right from the start, doesn’t change my response.

Since it is not signed, then this is not published into mavenCentral().

Well, you can just do so and publish to Maven Central if you want the plugin publicly available.
Or even better publish it to Gradle Plugin Portal, then it is also integrated in the Plugin Portal web UI.
If it should not be publicly available, just keep publishing it to your internal repository.
But still doesn’t change anything with my initial response.

So I think I’m stuck using the buildscript to define the custom repository and dependency → classpath before the plugins { … } block is called. Would this be accurate or is there some new trick I’m not aware of?

No, it is not accurate and no, no new trick necessary.
As I said, just define your repository as plugin repository.

Got it!

So when I did that, how do you define the dependencies so that in build.gradle I could define:

plugins {
   id 'my-plugin'
}

I kept getting an error saying it couldn’t find the plugin in the repository.

Did you publish the Plugin Marker Artifacts?
If not, you should, if you did, you probably miss to configure the version somewhere.

Besides that, if your plugin ID really looks like that, you should change it anyway, plugin IDs without namespace (without .) should not be used but be reserved for built-in plugins to not risk name clashes.

If you do not publish the marker artifact and don’t want to (you really should though) you can use pluginManagement { resolutionStrategy { ... } } to do the mapping from ID to code artifact manually for which the marker artifact would have been used otherwise.