nemo
nemo

Reputation: 1584

Gradle: Use BuildScript RepositoryHandler as Project Repository Handler

I'm implementing a custom plugin and inside this plugin I am providing a default repository configuration like so:

class MyPlugin implements Plugin<Project> {

  ...
  ...
  ...

  @Override
  void apply(Project project) {

    project.repositories {
      mavenLocal()
      mavenCentral()
      jcenter()
    }

    ...
    ...
    ...
  }
}

However, in order to be able to apply my plugin inside a build script, the client code will have a add a section like this in their build script anyway:

buildscript {
  repositories {
      mavenLocal()
      maven { url "<organization maven repo url>" }
      mavenCentral()
  }

  dependencies {
    classpath 'com.organization.xxx:my-plugin:1.0.130'
  }
}

apply plugin: 'com.organization.my-plugin'

Given, that, I'd like to be able to do something like this:

class MyPlugin implements Plugin<Project> {

  ...
  ...
  ...

  @Override
  void apply(Project project) {

    project.repositories = project.buildscript.repositories

    ...
    ...
    ...
  }
}

But, when I do that, I get an error, understandably, as the project.repositories property is read-only.

So, is there a proper way I could achieve what I want?

Upvotes: 2

Views: 1290

Answers (1)

Raffaele
Raffaele

Reputation: 20885

I think the error is because you are trying to assign a value to the property project.repositories. Fortunately, you just need to add repositories, so maybe this will work (untested)

@Override
void apply(Project project) {
  project.repositories.addAll(project.buildscript.repositories)
}

By the way, I'm not sure you really want to do this. Better would be to define common repositories somewhere else and then maybe add to both buildscript and project

Upvotes: 3

Related Questions