Problems with project dependencies when using Gradle with Ant

(Nho Jotom) #1

I have a simple layout of two subprojects, both of which simply import Ant build.xml’s. Project B depends on project A


include ‘:a:A’

include ‘:b:B’


ant.importBuild ‘build.xml’

defaultTasks ‘jar’


ant.importBuild ‘build.xml’

defaultTasks ‘jar’

How do I configure B so it’s dependent upon A so that if A isn’t built and I build B, A gets built first, then B.

(Peter Niederwieser) #2

Execution dependencies are always between tasks, not between projects. You could, for example, add the following task dependency to ‘b/B/build.gradle’:

// adapt as necessary

(Nho Jotom) #3

Thanks for this suggestion Peter. However, this solution isn’t working for me. I added println’s in both A and B’s build.gradle files to print the name, group, and version of each project. When I add the jar.dependsOn(":a:A:jar’) to b/B/build.gradle and run ‘gradle jar’, I see the output from the println’s I put into A’s build.gradle file, but the jar task never gets executed. This simply tells me A’s build.gradle is being executed. I tried using dependencies from other tasks from A (e.g. clean), but none A’s tasks get executed. I then added this to A’s build.gradle:

jar {

println “A’s jar task is being executed” }

And I see this output when running ‘gradle jar’ from B, but the A’s jar task which builds the JAR never gets executed. Clearly I’m missing something here

(Peter Niederwieser) #4

Can you provide a reproducible example? Otherwise, I can’t say what’s going wrong. Adding task dependencies should “just” work.

Regarding the println’s, you may want to read up on configuration phase vs. execution phase (e.g. in the Gradle User Guide). Unless you enclose the println’s in ‘someTask.doFirst { … }’, you will always see them, no matter which tasks get executed.

(Nho Jotom) #5

Hi Peter. I do have reproducible test case. Is there a way to upload a JAR of the files or do I need to paste all code here?

BTW, I found something which may be useful. When I use a/A as my “first” project and have B depend on A, it works fine. However, if I create a project named c/C as my “first” project and have B depend on C, it fails. Running ‘gradle --debug’, I see ‘Tasks to be executed: ’ seems to show the tasks being executed in alphabetical order by project name. So the B tasks show first even though I’ve listed jar.dependsOn(’:c:C:jar’) in B’s build.gradle. Unfortunately, I cannot depend on alphabetical ordering.

After some researching, it seems alphabetical ordering is the norm for Gradle if dependencies are not defined. It seems that jar.dependsOn(’:c:C:jar’) isn’t being honored for some reason.