MKB
MKB

Reputation: 7619

gradle: disable creation of the build folder at root in multi-projects application

My gradle project is a multi-project structure. It consists of two subprojects. My build.gradle in root project as following:

allprojects {
  apply plugin: 'eclipse'
}

subprojects {
  apply plugin: 'java'
  apply plugin: 'maven-publish'

  publishing {
    repositories {
      maven {
        name 'myMaven'
        def suffix = project.version.endsWith("SNAPSHOT") ? "snapshots" : "releases"
        url baseUrl + suffix
      }
    }

    publications {
      core(MavenPublication) {
        artifactId project.name
        artifact jar
      }
    }
  }
}

project(':projectA') {
  ext {
    sharedManifest = manifest {
      attributes("Implementation-Title": project.name,
                 "Implementation-Version": 1.0 )
    }
  }
  jar {
    manifest = project.manifest {
      from sharedManifest
    }
  }
}

project('projectB') {
  ext {
    sharedManifest = manifest {
      attributes("Implementation-Title": project.name,
                 "Implementation-Version": 2.0 )
    }
  }
  jar {
    manifest = project.manifest {
      from sharedManifest
    }
  }
}

After gradle build, I got three build folders. one at the root directory and other two in the respective subprojects. The root project is just an empty folder without any source code, it is just a multi-project container.

Now I need to eliminate the creation of root build folder since it is of no use. I search on the net and in the gradle docs/forum but did not get any hit.

Is there any way so that gradle stop creating build folder at root level?

Upvotes: 16

Views: 4408

Answers (2)

Nikolay Panyukov
Nikolay Panyukov

Reputation: 41

I think I have a more correct solution, without having to delete the /build folder after creation :)

As we know, when we append some plugins to our gradle project, as a rule it adds some tasks to gradle lifecycle. So you could disable tasks, that create /build folder.

With only java plugin, you could disable build and jar tasks.
To do this, add to root build.gradle file this lines:

jar.enabled = false
build.enabled = false

If you have a org.springframework.boot plugin, disable another two tasks:

bootJar.enabled = false
bootJarMainClassName.enabled = false

Upvotes: 4

ToYonos
ToYonos

Reputation: 16833

Who is responsible for the build dir creation ?

According to Mark Vieira, a Gradle core dev, each task is responsible for the creation of the ouput dir as long as it creates output.

So basically, it your root project does have a build dir, it means it's not empty and something is deliberately writing into it.

In my job, I'm managing a big project constituted by a root project, which handles many .gradle files, settings files etc. and many subprojects with the actual code. When I run gradle clean build, all subprojects get their own build dir with libs, binaries etc. but the root project does not end up with any build dir because no task whatsoever writes output in the root build dir.

What can I do to remove the root build dir, whatever task creating it ?

Just set up a buildFinished hook only for the root project

gradle.buildFinished {
    project.buildDir.deleteDir()
}

Upvotes: 12

Related Questions