Groovy & Gradle: JVM Automation and Build Engineering · Lekcja

Niestandardowe repozytoria i BOM-y

Skonfiguruj niestandardowe repozytoria Maven lub Ivy i używaj Bill of Materials (BOM), aby zapewnić spójne wersje zależności.

Lekcja 3 z 411 kroki

Niestandardowe repozytoria i BOM-y to bezpłatna lekcja Groovy & Gradle: JVM Automation and Build Engineering na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Groovy & Gradle: JVM Automation and Build Engineering, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Groovy & Gradle: JVM Automation and Build Engineering zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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!

Bezpłatny start

Ucz się Groovy dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
12
Lekcje
48

Często zadawane pytania

Czy lekcja „Niestandardowe repozytoria i BOM-y” jest bezpłatna?

Tak — pełny tekst „Niestandardowe repozytoria i BOM-y” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Groovy & Gradle: JVM Automation and Build Engineering, przejdź na CoddyKit PRO. Kurs Groovy & Gradle: JVM Automation and Build Engineering zawiera 4 lekcji w sumie.

Co nauczysz się w „Niestandardowe repozytoria i BOM-y”?

Skonfiguruj niestandardowe repozytoria Maven lub Ivy i używaj Bill of Materials (BOM), aby zapewnić spójne wersje zależności. Ćwiczysz Groovy & Gradle: JVM Automation and Build Engineering z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Groovy & Gradle: JVM Automation and Build Engineering?

Nie wymagamy żadnego doświadczenia. Groovy & Gradle: JVM Automation and Build Engineering w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Niestandardowe repozytoria i BOM-y”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Groovy & Gradle: JVM Automation and Build Engineering?

Tak. Każda lekcja Groovy & Gradle: JVM Automation and Build Engineering zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Deklarowanie zależności projektu
  2. Rozwiązywanie zależności i buforowanie
  3. Niestandardowe repozytoria i BOM-y
  4. Rozwiązywanie konfliktów wersji i ograniczenia zależności
← Powrót do Groovy & Gradle: JVM Automation and Build Engineering