Automatically substitute module dependencies for local projects

My situation is this: I have many Gradle-powered projects (100+).

The projects can be divided into 3 groups:

  • lib - can include other libs
  • plugin - can include libs and run in stand-alone mode
  • app - can include libs and plugins and is runnable

Depending on current task the projects can be checked out from repository, e.g. 1 app, 1 plugin and 3 libs.

I would like to automate the process to replace the module deps to artefactory with project deps to local projects.

I tried to add the following (as include) to settings.gradle:

def filter = { it.directory && rootProject.name != it.name && someOtherCriterias }

rootProject.projectDir.parentFile.listFiles( filter as FileFilter ).each{ File f ->
  includeBuild( f ){
    dependencySubstitution {
      substitute module( "my.company:$f.name" ) with project( ':' )
    }
  }
}

This solution works only half-way and includes all folders from workspace as gradle builds, also circular deps are possible.

Is there a nice way to achieve what I want automatically?