1

I have configured a Gradle plugin in settings.gradle file as follows,

buildscript {
  dependencies {
    classpath "org.test.group:gradleplugins:${pluginVersion}"
    ...
  }
}

apply plugin: 'org.test.group:gradleplugins'
....

and I am trying to provide artifacts repository using init.gradle as follows,

initscript {
    repositories {
        maven { url "https://test.repo/gradleplugins" }
        ...
    }
}

also, I have provided init.gradle file to the build task using,

.... -i -I ./init.gradle'

but the build still gets a dependency resolution error as follows,

Cannot resolve external dependency org.test.group:gradleplugins:1.0.0-SNAPSHOT because no repositories are defined.

Janitha Madushan
  • 1,453
  • 3
  • 28
  • 40

2 Answers2

2

It could be done either way by writing Gradle plugin in the init.gradle file as following,

apply plugin: EnterpriseRepositoryPlugin

class EnterpriseRepositoryPlugin implements Plugin<Gradle> {

    void apply(Gradle gradle) {
        gradle.settingsEvaluated { settings ->
            settings.pluginManagement {
                repositories {
                    maven { url "https://repo.org/gradleplugins" }
                    maven { url "https://repo.org/maven" }
                }    
            }
        }
    }
}

according to Gradle documentation, https://docs.gradle.org/current/userguide/init_scripts.html

Janitha Madushan
  • 1,453
  • 3
  • 28
  • 40
  • ..Can you please suggest on [How to determine the deployment url, uploadArchive task will pick at runtime](https://stackoverflow.com/questions/62838908/need-to-capture-activity-of-the-uploadarchive-and-publish-gradle-tasks?bcsi_scan_edc08cf19929fe1d=ZIDqjWalvKjvXYnxgbeS6WJznPgPAAAAFDlriA==) For uploadArchive task, based on artifact version or release type . It can pick SnapshotURL or ReleaseURL. how to get the deployment URL at runtime. I want to read that url in init.gradle ? Please suggest. – user3142041 Jul 22 '20 at 09:47
1

The init.gradle has another purpose (and that file is being automatically detected).

void settingsEvaluated​(Settings settings) might be to only chance to manipulate these settings (but the question does not provide the least valid reason to do so, with only one repository). It rather seems that you're unnecessarily over-complicating things, where there otherwise would be no problem. And this doesn't belong into the settings.gradle either (which also has another purpose). Just add the plugin repositories into the buildscript block of the root project's build.gradle, where they belong. The userguide shows how it should look alike, when defining the plugin repositories statically.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • 1
    Our company needs to keep these kinds of common configurations from the modules away. Because it could be easy to manage change, change artifacts repositories of all the modules from a single point in the future. – Janitha Madushan Dec 27 '19 at 08:05
  • This would be the current way to do it: https://stackoverflow.com/a/71281107/549372 – Martin Zeitler May 25 '22 at 12:51