Import own Functions and Classes


(Andreas) #1

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

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. Unfortunatly that didn’t worked for us.


(Stefan Oehme) #2

You’ll want to use the buildSrc 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 into subprojects.


(Andreas) #3

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.


(Stefan Oehme) #4

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.


(Andreas) #5

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. so maybe that won’t work :(.


(Stefan Oehme) #6

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


(Andreas) #7

yes, that was my actual intension :slight_smile:


(Stefan Oehme) #8

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


(Andreas) #9

ok, so i think i’m right here. many thanks for your help