Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between allprojects and subprojects

Tags:

gradle

People also ask

What is allprojects in gradle?

The " allprojects " section is for the modules being built by Gradle. Oftentimes the repository section is the same for both, since both will get their dependencies from jcenter usually (or maybe maven central).

What is Buildscript in build gradle?

buildscript: This block is used to configure the repositories and dependencies for Gradle. dependencies: This block in buildscript is used to configure dependencies that the Gradle needs to build during the project.

What is EXT in build gradle?

ext is shorthand for project. ext , and is used to define extra properties for the project object. (It's also possible to define extra properties for many other objects.) When reading an extra property, the ext. is omitted (e.g. println project. springVersion or println springVersion ).


In a multi-project gradle build, you have a rootProject and the subprojects. The combination of both is allprojects. The rootProject is where the build is starting from. A common pattern is a rootProject has no code and the subprojects are java projects. In which case, you apply the java plugin to only the subprojects:

subprojects {
    apply plugin: 'java'
} 

This would be equivalent to a maven aggregate pom project that just builds the sub-modules.

Concerning the two syntaxes, they do the exact same thing. The first one just looks better.


Adding to Ryan's answer, the configure method becomes important when you want to configure custom subsets of objects. For example configure([project(":foo"), project(":bar")]) { ... } or configure(tasks.matching { it.name.contains("foo") }) { ... }.

When to use allprojects vs. subprojects depends on the circumstances. Often you'll use both. For example, code related plugins like the Java plugin are typically applied to subprojects, because in many builds the root project doesn't contain any code. The Eclipse and IDEA plugins, on the other hand, are typically applied to allprojects. If in doubt, look at examples and other builds and/or experiment. The general goal is to avoid irrelevant configuration. In that sense, subprojects is better than allprojects as long as it gives the expected results.