Groovy & Gradle: JVM Automation and Build Engineering · Lezione

Repository personalizzati e BOM

Configuri repository Maven o Ivy personalizzati e utilizzi i Bill of Materials (BOM) per mantenere coerenti le versioni delle dipendenze.

Lezione 3 di 411 passaggi

Repository personalizzati e BOM è una lezione Groovy & Gradle: JVM Automation and Build Engineering gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Groovy & Gradle: JVM Automation and Build Engineering, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Groovy & Gradle: JVM Automation and Build Engineering include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Beyond Maven Central

By default, Gradle looks for dependencies in Maven Central. But what if your dependencies aren't there?

Custom repositories allow you to fetch libraries from other locations, like:

  • Your company's private artifact server.
  • Specific public repositories (e.g., Google's Maven repo for Android).
  • Local file system directories.

Adding a Maven Repository

To add a custom Maven repository, you declare it in the repositories block of your build.gradle file.

Gradle will check repositories in the order they are declared, stopping at the first match.

repositories {
    mavenCentral()
    maven {
        url 'https://repo.spring.io/milestone'
    }
}

Example: Google Maven Repo

Many Android libraries are hosted on Google's Maven repository. You'd add it like this:

This is crucial for projects using Google-specific libraries.

repositories {
    google() // Shortcut for Google's Maven repo
    mavenCentral()
}

Understanding Ivy Repositories

While Maven is very common, Gradle also supports Ivy repositories. Ivy has a more flexible layout for artifacts.

You might encounter Ivy repositories in older projects or specific corporate environments.

repositories {
    ivy {
        url "http://repo.mycompany.com/ivy"
        layout "pattern", {
            artifact "[organization]/[module]/[revision]/[artifact](-[classifier])-[revision].[ext]"
        }
    }
}

Local Directory as Repo

For testing or internal use, you can even use a local directory as a repository. This is handy for sharing artifacts within a team without a dedicated server.

Just specify the path to your local folder.

repositories {
    flatDir {
        dirs 'libs' // Looks for JARs directly in the 'libs' folder
    }
    // Or a more structured Maven-like local repo
    maven {
        url uri('../my-local-maven-repo')
    }
}

Bill of Materials (BOMs)

A Bill of Materials (BOM) is a special Maven POM file that defines a curated list of dependency versions.

It helps manage transitive dependencies and ensures consistent versions across a multi-module project or when using a suite of related libraries.

Consistency with BOMs

BOMs solve a common problem: dependency version conflicts. If multiple libraries depend on different versions of the same transitive dependency, you can end up with unpredictable behavior.

With a BOM, you declare a single "source of truth" for versions, making your build more reliable.

Importing a BOM

To use a BOM in Gradle, you declare it as a dependency using the platform() or enforcedPlatform() function. This tells Gradle to use the versions specified in the BOM.

Notice how we don't specify versions for spring-core or spring-web; the BOM handles it!

dependencies {
    implementation platform('org.springframework.boot:spring-boot-dependencies:2.7.5')

    // These versions are now managed by the BOM
    implementation 'org.springframework:spring-core'
    implementation 'org.springframework:spring-web'
}

Platform vs. EnforcedPlatform

There are two ways to import a BOM:

  • platform(): Suggests versions. Other modules can override these versions if explicitly declared.
  • enforcedPlatform(): Strictly enforces versions. Any explicitly declared versions for dependencies in the BOM will be overridden by the BOM's version.

Use enforcedPlatform() for strong version consistency.

Check Your Knowledge

Let's test your understanding of custom repositories and BOMs!

Custom Repos & BOMs Recap

Great job! You've learned how to:

  • Configure custom Maven and Ivy repositories in Gradle.
  • Understand the importance of repository order.
  • Use local directories as repositories.
  • Leverage Bill of Materials (BOMs) for consistent dependency version management.
  • Differentiate between platform() and enforcedPlatform().

These techniques are vital for managing complex dependency landscapes in real-world projects!

Gratis per iniziare

Impara Groovy con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
12
Lezioni
48

Domande Frequenti

La lezione «Repository personalizzati e BOM» è gratuita?

Sì — il testo completo di «Repository personalizzati e BOM» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Groovy & Gradle: JVM Automation and Build Engineering, passa a CoddyKit PRO. Il corso Groovy & Gradle: JVM Automation and Build Engineering include 4 lezioni in totale.

Cosa imparerò in «Repository personalizzati e BOM»?

Configuri repository Maven o Ivy personalizzati e utilizzi i Bill of Materials (BOM) per mantenere coerenti le versioni delle dipendenze. Eserciti Groovy & Gradle: JVM Automation and Build Engineering con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Groovy & Gradle: JVM Automation and Build Engineering?

Non è richiesta alcuna esperienza precedente. Groovy & Gradle: JVM Automation and Build Engineering su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.

Quanto tempo richiede la lezione «Repository personalizzati e BOM»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Groovy & Gradle: JVM Automation and Build Engineering?

Sì. Ogni lezione Groovy & Gradle: JVM Automation and Build Engineering include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Dichiarazione delle dipendenze del progetto
  2. Risoluzione e caching delle dipendenze
  3. Repository personalizzati e BOM
  4. Risoluzione dei conflitti di versione e vincoli sulle dipendenze
← Torna a Groovy & Gradle: JVM Automation and Build Engineering