0Pricing
Clojure Functional Programming & JVM Backend Development · 课时

管理依赖与插件

学习如何添加和管理外部库,并利用插件扩展构建流程。

管理依赖与插件 是 CoddyKit 上的免费 Clojure Functional Programming & JVM Backend Development 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Clojure Functional Programming & JVM Backend Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Clojure Functional Programming & JVM Backend Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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!

常见问题解答

「管理依赖与插件」课时是免费的吗?

是的 — 「管理依赖与插件」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Clojure Functional Programming & JVM Backend Development 课程的其余内容,请升级到 CoddyKit PRO。 Clojure Functional Programming & JVM Backend Development 课程共包含 4 节课。

「管理依赖与插件」这节课中我会学到什么?

学习如何添加和管理外部库,并利用插件扩展构建流程。 你通过在浏览器中直接运行的动手代码来练习 Clojure Functional Programming & JVM Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Clojure Functional Programming & JVM Backend Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Clojure Functional Programming & JVM Backend Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「管理依赖与插件」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Clojure Functional Programming & JVM Backend Development 课中编写并运行代码吗?

能。每节 Clojure Functional Programming & JVM Backend Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Leiningen 与 Deps.edn 基础
  2. 管理依赖与插件
  3. 构建、测试与部署
  4. 管理配置档案、别名与环境
← 返回 Clojure Functional Programming & JVM Backend Development