Zarządzanie zależnościami i pluginami
Naucz się dodawać i zarządzać zewnętrznymi bibliotekami oraz używać pluginów do rozszerzania procesu budowania.
Zarządzanie zależnościami i pluginami to bezpłatna lekcja Clojure Functional Programming & JVM Backend Development na CoddyKit. To lekcja 2 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 Clojure Functional Programming & JVM Backend Development, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Clojure Functional Programming & JVM Backend Development zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Libraries & Build Process
In any real-world project, you'll need external libraries to add functionality without writing everything from scratch. These are also called dependencies.
Build tools like Leiningen and Deps.edn help you manage these libraries, making it easy to add, update, and remove them automatically.
Leiningen: Adding Dependencies
With Leiningen, you declare dependencies in your project.clj file. They go into the :dependencies vector, which is a list of libraries your project needs.
Each dependency is a vector itself, containing the library's group ID, artifact ID, and version, for example:
[group-id/artifact-id "version"]
Leiningen: Using a Library
To use an external library like tick (a simple time library), you first add it to your project.clj file under the :dependencies key:
:dependencies [[org.clojure/clojure "1.11.1"]
[tick "0.5.0-RC1"]]
Once added, you can require the library in your namespace and use its functions. Try running this example:
(ns my-app.core
(:require [tick.core :as t]))
(defn -main
"Prints the current time using the tick library."
[& args]
(println "The current time is:" (t/now)))Deps.edn: Adding Dependencies
With deps.edn, dependencies are declared in the :deps map. This file is typically found at the root of your project.
Each entry in :deps maps a library name to a map containing its source, usually :mvn/version for Maven Central libraries:
{library-name {:mvn/version "version"}}
Deps.edn: Using a Library
To use the same tick library with deps.edn, you'd add it to your deps.edn file like this:
{:paths ["src"]
:deps {tick/tick {:mvn/version "0.5.0-RC1"}}}
With this in place, your code (e.g., in src/my_app/core.clj) can require and use the library. Try running it:
(ns my-app.core
(:require [tick.core :as t]))
(defn -main
"Prints the current time using the tick library."
[& args]
(println "The current time is:" (t/now)))Understanding Transitive Deps
When you add a library, it often relies on other libraries itself to function correctly. These are called transitive dependencies.
Your build tool automatically fetches these too. For example, the tick library might depend on clojure.java.time, which will also be downloaded.
This saves you from manually tracking every single required library for your project.
Excluding Transitive Deps
Sometimes, you might need to exclude a transitive dependency. This is usually done to avoid version conflicts with other libraries or to reduce your project's overall size.
In Leiningen, you can use the :exclusions key within a dependency vector. For example, if you wanted to exclude clojure.java.time from tick:
(defproject my-app "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.11.1"]
[tick "0.5.0-RC1" :exclusions [clojure.java-time]]]
:main ^:skip-aot my-app.core
:target-path "target/%s")Build Tool Plugins
Plugins extend the functionality of your build tool itself. They can add new tasks, automate processes, or integrate with other development tools.
Common uses include code formatting, static analysis, deployment, or running specific test suites.
Plugins are distinct from regular dependencies; they operate on the project's build process, not just provide libraries for your application code.
Leiningen: Using Plugins
Leiningen supports plugins defined in your project.clj under the :plugins key. Let's look at lein-ancient, which helps check for outdated dependencies.
You'd add [lein-ancient "0.6.15"] to your :plugins vector. Then, from your terminal, you can run lein ancient check to see if your libraries are up to date.
(defproject my-app "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.11.1"]]
:plugins [[lein-ancient "0.6.15"]]
:main ^:skip-aot my-app.core
:target-path "target/%s")Dependency Management Check
You've learned about managing dependencies and plugins in Clojure projects. Let's test your understanding.
Recap: Dependencies & Plugins
Great job! In this lesson, we covered:
- How to add external libraries (dependencies) using both Leiningen's
project.cljand Deps.edn'sdeps.edn. - The concept of transitive dependencies and how they are automatically managed.
- How to exclude specific transitive dependencies in Leiningen.
- The role of plugins in extending build tool functionality, distinct from regular libraries.
Mastering dependency and plugin management is crucial for maintaining healthy, efficient Clojure projects. Keep exploring new libraries and tools!
Często zadawane pytania
Czy lekcja „Zarządzanie zależnościami i pluginami” jest bezpłatna?
Tak — pełny tekst „Zarządzanie zależnościami i pluginami” 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 Clojure Functional Programming & JVM Backend Development, przejdź na CoddyKit PRO. Kurs Clojure Functional Programming & JVM Backend Development zawiera 4 lekcji w sumie.
Co nauczysz się w „Zarządzanie zależnościami i pluginami”?
Naucz się dodawać i zarządzać zewnętrznymi bibliotekami oraz używać pluginów do rozszerzania procesu budowania. Ćwiczysz Clojure Functional Programming & JVM Backend Development 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ąć Clojure Functional Programming & JVM Backend Development?
Nie wymagamy żadnego doświadczenia. Clojure Functional Programming & JVM Backend Development 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 2 z 4.
Ile czasu zajmuje lekcja „Zarządzanie zależnościami i pluginami”?
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 Clojure Functional Programming & JVM Backend Development?
Tak. Każda lekcja Clojure Functional Programming & JVM Backend Development 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
- Podstawy Leiningen i Deps.edn
- Zarządzanie zależnościami i pluginami
- Budowanie, testowanie i wdrażanie
- Zarządzanie profilami, aliasami i środowiskami