0Pricing
Clojure Functional Programming & JVM Backend Development · Lezione

Gestione delle dipendenze e dei plugin

Impari ad aggiungere e gestire librerie esterne e a utilizzare plugin per estendere il processo di build

Gestione delle dipendenze e dei plugin è una lezione Clojure Functional Programming & JVM Backend Development gratuita su CoddyKit. Questa è la lezione 2 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 Clojure Functional Programming & JVM Backend Development, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Clojure Functional Programming & JVM Backend Development include 4 lezioni in totale.

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

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.clj and Deps.edn's deps.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!

Domande Frequenti

La lezione «Gestione delle dipendenze e dei plugin» è gratuita?

Sì — il testo completo di «Gestione delle dipendenze e dei plugin» è 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 Clojure Functional Programming & JVM Backend Development, passa a CoddyKit PRO. Il corso Clojure Functional Programming & JVM Backend Development include 4 lezioni in totale.

Cosa imparerò in «Gestione delle dipendenze e dei plugin»?

Impari ad aggiungere e gestire librerie esterne e a utilizzare plugin per estendere il processo di build Eserciti Clojure Functional Programming & JVM Backend Development 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 Clojure Functional Programming & JVM Backend Development?

Non è richiesta alcuna esperienza precedente. Clojure Functional Programming & JVM Backend Development su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.

Quanto tempo richiede la lezione «Gestione delle dipendenze e dei plugin»?

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 Clojure Functional Programming & JVM Backend Development?

Sì. Ogni lezione Clojure Functional Programming & JVM Backend Development 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. Fondamenti di Leiningen e Deps.edn
  2. Gestione delle dipendenze e dei plugin
  3. Build, test e distribuzione
  4. Gestire profili, alias e ambienti
← Torna a Clojure Functional Programming & JVM Backend Development