settings.gradle and rootProject.name

Gradle reads one file before any build script: settings.gradle, or settings.gradle.kts. It decides what the build is made of. A single project can get away with a line; a build of three projects is named, included and pointed at its plugin repositories here.

The settings file

settings.gradle

pluginManagement { repositories { mavenLocal() gradlePluginPortal() } } rootProject.name = 'shop' include 'core'

build.gradle

// A root project with no code of its own. plugins { id 'base' }

core/build.gradle

plugins { id 'java' }

rootProject.name

The name is what every log line, every report and the jar file use. Gradle takes the name of the folder when the line is missing, so a project cloned into shop-main/ builds shop-main-1.0.0.jar until the line pins shop. Pin it in the first commit.

include

Every include adds a project. The argument is a path relative to the settings file, with a colon for a nested folder: include 'services:api' reads services/api/build.gradle and gives the project the path :services:api. A folder that is not included is not part of the build, even when it has a build script.

The root project has a build script of its own here because it applies base, which brings clean and assemble to the root without compiling anything. A root project without a build script works just as well.

pluginManagement

A plugin that is not one of Gradle’s own is downloaded from a repository. The pluginManagement block says which repositories, and it lives in the settings file because the plugins of the settings file itself need it first. The two lines above put the local Maven repository in front of the Gradle Plugin Portal, which is how a company build uses a plugin it publishes itself. Neither repository is reached in this offline container, and the build below needs neither: base and java are core plugins.

gradle projects

gradle projects --console=plain --offline --no-daemon > build.log 2>&1; sed "s#$PWD#<project>#g" build.log


> Task :projects

Projects:

------------------------------------------------------------
Root project 'shop'
------------------------------------------------------------

Location: <project>

Project hierarchy:

Root project 'shop'
\--- Project ':core'

Project locations:

project ':core' - /core

To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :core:tasks

BUILD SUCCESSFUL in 11s
1 actionable task: 1 executed

Two projects: the root and :core, with the path every other command uses. gradle :core:build builds one of them, gradle build builds all of them, and the multi-project lesson takes the build apart.

Related

Автор статьи: Arthur Isaev