# How to refer to classes on the buildscript classpath in an applied script

**URL:** https://discuss.gradle.org/t/how-to-refer-to-classes-on-the-buildscript-classpath-in-an-applied-script/6811
**Category:** Old Forum Archive
**Created:** [November 28, 2012, 11:36am UTC](https://discuss.gradle.org/t/how-to-refer-to-classes-on-the-buildscript-classpath-in-an-applied-script/6811 "2012-11-28T11:36:00Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mattkhan](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mattkhan/32/91_2.png) [@mattkhan](https://discuss.gradle.org/u/mattkhan)
#### Post date: [November 28, 2012, 11:36am UTC](https://discuss.gradle.org/t/how-to-refer-to-classes-on-the-buildscript-classpath-in-an-applied-script/6811/1 "2012-11-28T11:36:00Z")

</div>

I add some dependencies (containing our plugins) to the buildscript classpath. I then want to refer to some class I’ve added in the script and set some ‘ext’ property. For example, lets say I just want to get a reference to ‘SomeEnum.values()’ and ‘SomeEnum’ is on my classpath.

As far as I can see, this does not work when the code is shifted to a gradle script that is applied and I can’t see why this would be so. For example, this works;

```gradle
// build.gradle
ext {
    someValues = com.mycomponent.SomeEnum.values()
}
  someValues.each {
     // do something with it
}

```

but this does not

```gradle
// build.gradle
apply from: projects.gradle
  // projects.gradle
ext {
    someValues = com.mycomponent.SomeEnum.values()
}
  someValues.each {
     // do something with it
}

```

The latter blows with

```gradle
* What went wrong:
A problem occurred evaluating script.
> cannot get property 'com' on extra properties extension as it does not exist

```

---

<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: [November 28, 2012, 12:47pm UTC](https://discuss.gradle.org/t/how-to-refer-to-classes-on-the-buildscript-classpath-in-an-applied-script/6811/2 "2012-11-28T12:47:00Z")

</div>

Officially, each build script needs to declare its build script dependencies on its own. In the current implementation, a child build script can also see the parent script’s dependencies, but this isn’t the case for applied build scripts.

---

<div class="post-metadata">

### Author: ![Detelin](https://avatars.discourse-cdn.com/v4/letter/d/3e96dc/32.png) [@Detelin](https://discuss.gradle.org/u/Detelin)
#### Post date: [November 28, 2012, 12:47pm UTC](https://discuss.gradle.org/t/how-to-refer-to-classes-on-the-buildscript-classpath-in-an-applied-script/6811/3 "2012-11-28T12:47:00Z")

</div>

It seems to be designed that way, the build script’s ClassLoader is used only for project’s build script and not for any other script, see [AbstractProject.beforeCompile](https://github.com/gradle/gradle/blob/RB_1.3/subprojects/core/src/main/groovy/org/gradle/api/internal/project/AbstractProject.java#L224)

---

<div class="post-metadata">

### Author: ![mattkhan](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mattkhan/32/91_2.png) [@mattkhan](https://discuss.gradle.org/u/mattkhan)
#### Post date: [November 28, 2012, 1:27pm UTC](https://discuss.gradle.org/t/how-to-refer-to-classes-on-the-buildscript-classpath-in-an-applied-script/6811/4 "2012-11-28T13:27:00Z")

</div>

what is a “child build script” exactly?

This seems slightly counterintuitive and/or inconsistent though given that the applied script references things (plugins) that only exist by virtue of the classpath specified in the applying script.

> In the current implementation

does this imply the implementation is changing?

---

<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: [November 28, 2012, 1:38pm UTC](https://discuss.gradle.org/t/how-to-refer-to-classes-on-the-buildscript-classpath-in-an-applied-script/6811/5 "2012-11-28T13:38:00Z")

</div>

A child build script is the build script of a child (i.e. sub) project.

> does this imply the implementation is changing?

It means that the inheritance behavior isn’t a guaranteed feature.

---

<div class="post-metadata">

### Author: ![mattkhan](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mattkhan/32/91_2.png) [@mattkhan](https://discuss.gradle.org/u/mattkhan)
#### Post date: [November 28, 2012, 1:56pm UTC](https://discuss.gradle.org/t/how-to-refer-to-classes-on-the-buildscript-classpath-in-an-applied-script/6811/6 "2012-11-28T13:56:00Z")

</div>

OK thanks. This implies that this should work then (but it doesn’t) or are you saying that inheritence only applied if it is within the same script?

```gradle
// build.gradle start
buildscript {
    // add something to classpath
}
  // do some root project configuration
  apply from: project.gradle
  // build.gradle end
  // project.gradle start
  project('projectA') {
    ext {
        someValues = com.mycompany.SomeEnum.values()
    }
}
// project.gradle end

```

---

<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: [November 28, 2012, 2:01pm UTC](https://discuss.gradle.org/t/how-to-refer-to-classes-on-the-buildscript-classpath-in-an-applied-script/6811/7 "2012-11-28T14:01:00Z")

</div>

It won’t work because it’s an applied build script, but the class path is only inherited by child build scripts (i.e. build scripts of child projects).
