Is it possible to set up a gradle project with more than 2 levels?

前端 未结 4 957
青春惊慌失措
青春惊慌失措 2020-12-14 06:19

We have been using gradle for about a year and have been somewhat successful with it. A number of features are still a little opaque, but we are getting there. I am not sure

相关标签:
4条回答
  • 2020-12-14 06:48

    Kotlin version of settings.gradle.kts

    fileTree(".") {
        include("**/build.gradle")
        include("**/build.gradle.kts")
        exclude("buildSrc/**")
        exclude("build.gradle.kts")
    }.map {
        relativePath(it.parent)
            .replace(File.separator, ":")
    }.forEach {
        include(it)
    }
    

    Instead of adding it manually like this (like Intellij Idea suggests):

    include("modules:core")
    findProject(":modules:core")?.name = "core"
    
    0 讨论(0)
  • 2020-12-14 06:50

    I would simply put the following in TopProject/settings.gradle:

    include 'Project1:SubProject1'
    include 'Project1:SubProject2'
    include 'Project2:SubProject1'
    ...
    

    The only change required to the projects you currently have is to remove settings.gradle files from them as you can only have one setting.gradle file per project structure.

    0 讨论(0)
  • 2020-12-14 06:50

    This is working quite well:

    ext{
      buildPrefix = "build"
      allProjects = ["P1", "P2"]
    }
    
    def createTasks(String prefix) {
      def newTasks = allProjects.each {
        def pName ->
          def tName = "$prefix$pName"
          tasks.add(name: tName, type: GradleBuild) {
            dir = pName
            tasks = [ prefix ]
          }
      }
    }
    
    createTasks(buildPrefix)
    
    ext {
      buildTasks = tasks.findAll{ t -> t.name.startsWith(buildPrefix) }
    }
    
    task build(dependsOn: buildTasks) {}
    

    I can just add the other tasks that I want to expose at the top level.

    Thanks for the pointers.

    0 讨论(0)
  • 2020-12-14 07:03

    I had a similar problem. The simplest solution was to configure gradle by adding projects in the $root/settings.gradle file similar to the Erdi's answer. However, I managed to automatically add all subprojects. The logic will simply go through my directory structure and find all directories that contain build.gradle and add them as subprojects.

    Here is how to do it:

    • Make sure you don't have any other settings.gradle but the root one
    • The content of the root settings.gradle file should be:
    fileTree('.') {
      include '**/build.gradle'
      exclude 'build.gradle' // Exclude the root build file.
    }.collect { relativePath(it.parent).replace(File.separator, ':') }
     .each { include(it) }
    

    I hope this helps.

    0 讨论(0)
提交回复
热议问题