Need dependency before compile task


(Matthew Adams) #1

Hi all,

I have a code generation task that extends type: Exec and needs a maven dependency to be downloaded before it can run, and the code generation task must run before my compileJava task.

What would be a good way to force the dependency to be downloaded to my ~/.m2/repository before my code generation task executes? There doesn’t appear to be a dependencies scope that I can find that will make that happen, despite trying compileOnly and annotationProcessor.

Thanks!


(Maksim Kostromin) #2

not sure about gradle configuration you are looking for, but as workaround, try this:

# downloading dependencies excluding `yourExecTask` execution
./gradlew assemble -x yourExecTask

# after dependencies where cached in mavenLocal()
./gradlew yourExecTask

Regards,
Maksim


(Matthew Adams) #3

Thanks @daggerok, but I’d like for the solution to be transparent to the other developers. I suppose I could add another task that’s also of type: Exec that invokes gradlew with the commands you gave.

Still, it seems like there ought to be a way in gradle to force the download of Maven dependencies.


(Maksim Kostromin) #4
  1. create a library, where your code generation task will be bundle jar library file with all generated sources
  2. in that shared-library project add dependencies closure with all needed transitive dependencies
  3. in any other projects uses your library just use regular dependencies closure to depends on that project

settings.gradle

root.projectName = 'parent'
include 'shared-library'
include 'app-1'
include 'app-2'
// include more subprojects...

in shared-library/build.gradle:

apply plugin: 'java-library' // or apply plugin: 'java' // it depends..

dependencies {
  // add here needed dependencies...
}

task myExecTask(type Exec) {
  // generate sources...
}

jar.dependsOn myExecTask
jar.shouldRunAfter myExecTask

in app-1/build.gradle and app-2/build.gradle:

dependencies {
  compile(project(':shared-library'))
  // other deps...
}

Also I would recommend you look at composite builds gradle feature: https://docs.gradle.org/current/userguide/composite_builds.html
maybe it’s exact what you are looking for…


Regards,
Maksim Kostormin