Copying dependencies from a subset of subprojects, and excluding the jars produced by those projects

So, I have a complex project with about 6 subprojects that builds an application (ultimately building a single application jar). However, only 3 subprojects go into the application jar, the others are utility projects for setting up data etc.

What I’m trying to write is a task that will let me define a list of the projects to include (e.g., Set targetProjects = [‘common’, ‘schema’, ‘mainapp’ ] )

From that target list of subprojects I want to

  • copy all the classes produced by those subprojects into a single directory, to be assembled into a single “application” jar
  • copy all the dependencies of those subprojects (third party libraries) into a “lib” directory, BUT, excluding the jar files produced by each subproject. E.g., the default behaviour would (in my example) create “common.jar”, “schema.jar”, and “mainapp.jar” under the build/libs directory. And since common and schema is a (project) dependency for “mainapp”, those jars end up getting included when I try to build my consolidated “lib” directory.

Any suggestions? I’ve been trying various approaches for days now, and am just getting lost.