Task dependency to tasks in other projects


(Yennick Trevels) #1

I’ll start by explaining what I want to do. This is the scenario:

projectA {

}

projectB {

dependencies {

merged project(’:projectA’)

} }

Both projects have a compile and asdoc task. When the compile task is executed it will first compile ProjectA and then use the resulting swc when compiling projectB.

Now here’s my problem. The asdoc task also needs the projectA swc (but doesn’t need the projectB swc), but this is only generated when the compile task is executed. So basically I want the projectA.compile task to be executed before the projectB.asdoc task is executed, while not executing the projectB.compile task. Is there an easy way to declare this dependency in my plugin’s asdoc task?


(Peter Niederwieser) #2

It’s as easy as ‘dependsOn(":projectA:compile")’.


(Yennick Trevels) #3

yes, but that’s when you know which projects are involved. But I want to do this in a more generic way because in my GradleFx plugin (plugin to compile flex projects) I don’t know beforehand which projects are involved.

So I’d want to do something like: class AsDoc extends DefaultTask {

AsDoc() {

dependsOn(":dependantProjects:compile")

} }


(Rolf Suurd) #4

Have you tried retrieving dependant projects from the current project (Project#getDependsOnProjects) ?

You should have access from the current project in your custom Task.

Or, configure it in a “whenTaskAdded” closure.


(Yennick Trevels) #5

I didn’t use dependsOnProjects for two reasons: 1. one of the developers mentioned he wanted to deprecate dependsOnProjects() and dependsOn() --> http://gradle.1045684.n5.nabble.com/Remove-Project-dependsOn-td5147791.html 2. dependsOnProjects only works when you specify dependsOn(someProject), which isn’t the case when it’s a artifact dependency specified in the dependencies block

So it’s not this easy I’m afraid :slight_smile:


(Peter Niederwieser) #6

The usual way to do this is to make projectA publish its swc artifact(s) (in an ‘artifacts’ block), and make projectB consume them (in a ‘dependencies’ block). The ‘asDoc’ task would accept the swc(s) as a ‘@InputFiles FileCollection classpath’ (the wiring could be done by a plugin). This will allow Gradle to automatically deduce that it has to compile the swc(s) before executing the ‘asDoc’ task. This is similar to how the ‘Javadoc’ task works.


(Yennick Trevels) #7

Thanks Peter! I’ll try this out.


(Yennick Trevels) #8

This piece of code which is being used when adding the javadoc task to the project did exactly what I needed:

Project project = task.getProject();
final Configuration configuration = project.getConfigurations().getByName(configurationName);
task.dependsOn(configuration.getTaskDependencyFromProjectDependency(useDependedOn, otherProjectTaskName));

https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/groovy/org/gradle/api/plugins/JavaPlugin.java

Thanks again Peter :slight_smile: