0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · Lektion

Teilprojekte und Konfigurationen

Definieren und konfigurieren Sie Teilprojekte und verstehen Sie, wie diese innerhalb eines Multi-Projekt-Builds zusammenwirken.

Teilprojekte und Konfigurationen ist eine kostenlose Groovy & Gradle: JVM Automation and Build Engineering-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Groovy & Gradle: JVM Automation and Build Engineering-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Groovy & Gradle: JVM Automation and Build Engineering-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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!

Häufig gestellte Fragen

Ist die Lektion „Teilprojekte und Konfigurationen“ kostenlos?

Ja — der vollständige Text von „Teilprojekte und Konfigurationen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Groovy & Gradle: JVM Automation and Build Engineering-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Groovy & Gradle: JVM Automation and Build Engineering-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Teilprojekte und Konfigurationen“?

Definieren und konfigurieren Sie Teilprojekte und verstehen Sie, wie diese innerhalb eines Multi-Projekt-Builds zusammenwirken. Du übst Groovy & Gradle: JVM Automation and Build Engineering mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Groovy & Gradle: JVM Automation and Build Engineering zu starten?

Keine Vorkenntnisse erforderlich. Groovy & Gradle: JVM Automation and Build Engineering auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Teilprojekte und Konfigurationen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Groovy & Gradle: JVM Automation and Build Engineering-Lektion Code schreiben und ausführen?

Ja. Jede Groovy & Gradle: JVM Automation and Build Engineering-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Monorepo-Projektstruktur
  2. Teilprojekte und Konfigurationen
  3. Abhängigkeiten zwischen Projekten
  4. Composite Builds und Build-Komposition
← Zurück zu Groovy & Gradle: JVM Automation and Build Engineering