Home > Net >  How can I generate an entire gradle subproject and have it as a dependency in another project?
How can I generate an entire gradle subproject and have it as a dependency in another project?

Time:01-11

So let's say I have the following settings.gradle:

include 'math-server'
project(':math-server').projectDir = file('math/server')

include 'math-client-gen'
project(':math-client-gen').projectDir = file('math/client')

include 'ai'

Now I'd like to not commit the any of the files in math-client-gen (including the build.gradle) since those are generated by a build job in math-server:

// math/server/build.gradle
task generateClient(type:Exec) {
    workingDir '.'
    inputs.dir('./app')
    inputs.dir('.')
    outputs.dir('../client')
    commandLine './client-generator/generate.sh'
}

The generate.sh leverages the openapi client generator for kotlin.

Now the ai project relies on the math-client-gen:

// ai/build.gradle
dependencies {
    compile project(':math-client-gen')
}

Now I have currently found two suboptimal ways to make this work.

Option 1 is to run ./gradlew :math-server:generateClient before I'm able to run ./gradlew :ai:build. This sucks, since you cannot build ai on its own anymore.

Option 2 is to commit the files, which of course also isn't the way it should be.

I'm sure there is a better way to do it with gradle, but I just didn't manage to find it yet. As a compromise, I'd be willing to commit the generated math-client-gen/build.gradle if it doesn't work without that.

What's the best solution to this problem?

Note: I also saw something like:

    implementation files(layout.buildDirectory.dir('classes')) {
        builtBy 'compile'
    }

in the docs, that looks promising, but i'd like to have it for an entire subproject and not just some source files if possible.

CodePudding user response:

// ai/build.gradle

afterEvaluate {
    tasks.build.dependsOn(project(":math-client").tasks["generateClient"])
}

To automate your first option.

CodePudding user response:

I ended up committing the build.gradle of the math-client-gen and have this line there:

// math/client/build.gradle
compileKotlin {
    dependsOn ':math-server:buildMathClient'
}

This ensures that the client is always generated when this project is listed as a dependency somewhere.

Then you can simply add the math-client-gen as a dependency in other projects, no additional steps required:

// ai/build.gradle
dependencies {
    compile project(':math-client-gen')
}
  •  Tags:  
  • Related