พื้นฐาน Leiningen และ Deps.edn
ทำความเข้าใจพื้นฐานของ Leiningen และ Deps.edn สำหรับการสร้างโครงการ การจัดการสิ่งที่ต้องพึ่งพา และการทำงานอัตโนมัติของงานต่าง ๆ
พื้นฐาน Leiningen และ Deps.edn เป็นบทเรียน Clojure Functional Programming & JVM Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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-appInside 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.cljfor configuration, offering project creation and task automation. deps.edn: A lightweight, official tool usingdeps.ednfor classpath and dependency management, typically used with thecljCLI.
Both are crucial for managing Clojure projects efficiently, allowing you to choose based on your project's needs.
คำถามที่พบบ่อย
บทเรียน “พื้นฐาน Leiningen และ Deps.edn” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “พื้นฐาน Leiningen และ Deps.edn” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส 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 ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Clojure Functional Programming & JVM Backend Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Clojure Functional Programming & JVM Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “พื้นฐาน Leiningen และ Deps.edn” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Clojure Functional Programming & JVM Backend Development นี้ได้ไหม
ได้ บทเรียน Clojure Functional Programming & JVM Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พื้นฐาน Leiningen และ Deps.edn
- การจัดการสิ่งที่ต้องพึ่งพาและปลั๊กอิน
- การสร้าง การทดสอบ และการนำไปใช้งาน
- การจัดการโปรไฟล์ นามแฝง และสภาพแวดล้อม