# Share classpath between buildscript and project configuration

**URL:** <https://discuss.gradle.org/t/share-classpath-between-buildscript-and-project-configuration/6527>\
**Category:** Old Forum Archive\
**Created:** [December 16, 2012, 11:32pm UTC](https://discuss.gradle.org/t/share-classpath-between-buildscript-and-project-configuration/6527 "2012-12-16T23:32:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![queshaw](https://avatars.discourse-cdn.com/v4/letter/q/d6d6ee/32.png) [@queshaw](https://discuss.gradle.org/u/queshaw)\
**Post date:** [December 16, 2012, 11:32pm UTC](https://discuss.gradle.org/t/share-classpath-between-buildscript-and-project-configuration/6527/1 "2012-12-16T23:32:00Z")

</div>

My build script needs the same classpath for buildscript and the projects runtime configuration. I am probably using the wrong terminology.

I don’t want this duplication:

buildscript {

repositories { flatDir dirs: ‘lib’ }

classpath ‘a’

classpath ‘b’ }

repositories { flatDir dirs: ‘lib’ }

dependencies {

runtime ‘a’

runtime ‘b’

}

How can I share? For example, this doesn’t work:

buildscript {

repositories { flatDir dirs: ‘lib’ }

classpath ‘a’

classpath ‘b’ }

dependencies {

runtime buildscript.configurations.classpath }

And I would want to share the repositories.

It seems like I am endlessly struggling to find how to get access to a key and a value from different places in gradle.

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [December 17, 2012, 6:15am UTC](https://discuss.gradle.org/t/share-classpath-between-buildscript-and-project-configuration/6527/2 "2012-12-17T06:15:00Z")

</div>

The ‘buildscript’ block is very special. It’s more or less a “meta” build script that has to be evaluated before the rest of the build script can even be compiled. There shouldn’t be many common repositories/dependencies between the two, and I recommend to declare them twice. If you really want to share, something like this might work:

```gradle
buildscript {
    ext.sharedRepos = {
        flatDir dirs: 'lib'
    }
    ext.sharedDeps = ['a', 'b']
      repositories sharedRepos
    dependencies {
        classpath sharedDeps
    }
}
  repositories sharedRepos
dependencies {
    runtime sharedDeps
}

```

The basic idea here is that because the ‘buildscript’ block gets evaluated first, the rest of the build script can access the ‘buildscript’ block, but not the other way around.

---

<div class="post-metadata">

**Author:** ![queshaw](https://avatars.discourse-cdn.com/v4/letter/q/d6d6ee/32.png) [@queshaw](https://discuss.gradle.org/u/queshaw)\
**Post date:** [December 17, 2012, 2:41pm UTC](https://discuss.gradle.org/t/share-classpath-between-buildscript-and-project-configuration/6527/3 "2012-12-17T14:41:00Z")

</div>

Thank you for the explanation about the evaluation time.

I have groovy code in a task and import statements. If I don’t have the buildscript block, the classes in the import statements aren’t found. If I don’t have the dependencies block, the classes in the task aren’t found.

Unfortunately, using the code that you suggested, and nothing else (with valid string instead of ‘a’ and ‘b’), I get an error message saying no such property sharedRepos for class DefaultScriptHandler.

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [December 17, 2012, 4:38pm UTC](https://discuss.gradle.org/t/share-classpath-between-buildscript-and-project-configuration/6527/4 "2012-12-17T16:38:00Z")

</div>

I’ve updated the code (see above), it should now work.

---

<div class="post-metadata">

**Author:** ![queshaw](https://avatars.discourse-cdn.com/v4/letter/q/d6d6ee/32.png) [@queshaw](https://discuss.gradle.org/u/queshaw)\
**Post date:** [December 17, 2012, 6:06pm UTC](https://discuss.gradle.org/t/share-classpath-between-buildscript-and-project-configuration/6527/5 "2012-12-17T18:06:00Z")

</div>

Thank you. That worked, with a plugin that creates the runtime configuration.

Is the buildscript block evaluated during what the documentation calls the build initialization phase? Is there a project at that point and evaluating the build script adds the extra properties to the project?

---

<div class="post-metadata">

**Author:** ![luke\_daley](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/luke_daley/32/9890_2.png) [@luke\_daley](https://discuss.gradle.org/u/luke_daley)\
**Post date:** [January 2, 2013, 6:15pm UTC](https://discuss.gradle.org/t/share-classpath-between-buildscript-and-project-configuration/6527/6 "2013-01-02T18:15:00Z")

</div>

Not quite. A build script is essentially executed in two phases. In the first, only ‘buildscript {}’ blocks are evaluated. At the end of this, what was in these blocks goes into forming the classpath for the execution of the rest of the script.
