Specify the order when a task depends on 2 other tasks

I’ve searched for a solution but could not really find an answer that would fit my needs.

I have a project that is split into 4 subprojects like so

  • main project
    – backend
    – frontend
    – shared code
    – shared resources

My backend builds a .jar file for deployment but it needs some files that are generated in the shared-resources and frontend subprojects.

So my frontend subproject generates some javascript files that we need in the jar of the backend
and the shared resources generates some css files that need to be in the jar of the backend too.

How do I make sure that that the backend module is build as last.

In my backend build.gradle I have the following task:

task resolveFrontendCode(type: Copy) {

    from "${project.rootDir}/frontend/dist"
    into "${project.buildDir}/resources/main/static/js"

    from "${project.rootDir}/shared-resources/src/main/webapp/css/build"
    into "${project.buildDir}/resources/main/static/css/build"

    from "${project.rootDir}/shared-resources/src/main/webapp/images"
    into "${project.buildDir}/resources/main/static/images"

    it.dependsOn 'buildClientSideCode'
}

compileJava.dependsOn(resolveFrontendCode)

this task will copy the generated files into the backend project but it seems that my other subprojects are build first.

the buildClientSideCode task that it dependsOn is both defined in frontend and shared-resources

I’m hoping someone could point me in the right direction. Gradle is rather new to me.

Hi @Shuyinsama,

It is generally the better approach to declare dependencies between projects instead of dependencies between tasks. E.g.:

backend.gradle

dependencies {
  implementation project(":frontend")
}

If that is set up correctly, Gradle will always execute things in the right order.

If you need to share other artifacts than the jar file(s) (which are the default artifacts if you built Java projects) you can define your own artifacts. You can also set that up without applying a Java plugin at all, if you are not compiling Java sources. Please have a look at the documentation on Sharing outputs between projects. Let me know if you are stuck with something in your non-Java centric use case.

There are quite some improvements in this area with the latest Gradle 5 and Gradle 6 versions. So I would recommend using the latest Gradle version if you can.

Hi @jendrik thank you for taking the time to reply and explain some things.

I do have those implementations along with a bunch of project dependencies like spring-boot etc.

dependencies {
    implementation project(':shared-code')
    implementation project(':shared-resources')
    implementation project(':frontend')
}

Yet when I specify the task to depend on buildClientSideCode it does not work correctly.

I did solve it for now based on the projects maven setup. I guess for that is currently what we need. Any optimisations are definitely made but we’ll take it one step at a time :slight_smile:

I have now the following setup which works perfectly for me right now while I look at the docs you gave me to alter anything

task resolveFrontendCode(type: Copy) {
    into project.buildDir

    into("resources/main/static/js") {
        from "${project.rootDir}/frontend/dist"
        include "**/*.js"
    }

    into("resources/main/static/css/build") {
        include "**/*.css"
        from "${project.rootDir}/shared-resources/src/main/webapp/css/build"
    }

    into("resources/main/static/images") {
        from "${project.rootDir}/shared-resources/src/main/webapp/images"
    }

    // This task depends on the buildClientSide Tasks for both the frontend and shared-resource
    // subprojects. These need to be build before we can copy the build files.
    it.dependsOn ':shared-resources:buildClientSideCode'
    it.dependsOn ':frontend:buildClientSideCode'
}

Thank you very much for that link that looks to be what I need.
The project has some bigger dependency issues that can be optimised further though, but this has helped me to at least get the project completely running under gradle

1 Like