The files of a Gradle project

A Gradle project is a folder with files whose names Gradle already knows. This lesson shows every one of them, whole, with the path it has, and then runs a build so the two folders Gradle creates appear next to them.

The files of a project

The project below is a Java project of one class, with the JUnit dependencies a test would need. Its name is shop and it has version 1.0.0.

settings.gradle

rootProject.name = 'shop'

build.gradle

plugins { id 'java' } group = 'com.example' version = '1.0.0' repositories { mavenLocal() } dependencies { testImplementation libs.junit.jupiter testRuntimeOnly libs.junit.launcher } tasks.named('test') { useJUnitPlatform() }

gradle.properties

# Independent tasks of this project run at the same time. org.gradle.parallel=true

gradle/libs.versions.toml

[versions] junit = "5.14.3" [libraries] junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" } junit-launcher = { module = "org.junit.platform:junit-platform-launcher", version = "1.14.3" }

gradle/wrapper/gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.2-bin.zip networkTimeout=10000 retries=0 retryBackOffMs=500 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists

.gitignore

# Ignore Gradle project-specific cache directory .gradle # Ignore Gradle build output directory build # Ignore Kotlin plugin data .kotlin

src/main/java/com/example/App.java

package com.example; public final class App { public static void main(String[] args) { System.out.println("shop 1.0.0"); } }

ls -A1

.gitignore
build.gradle
gradle
gradle.properties
settings.gradle
src

settings.gradle

Gradle reads the settings file before anything else. It holds the name of the root project, the projects that belong to the build and, in a larger build, the repositories the plugins come from. One line is enough for a single project: rootProject.name names the folder in every log and names the jar. Without it Gradle takes the name of the folder, which is why a checkout into shop-main/ would build shop-main-1.0.0.jar.

build.gradle

The build script is the project: the plugins it applies, its coordinates, where its dependencies come from and which of them it needs. The libs.junit.jupiter accessors come from the catalog below, and tasks.named('test') configures the task the Java plugin adds instead of creating a new one.

gradle.properties

The properties file holds values for the build tool itself, not for the project: the JVM arguments of the build, the cache switches, the proxy of a network. Here it lets independent tasks run at the same time, which changes nothing in a project of one module and a lot in a project of twenty. Gradle also reads a file of the same name from the user’s home, which is where a machine-wide org.gradle.jvmargs belongs; the last lesson of the Structure chapter lists the properties worth setting.

gradle/libs.versions.toml

A version catalog is a TOML file Gradle finds by its path. Every library in it becomes an accessor such as libs.junit.jupiter, so a version is written once and the build script holds no coordinates. The file is optional, and Gradle generates an empty one in every project init writes; the version catalog lesson is about it.

The wrapper folder

gradlew, gradlew.bat and gradle/wrapper/ are the project’s own copy of the tool: a shell script, a batch file, a small jar and the properties file above. A CI job runs ./gradlew build and gets the version the file names, whatever is installed on the machine.

The folders a build creates

Two folders appear, and both are in .gitignore because neither is source. Gradle writes .gradle/ for its own state, and the build writes build/ for everything a task produced.

gradle build --console=plain --offline --no-daemon

> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :jar
> Task :assemble
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
> Task :check UP-TO-DATE
> Task :build

BUILD SUCCESSFUL in 14s
2 actionable tasks: 2 executed

ls -A1

.gitignore
.gradle
build
build.gradle
gradle
gradle.properties
settings.gradle
src

ls build

classes
generated
libs
tmp

build/classes holds the compiled classes, build/libs the jar, build/tmp the scratch files of the compile task. Every task writes under build/, which is why deleting it is always safe and gradle clean is the same thing with the task in charge.

Related

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