0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · Leçon

Sous-projets et configurations

Définissez et configurez des sous-projets en comprenant leurs interactions au sein d’une construction multi-projets.

Sous-projets et configurations est une leçon Groovy & Gradle: JVM Automation and Build Engineering gratuite sur CoddyKit. Ceci est la leçon 2 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Groovy & Gradle: JVM Automation and Build Engineering, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Groovy & Gradle: JVM Automation and Build Engineering comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

Welcome to Subprojects!

In large software projects, keeping everything in one giant build file can get messy. This is where subprojects come in handy!

Subprojects allow you to break down a big project into smaller, manageable modules. Think of them as mini-projects living within your main project.

  • Modularity: Organize code logically.
  • Reusability: Share common logic across modules.
  • Faster Builds: Gradle can optimize builds by only working on changed subprojects.

Declaring Subprojects

To tell Gradle about your subprojects, you use the settings.gradle file. This file lives in your root project directory.

You use the include keyword, followed by the path to your subproject's directory. Each subproject usually resides in its own folder.

/* settings.gradle */
rootProject.name = 'my-multi-project'

include 'app'
include 'library'

Subproject Directory Structure

Once declared in settings.gradle, your project structure will typically look like this:

  • my-multi-project/ (root project)
    • settings.gradle
    • build.gradle (root build file)
    • app/ (subproject 1)
      • build.gradle
      • ... (source code, resources)
    • library/ (subproject 2)
      • build.gradle
      • ... (source code, resources)

Each subproject gets its own build.gradle file.

Subproject Build Files

Just like your root project, each subproject has its own build.gradle file. This allows you to define specific configurations, dependencies, and tasks for that particular module.

For example, your app subproject might build a runnable application, while your library subproject might produce a reusable JAR.

/* library/build.gradle */
plugins {
    id 'java'
}

group 'com.coddykit'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation platform('org.junit:junit-bom:5.9.1')
    testImplementation 'org.junit.jupiter:junit-jupiter'
}

Defining a Subproject Task

You can define tasks within a subproject's build.gradle file. These tasks are then specific to that subproject.

Let's add a simple task to our library subproject that prints a message.

/* library/build.gradle */
plugins {
    id 'java'
}

group 'com.coddykit'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

task helloLibrary {
    doLast {
        println "Hello from the Library subproject!"
    }
}

Executing Subproject Tasks

To run a task belonging to a specific subproject from the root directory, you use a colon : to separate the subproject name and the task name.

For example, to run the helloLibrary task from our library subproject:

public class Main {
  public static void main(String[] args) {
    System.out.println("To run the task: gradle :library:helloLibrary");
    System.out.println("Output will be: Hello from the Library subproject!");
  }
}

Common Configuration: allprojects

Sometimes, you want to apply the same configuration to all projects in your multi-project build (the root project and all its subprojects).

You can do this using the allprojects { ... } block in your root build.gradle file.

/* build.gradle (root) */

allprojects {
    group 'com.coddykit.common'
    version '1.0.0'

    repositories {
        mavenCentral()
    }
}

Targeting Specific Subprojects

While allprojects is great for common settings, you often need to configure individual subprojects from the root build file.

You can use the project(':subprojectName') { ... } block to apply configuration specifically to one subproject.

/* build.gradle (root) */

project(':app') {
    apply plugin: 'application'
    mainClassName = 'com.coddykit.app.MainApp'
}

project(':library') {
    apply plugin: 'java-library'
}

Sharing Properties

You can also share properties or variables from your root project to all subprojects. This is useful for things like consistent version numbers or build flags.

Use the ext block (for "extra properties") in the root build.gradle and access it with rootProject.ext.propertyName in subprojects.

/* build.gradle (root) */
ext {
    groovyVersion = '3.0.9'
    springBootVersion = '2.7.5'
}

allprojects {
    // Access properties in subprojects
    println "Groovy Version: ${rootProject.ext.groovyVersion}"
}

Multi-Select Check

Which of the following statements about Gradle subprojects and their configuration are TRUE?

Subprojects Recap

Great job! You've learned the basics of setting up and configuring Gradle subprojects.

  • Subprojects help organize large builds.
  • They are declared in settings.gradle using include.
  • Each subproject has its own build.gradle.
  • You can configure all projects using allprojects { ... } or specific ones using project(':name') { ... }.
  • Tasks are run with the :subproject:task syntax.

Next, we'll explore how subprojects can depend on each other!

Questions Fréquemment Posées

La leçon « Sous-projets et configurations » est-elle gratuite ?

Oui — le texte complet de « Sous-projets et configurations » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Groovy & Gradle: JVM Automation and Build Engineering, passe à CoddyKit PRO. Le cours Groovy & Gradle: JVM Automation and Build Engineering comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Sous-projets et configurations » ?

Définissez et configurez des sous-projets en comprenant leurs interactions au sein d’une construction multi-projets. Tu pratiques Groovy & Gradle: JVM Automation and Build Engineering avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Groovy & Gradle: JVM Automation and Build Engineering ?

Aucune expérience préalable n'est requise. Groovy & Gradle: JVM Automation and Build Engineering sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 2 sur 4.

Combien de temps prend la leçon « Sous-projets et configurations » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Groovy & Gradle: JVM Automation and Build Engineering ?

Oui. Chaque leçon Groovy & Gradle: JVM Automation and Build Engineering inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Structure de projet monorepo
  2. Sous-projets et configurations
  3. Dépendances entre projets
  4. Compilations composites et composition de compilations
← Retour à Groovy & Gradle: JVM Automation and Build Engineering