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

Dépendances entre projets

Gérez les dépendances entre sous-projets et garantissez l’ordre correct de construction ainsi que la résolution des artefacts.

Dépendances entre projets est une leçon Groovy & Gradle: JVM Automation and Build Engineering gratuite sur CoddyKit. Ceci est la leçon 3 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.

Introduction to Project Dependencies

In a multi-project Gradle build, different subprojects often need to work together. One subproject might produce a library that another subproject uses.

This is where inter-project dependencies come in! They define how subprojects rely on each other, ensuring the correct build order and artifact sharing.

  • Inter-project dependencies: How one subproject uses code or outputs from another subproject.
  • Crucial for modular applications.

Declaring a Basic Dependency

Declaring that one subproject depends on another is straightforward in Gradle. You use the project() function within your dependencies block.

For example, if your app subproject needs code from your lib subproject, you'd add this to app/build.gradle:

// app/build.gradle
dependencies {
    implementation project(':lib')
}

The :lib part refers to the path of the lib subproject relative to the root.

Setting Up Our Example

Let's imagine a simple multi-project setup. We'll have a root project, an app subproject, and a lib subproject.

Your settings.gradle would look like this:

// settings.gradle
rootProject.name = 'my-multi-project'
include 'app', 'lib'

This tells Gradle about our two subprojects, app and lib.

`implementation` vs. `api`

When declaring inter-project dependencies, you'll typically use configurations like implementation or api.

  • implementation: The most common choice. The dependency is used internally by the subproject and is not exposed to consumers of that subproject.
  • api: Exposes the dependency to consumers. If app depends on lib via api, and otherApp depends on app, then otherApp will also see lib's API.

For internal use between subprojects, implementation is generally preferred for better encapsulation and faster builds.

The Library Subproject (`lib`)

First, let's create a simple class in our lib subproject that the app subproject will use. This class will provide a basic utility.

Inside lib/src/main/groovy/com/coddykit/LibUtils.groovy:

package com.coddykit

class LibUtils {
    static String getGreeting() {
        return "Hello from LibUtils!"
    }
}

App Subproject Using `lib`'s Code

Now, let's make our app subproject use the LibUtils class from our lib subproject. Remember, we declared implementation project(':lib') in app/build.gradle.

Try running this example:

package com.coddykit

import com.coddykit.LibUtils

class AppMain {
    static void main(String[] args) {
        String message = LibUtils.getGreeting()
        System.out.println(message)
    }
}

Automatic Build Order

One of the biggest advantages of declaring inter-project dependencies is that Gradle automatically figures out the correct build order.

  • If app depends on lib, Gradle ensures that lib is compiled and its artifacts are available before app starts its compilation.
  • This saves you from manually managing build sequences, especially in complex multi-project setups.

When you run gradle build from the root, Gradle will build lib first, then app.

Understanding Transitive Effects

What if your lib subproject itself depends on another subproject, say common?

// lib/build.gradle
dependencies {
    implementation project(':common')
}

// app/build.gradle
dependencies {
    implementation project(':lib')
}

If app depends on lib, and lib depends on common, Gradle will ensure that common is also built and its artifacts are available transitively to app's classpath for compilation, but not necessarily for its API if implementation is used for lib.

Maintain Clean Dependencies

To keep your multi-project builds manageable and efficient, follow these best practices:

  • Minimal Dependencies: Only declare dependencies on subprojects you truly need.
  • Use implementation: Prefer implementation over api for internal subproject dependencies to improve encapsulation and reduce rebuilds.
  • Clear Boundaries: Design your subprojects with clear responsibilities to avoid circular dependencies.
  • Avoid Deep Paths: Keep subproject paths concise (e.g., :module:submodule is fine, but avoid excessively long paths).

Dependency Declaration Check

You have a multi-project build with a web subproject and a core subproject. The web subproject needs to use classes from the core subproject for its internal logic, but not expose core's API to any further consumers of web.

Inter-Project Dependencies Recap

Great job! In this lesson, you learned how to manage dependencies between subprojects in a Gradle multi-project build.

  • We covered how to declare dependencies using project(':subproject').
  • We discussed the importance of implementation for internal use.
  • You saw how Gradle automatically handles build order.
  • We explored best practices for clean dependency management.

Mastering inter-project dependencies is key to building large, modular applications efficiently with Gradle!

Questions Fréquemment Posées

La leçon « Dépendances entre projets » est-elle gratuite ?

Oui — le texte complet de « Dépendances entre projets » 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 « Dépendances entre projets » ?

Gérez les dépendances entre sous-projets et garantissez l’ordre correct de construction ainsi que la résolution des artefacts. 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 3 sur 4.

Combien de temps prend la leçon « Dépendances entre projets » ?

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