# Import own Functions and Classes

**URL:** <https://discuss.gradle.org/t/import-own-functions-and-classes/19894>\
**Category:** Help/Discuss\
**Created:** [October 6, 2016, 2:20pm UTC](https://discuss.gradle.org/t/import-own-functions-and-classes/19894 "2016-10-06T14:20:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![awltr](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@awltr](https://discuss.gradle.org/u/awltr)\
**Post date:** [October 6, 2016, 2:20pm UTC](https://discuss.gradle.org/t/import-own-functions-and-classes/19894/1 "2016-10-06T14:20:58Z")

</div>

Hello,

we’re planning to use gradle for our build processes. We want to provide one Build Project for all our solutions. In case of modularisation and multiple usage of some global functionally we have a project directory like this (using gradle-wrapper):

```
root
  global.gradle
  gradlew
  gradlew.bat
root/gradle/wrapper
  gradle dependencies
root/build/solutionname
  build.gradle
  some properties and settings

```

The build.gradle first contains the import of global.gradle:

`apply from: '../../global.gradle'`

It has cost us some time to figure out how the functions needs to be defined in global.gradle, so we can use them in the other build.gradle files:

```
ext.foo = { param ->
  //do sth
}

```

in my opinion it’s kinda different to the documentation how you can organize the build logic: [Chapter 42. Organizing Build Logic](https://docs.gradle.org/current/userguide/organizing_build_logic.html)

However, it works for us. My question is now, how own classes in global.gradle can be provided? A class like this is not working:

```
class Foo {
  Foo(param) {
    //do sth
  }
  //functions
}

```

I’m also open for other solutions to handle the Import. I also tried it like explained here: [42.1. Inherited properties and methods](https://docs.gradle.org/current/userguide/organizing_build_logic.html#sec:inherited_properties_and_methods). Unfortunatly that didn’t worked for us.

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [October 6, 2016, 4:49pm UTC](https://discuss.gradle.org/t/import-own-functions-and-classes/19894/2 "2016-10-06T16:49:00Z")

</div>

You’ll want to use the [buildSrc](https://docs.gradle.org/current/userguide/organizing_build_logic.html#sec:build_sources) project for sharing common build script classes.

Also, instead of doing `apply from: '../../global.gradle'`, you can just have a build.gradle in your root and use a `subprojects {}` block to [inject configuration](https://docs.gradle.org/current/userguide/organizing_build_logic.html#sec:injected_configuration) into subprojects.

---

<div class="post-metadata">

**Author:** ![awltr](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@awltr](https://discuss.gradle.org/u/awltr)\
**Post date:** [October 11, 2016, 4:42pm UTC](https://discuss.gradle.org/t/import-own-functions-and-classes/19894/3 "2016-10-11T16:42:50Z")

</div>

thanks for your quick reply. now i have a project directory like this:

```
root
  global.gradle
  gradlew
  gradlew.bat
root/gradle/wrapper
  gradle dependencies
root/buildSrc
   build.gradle
root/build/solutionname
  build.gradle
  some properties and settings

```

the Classes defined in buildSrc/build.gradle can not be used in build/solutionname/build.gradle.

–debug says the source dir does not exist:  
18:39:52.664 [DEBUG] [org.gradle.initialization.buildsrc.BuildSourceBuilder] Starting to build the build sources.  
18:39:52.664 [DEBUG] [org.gradle.initialization.buildsrc.BuildSourceBuilder] Gradle source dir does not exist. We leave.

I’m sure i got something wrong but i don’t get it.

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [October 11, 2016, 8:32pm UTC](https://discuss.gradle.org/t/import-own-functions-and-classes/19894/4 "2016-10-11T20:32:29Z")

</div>

Please read the user guide chapter I linked above to understand the `buildSrc` project. You don’t define classes in it’s build.gradle, you move them to actual java source code, which gets compiled.

---

<div class="post-metadata">

**Author:** ![awltr](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@awltr](https://discuss.gradle.org/u/awltr)\
**Post date:** [October 13, 2016, 2:00pm UTC](https://discuss.gradle.org/t/import-own-functions-and-classes/19894/5 "2016-10-13T14:00:53Z")

</div>

ok, i got it. i’ve misunderstood the location of “root project directory” in this context. and that’s unfortunately also my last question. now i need a structure like this:

```
project
  global.gradle
  gradlew
  gradlew.bat
project/gradle/wrapper
  gradle dependencies
project/build/solutionname
  build.gradle
  some properties and settings
project/build/solutionname/buildSrc
  java-classes in src/main/java
  internal compiled sources

```

the buildSrc-directory needs to be in the root of my solution (project/build/solutionname). it would be nicer if i can put it in the project-root because it contains functionality for all solutions.

i only found [this](https://discuss.gradle.org/t/is-it-possible-to-change-buildsrc-location-e-g-in-settings-gradle/6101). so maybe that won’t work :(.

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [October 13, 2016, 3:26pm UTC](https://discuss.gradle.org/t/import-own-functions-and-classes/19894/6 "2016-10-13T15:26:52Z")

</div>

You can make the whole thing one large multi-project build including all solutions. Would that work for you?

---

<div class="post-metadata">

**Author:** ![awltr](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@awltr](https://discuss.gradle.org/u/awltr)\
**Post date:** [October 13, 2016, 4:21pm UTC](https://discuss.gradle.org/t/import-own-functions-and-classes/19894/7 "2016-10-13T16:21:26Z")

</div>

yes, that was my actual intension 🙂

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [October 13, 2016, 6:29pm UTC](https://discuss.gradle.org/t/import-own-functions-and-classes/19894/8 "2016-10-13T18:29:34Z")

</div>

Then you can have buildSrc in the root and everything will work 🙂

---

<div class="post-metadata">

**Author:** ![awltr](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@awltr](https://discuss.gradle.org/u/awltr)\
**Post date:** [October 14, 2016, 9:12am UTC](https://discuss.gradle.org/t/import-own-functions-and-classes/19894/9 "2016-10-14T09:12:22Z")

</div>

ok, so i think i’m right [here](https://docs.gradle.org/current/userguide/multi_project_builds.html). many thanks for your help
