groovy - Gradle: create a task that runs after the "build" task -


this seems pretty simple thing do, tried countless examples found online , fail in different way, means i'm not getting concept of how tasks work in gradle (sorry, it's still new me).

what i'm trying this: have multi-project gradle solution has several java sub-projects. each of these sub-projects want create task will:

  1. build project
  2. copy generated jar file different directory

this far got (it might missing little thing or might incorrect approach, hope explain me): in root build.gradle i've put this:

subprojects{     plugins.withtype(javaplugin){ //create copyoutput task java projects         tasks.create("copyoutput"){             path infile = paths.get(builddir.getcanonicalpath() + "/libs/" + project.name + ".jar")             path outfile = paths.get(rootproject.projectdir.getcanonicalpath() + "/bin/" + project.name + ".jar")             files.copy(infile, outfile, replace_existing)         }         tasks['copyoutput'].dependson tasks['build'] //only run after build (doesn't work, runs @ configuration time)     } } 

the problem this, can gather stack traces i'm getting, executes task immediately @ configuration time, , every time (not when gradle copyoutput, always, if gradle clean).

evidently i'm not understanding how task creation , dependency work. can clarify?

you need add action (<<):

subprojects{     plugins.withtype(javaplugin){ //create copyoutput task java projects         tasks.create("copyoutput") << {             path infile = paths.get(builddir.getcanonicalpath() + "/libs/" + project.name + ".jar")             path outfile = paths.get(rootproject.projectdir.getcanonicalpath() + "/bin/" + project.name + ".jar")             files.copy(infile, outfile, replace_existing)         }         tasks['copyoutput'].dependson tasks['build'] //only run after build (doesn't work, runs @ configuration time)     } } 

Comments