0Pricing
Clojure Functional Programming & JVM Backend Development · บทเรียน

การจัดการสิ่งที่ต้องพึ่งพาและปลั๊กอิน

เรียนรู้การเพิ่มและจัดการไลบรารีภายนอก รวมถึงการใช้ปลั๊กอินเพื่อขยายกระบวนการสร้าง

การจัดการสิ่งที่ต้องพึ่งพาและปลั๊กอิน เป็นบทเรียน Clojure Functional Programming & JVM Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Clojure Functional Programming & JVM Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Clojure Functional Programming & JVM Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการสิ่งที่ต้องพึ่งพาและปลั๊กอิน”

เรียนรู้การเพิ่มและจัดการไลบรารีภายนอก รวมถึงการใช้ปลั๊กอินเพื่อขยายกระบวนการสร้าง คุณปฏิบัติ Clojure Functional Programming & JVM Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Clojure Functional Programming & JVM Backend Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Clojure Functional Programming & JVM Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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