Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

gradle

build

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?

like image 605
MKB Avatar asked Jul 25 '18 11:07

MKB


People also ask

How do I get rid of Gradle build folder?

If you wish to clean (empty) the build directory and do a clean build again, you can invoke a gradle clean command first and then a gradle assemble command. Now, fire the gradle assemble command and you should have a JAR file that is named as <name>-<version>. jar in the build/libs folder.

What is multi-project build in Gradle?

A multi-project build in Gradle consists of one root project, and one or more subprojects. A basic multi-project build contains a root project and a single subproject. This is a structure of a multi-project build that contains a single subproject called app : Example 1. Basic multi-project build.


Video Answer


2 Answers

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()
}
like image 179
ToYonos Avatar answered Oct 21 '22 01:10

ToYonos


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
like image 33
Николай Панюков Avatar answered Oct 21 '22 01:10

Николай Панюков