Reputation: 2037
Let's say I find a cool clojure library like https://github.com/clojurewerkz/buffy
Now I want to use it. And it only lives on github.
How do I do this? I would love a full start to finish hello world example.
I've read about compiling it as a jar and using that, or using :dependencies in my project.clj but so far no examples have been complete, and I'm new.
For example in python I'd git clone
the repo into the root of my working tree and any file could just say import buffy
Upvotes: 3
Views: 513
Reputation: 537
I just learned this five minutes ago. I wanted to use the clojure-csv library, here's what I did using Leiningen
Make a new leiningen project
lein new app csvtest
Now I have a folder called csvtest/
. In the csvtest/project.clj
file, edit the dependencies section to add the clojure-csv github path and a version. I'm not quite sure how the versioning works to be honest:
:dependencies [[org.clojure/clojure "1.5.1"]
[clojure-csv/clojure-csv "2.0.1"]]
Now run lein deps
to automagically download any unresolved dependencies for your project
$ lein deps
...
Retrieving clojure-csv/clojure-csv/2.0.1/clojure-csv-2.0.1.pom from clojars
...
Now edit csvtest/src/csvtest/core.clj
. Make it look like this:
Edit: (Based on sveri's comment I changed :use closjure-csv.core
to the :require
line)
(ns csvtest.core
(:gen-class)
(:require [clojure-csv.core :refer [parse-csv]]))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println (parse-csv "1,2,3,hello,world")))
You have to add the (:require [clojure-csv.core :refer [parse-csv]])
line to your ns
, and in main it calls parse-csv
, one of the functions provided by the library. From reading, it looks like :use
is depreciated in favor of :require
.
Note: For anyone coming from python (like me), it looks like doing :use clojure-csv.core
is akin to from closure_csv import *
, which is bad practice in python as well. And (:require [clojure-csv.core :refer [parse-csv]])
is like from clojure_csv import parse_csv
Run the project with lein run
. My output was
$ lein run
([1 2 3 hello world])
I hope that helps!
Upvotes: 4
Reputation: 4748
Using lein (which is Clojure's build tool) you have a project.clj file. if you do lein new project-name
you get a new project named "project-name", with a project.clj file in it. look at that file, you will see a :dependencies entry in it as in this example:
:dependencies [[org.clojure/clojure "1.5.1"]]
all you must do to include buffy, is look at their github site, and find out which version to use, then you add it to the :dependecies list:
:dependencies [[org.clojure/clojure "1.5.1"]
[clojurewerkz/buffy "1.0.0-beta3"]]
now you can simply do lein deps
which will download the dependencies and once you written your code lein run
to actually run it.
read a little about lein and check out its sample project.clj file to get a better understanding of how you should use clojure.
Upvotes: 1
Reputation: 188
You can download/use it from Clojars.
Clojars link: https://clojars.org/clojurewerkz/buffy
To use it with Leiningen, add this to your dependencies(on project.clj file):
[clojurewerkz/buffy "1.0.0-beta4"]
After that, you can run lein deps
from your project root folder in order to download dependencies.
To use dependencies directly from Github repos, you can check out this: https://github.com/tobyhede/lein-git-deps
Upvotes: 1