How to declare a dependency on another configuration's artifact in the same project

Hi,

I’m trying to understand how I would have to declare a dependency on an artifact of a configuration in the same project so that a dependency on the task producing the artifact is established implicitly.

In the case I’m currently looking at, a project with basically the following build.gradle is supposed to build a JAR with some Java code and XML/XSL resources first, then build a PDF for the manual using the jdocbook plugin, using the resources in the JAR:

apply plugin: 'java'
apply plugin: 'jdocbook'

dependencies {
  jdocbookStyles jar.outputs.files
}

afterEvaluate {
  tasks.findAll() { task ->
    if (task.name.startsWith('stageStyles_')) {
      task.dependsOn('assemble')
    }
  }
}

jdocbook {
  manual {
    masterSourceDocumentName = 'my-manual-en.xml'
    format {
      name = 'pdf'
      finalName = "my-manual-en.pdf"
      stylesheet = "classpath:/my-jdocbook-style/xslt/my-fo.xsl"
    }
  }
}

This works - running the “buildDocs” task added by the jdocbook plugin runs the “assemble” task first, and the resulting JAR file is used by JDocBook. But I’m guessing that there is a way to avoid the “afterEvaluate” part and have this dependency added implicitly. I haven’t found it, yet, though. Changing the above dependency to

jdocbookStyles configurations.runtime

or

jdocbookStyles configurations.archives

didn’t help - I still need to add the task dependency explictly. What is the intended way of doing this?

Best regards,
Stefan

Have you tried:

dependencies {
   jdocbookStyles files(jar)
}

project.files() takes a lot of different things and turns them into a FileCollection that keeps track of the dependency.

You can also avoid the afterEvaluate hook if you defer the way you look up tasks. e.g. something like:

tasks.matching({ it.name.startsWith("stageStyles_") }).all {
   dependsOn 'assemble'
}

(Just typed this, didn’t try it.)

findAll() requires all the tasks to be added to the TaskContainer at that point. While matching() runs for any tasks that have already been added and any tasks that are added in the future.

I have just tried

jdocbookStyles files(jar)

instead of

jdocbookStyles jar.outputs.files

Unfortunately, it doesn’t remove the need for explicitly adding the task dependency, either.

It’s good to know that doing it via an afterEvaluate hook is not necessary, but adding the dependency at all is what I’m actually trying to avoid.

Ah, it looks like a bug in the custom task for jdocbook.

@Input just records the value and doesn’t add any dependencies. That would need to be @InputFiles instead. The only way to make it work would be to use tasks.matching or tasks.withType right now.

OK - good to know it’s not me doing something wrong, then. :wink: Thanks for your help!

I guess it would be good to have that line changed, then. Do you want to handle this, or should I make the change and file a pull request on GitHub?