Question

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

Était-ce utile?

La solution

I just learned this five minutes ago. I wanted to use the clojure-csv library, here's what I did using Leiningen

  1. Make a new leiningen project

    lein new app csvtest

  2. 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"]]
    
  3. 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
    ...
    
  4. 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

  5. Run the project with lein run. My output was

    $ lein run  
    ([1 2 3 hello world])
    

I hope that helps!

Autres conseils

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

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top