Clojure Functional Programming & JVM Backend Development · 课时

Leiningen 与 Deps.edn 基础

了解 Leiningen 和 Deps.edn 在创建项目、管理依赖和自动执行任务方面的基础知识。

第 1 / 4 课12 个步骤

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

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

Why Clojure Needs Build Tools

In Clojure, build tools are essential for automating tasks like managing dependencies, compiling code, running tests, and packaging your application. They simplify project setup and ensure consistency.

Without them, you'd manually download libraries, set up classpaths, and invoke the JVM, which quickly becomes cumbersome for any non-trivial project.

Leiningen: The Clojure Workhorse

Leiningen is a popular and mature build automation tool for Clojure projects. It's inspired by Maven and handles everything from creating new projects to deploying them.

Its primary configuration file is project.clj, a Clojure data structure itself, making it very flexible.

Creating a Leiningen Project

You can create a new Leiningen application project using the lein new command. This command quickly generates a standard directory structure with boilerplate code.

Run this command in your terminal:

lein new app my-lein-app

Inside project.clj

The project.clj file defines your project's metadata and configuration. Key elements include:

  • :dependencies: A list of external libraries your project needs.
  • :main: Specifies the main entry point for your application.
  • :version: Your project's current version.

It's written in Clojure syntax, allowing for powerful, dynamic configurations.

(defproject my-lein-app "0.1.0-SNAPSHOT"
  :description "A sample Leiningen project."
  :url "http://example.com/FIXME"
  :dependencies [[org.clojure/clojure "1.11.1"]]
  :main ^:skip-aot my-lein-app.core
  :target-path "target/%s")

First Run with Leiningen

After creating your project, Leiningen provides an easy way to run your application. The lein run command executes the main function specified in project.clj.

Here's the default core.clj content that lein new app generates. Try running it!

(ns my-lein-app.core
  (:gen-class))

(defn -main
  "The entry point for our Leiningen app."
  [& args]
  (println "Hello from Leiningen!"))

Deps.edn: The Official Tool

deps.edn is Clojure's official and more lightweight dependency management and build tool, introduced with Clojure 1.9. It focuses solely on managing dependencies and classpaths.

It uses EDN (Extensible Data Notation), a subset of Clojure, for its configuration, making it very readable and easy to parse.

Setting Up a Deps.edn Project

Unlike Leiningen, deps.edn doesn't have a new command to scaffold projects. You typically create the necessary files and directories manually.

A minimal project requires a deps.edn file in your project root and a source directory (e.g., src) for your code.

Inside deps.edn

The deps.edn file defines your project's dependencies and source paths. Key elements include:

  • :paths: A vector of directories where your source code is located.
  • :deps: A map of external libraries and their versions, typically using Maven coordinates.

It's simpler than project.clj, focusing on classpath construction.

{:paths ["src"]
 :deps {org.clojure/clojure {:mvn/version "1.11.1"}
        org.clojure/tools.cli {:mvn/version "1.0.206"}}}

First Run with Deps.edn

To run a deps.edn project, you use the clj command (the Clojure CLI tool). You specify the main namespace using -m.

Here's a simple core.clj file that can be run with clj -m my-deps-app.core. Try it out!

(ns my-deps-app.core
  (:gen-class))

(defn -main
  "The entry point for our deps.edn app."
  [& args]
  (println "Hello from deps.edn!"))

Leiningen vs. Deps.edn

Both Leiningen and deps.edn are excellent tools, but they serve slightly different philosophies:

  • Leiningen: An all-in-one project manager with extensive plugin support for various tasks (testing, deployment, etc.).
  • deps.edn: A simpler, more focused tool for dependency and classpath management, often used with other CLI tools for specific tasks.

Many modern Clojure projects use deps.edn for its simplicity and directness, sometimes alongside Leiningen.

Build Tool Knowledge Check

Consider the core purpose and configuration approach of Leiningen and deps.edn.

Recap: Leiningen & Deps.edn Basics

We've explored the fundamentals of Clojure's two primary build tools:

  • Leiningen: A full-featured tool using project.clj for configuration, offering project creation and task automation.
  • deps.edn: A lightweight, official tool using deps.edn for classpath and dependency management, typically used with the clj CLI.

Both are crucial for managing Clojure projects efficiently, allowing you to choose based on your project's needs.

免费开始

用 AI 导师学习 Clojure — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「Leiningen 与 Deps.edn 基础」课时是免费的吗?

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

「Leiningen 与 Deps.edn 基础」这节课中我会学到什么?

了解 Leiningen 和 Deps.edn 在创建项目、管理依赖和自动执行任务方面的基础知识。 你通过在浏览器中直接运行的动手代码来练习 Clojure Functional Programming & JVM Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

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

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

「Leiningen 与 Deps.edn 基础」课时需要多长时间?

大多数 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