0

I have a build.gradle file that includes a repository definition in a buildscript block, like so:

buildscript {
    repositories {
        // Several proprietary repositories are declared
    }
    dependencies {
        classpath "com.example:foo:1.0"
    }
}

apply from: "bar.gradle"

I would like to bar.gradle to import a class from the foo dependency. According to this answer, the way to do that is to duplicate the buildscript block in bar.gradle.

However, I would like to reduce code duplication because the list of repositories is rather long and might change. Is it possible to have bar.gradle reference the repositories declared within the parent build.gradle?

So far, I tried this in bar.gradle:

buildscript {
    repositories {
        project.buildscript.repositories
    }
    dependencies {
        classpath "com.example:foo:1.0"
    }
}

But unfortunately, I still get an error saying that repositories are not defined.

Thunderforge
  • 19,637
  • 18
  • 83
  • 130

1 Answers1

1

if you're not applying something within buildscript your buildscript can't reference it

try...

buildscript.gradle

project.buildscript {
    repositories  {
        someRepo()
    }   
    dependencies{
        classpath 'some:dependency:0.1.0'
    }
}

build.gradle

buildscript {
    apply from: "buildscript.gradle"
}

kassim
  • 3,880
  • 3
  • 26
  • 27