I have a lein project called foo. Foo has a resources directory in which I have another lein project called bar, with its own project.clj.
foo's project.clj:
(defproject foo "0.1.0"
:description "Instaboi's drone autonomy software"
:dependencies [[org.clojure/clojure "1.10.0"]
[nrepl "0.8.0"]
]
:repl-options {:init-ns foo.core}
:plugins [[cider/cider-nrepl "0.25.3"]
[lein-environ "1.2.0"]]
:source-paths ["src" "resources/bar/src"]
)
Notice that I add a source path "resources/bar/src", so that I can use bar's namespaces in foo.
bar's project.clj:
(defproject bar "1.47-SNAPSHOT"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:profiles {:dev {:dependencies [[criterium "0.4.5"]]}}
:java-source-paths ["java"]
:plugins [[lein-tools-deps "0.4.5"]]
:middleware [lein-tools-deps.plugin/resolve-dependencies-with-deps-edn]
:lein-tools-deps/config {:config-files [:install :user :project]})
Notice that bar uses a middleware to resolve-dependencies-with-deps-edn, and so there's a deps.edn in bar's root folder:
{
:paths ["src"]
:deps {
org.clojure/clojure {:mvn/version "1.10.1"}
camel-snake-kebab {:mvn/version "0.4.0"}
org.clojure/data.json {:mvn/version "1.0.0"}
}
}
The problem is that these dependencies are not loaded, because they aren't present in foo's dependencies. This causes an error when bar's modules are used in foo. How to make sure that the bar project is also loaded when a repl is run or a jar is created in foo's root?
I would build/install bar separately. I believe you can use lein profiles to achieve this from within Foo's project.clj. This way you can later add it as a dependency in Foo.
I'm suggesting this since you seem to only need access to bar's namespaces inside Foo and there's no clear reason to have them coupled.
Related
Despite adding the desired dependency to my project.clj file,
(defproject word-tree "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.10.1"]
[org.clojure/clojurescript "1.10.520"]
[clojure-opennlp "0.5.0"] ; <----------------------here
[reagent "0.8.1"]]
...
I am unable to access the functions that the dependency provides.
After referencing the namespace in one of my files,
(ns word-tree.suffix-tree
(:require [clojure.string :as str]
[opennlp.nlp :as nlp])) ; <-----this is the namespace of the dependency
I get this error:
No such namespace: opennlp.nlp, could not locate opennlp/nlp.cljs, opennlp/nlp.cljc, or JavaScript source providing "opennlp.nlp"
The weirdest part is that when I run lein deps :tree the dependency shows up!
$ lein deps :tree
...
[cider/piggieback "0.4.1" :scope "test"]
[cljfmt "0.5.7"]
[rewrite-clj "0.5.2"]
[rewrite-cljs "0.4.3"]
[clojure-complete "0.2.5" :exclusions [[org.clojure/clojure]]]
[clojure-opennlp "0.5.0"] <------------------------------------------------here!!!
[instaparse "1.4.9"]
[org.apache.opennlp/opennlp-tools "1.9.0"]
[figwheel-sidecar "0.5.19" :scope "test"]
...
For my project I really want to use this dependency but it's not working. Any advice would be most welcome. Thanks.
It seems to me the problem is that the library is for Clojure on the JVM only, and you are trying to use it on a ClojureScript project.
No such namespace: opennlp.nlp, could not locate opennlp/nlp.cljs, opennlp/nlp.cljc, or JavaScript source providing "opennlp.nlp"
The compiler tried to look for ClojureScript code (extension .cljs) or compatible with both Clojure and ClojureScript (extension .cljc) or plain JavaScript, but found none.
I'm trying to set up a development version and a production version of my application using leiningen via the project.clj.
How can I have both? Because I have to comment out the :main part of my project.clj in order to get access to the development version when I use lein repl.
So I am using stuartsierra/reloaded leiningen template which has a nice development environment.
It comes with no :main key and when I added one that's when I stopped getting the development version of my project.
Instead of seeing
user=>
When I type lein repl I end up seeing my main, which is in the jaribu namespace
io.wakamau.jaribu=> ;; my main
The solution that seems to work is to comment out the :main part of the project.clj.
(defproject io.wakamau/jaribu "0.1.0-SNAPSHOT"
:description "trying out pedestal and component"
:url "https://github.com/kevinmungai/jaribu"
:license {:name "TODO: Choose a license"
:url "http://choosealicense.com/"}
:dependencies [[org.clojure/clojure "1.10.0"]
[com.stuartsierra/component "0.3.2"]]
:profiles {:dev
{:dependencies [[org.clojure/tools.namespace 0.2.11"]
[com.stuartsierra/component.repl "0.2.0"]]
:source-paths ["dev"]}
:uberjar {:aot [io.wakamau.jaribu]}}
:main ^{:skip-aot true} io.wakamau.jaribu
:min-lein-version "2.0.0")
trying out
lein repl
results in:
io.wakamau.jaribu=>
when the :main is commented out:
(defproject io.wakamau/jaribu "0.1.0-SNAPSHOT"
...
;; :main ^{:skip-aot true} io.wakamau.jaribu
:min-lein-version "2.0.0")
the result is:
user=>
I have to admit that I don't know much about using leiningen.
You can specify :repl-options {:init-ns io.wakamau.jaribu=>} in project.clj. You can also specify constants for different environments in profiles.clj, e.g., :dev {:main io.wakamau.jaribu}. Also see How do I start the REPL in a user defined namespace?.
I've gotten leiningen to find a library I have in a corporate Artifactory repository, and it seems to download it just fine, but when I open try to require it, running the code with lein run comes back with a FileNotFoundException.
My project file looks like this:
(defproject clj-test "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"]
[com.ourgroup/library "1.0.0"]]
:repositories [["releases" {:url "https://url-to-our-repo"
:username "username"
:password "password"}]]
:main ^:skip-aot clj-test.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
My single code file looks like this:
(ns clj-test.core
(:gen-class)
(:require [library :as lib]))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "And now for something completely different..."))
You need to import a class. To get the name of the class take a look inside the jar (or at the Java source code) and find the Java package and class that make up the fully qualified class name that you need to import.
I've edited this. My earlier mistake has been corrected with the comment: "You require namespaces; you import classes. Requiring a class is no good". So for Java interop you need to import classes, the same as you would be doing if you were writing a Java source file.
I was trying to pick up Clojure again, but am stumbling right at the beginning. I downloaded lein, and copied the following project.clj and a hello.clj to be absolutely sure that I have a minimal working example.
project.clj:
(defproject hello "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.7.0"]]
:uberjar {:aot :all}
:main hello.core
)
hello.clj:
(ns hello.core
(:gen-class)
)
(defn -main
"This should be pretty simple."
[]
(println "Hello, World!"))
When I run './lein uberjar' I get these warnings:
Warning: specified :main without including it in :aot.
Implicit AOT of :main will be removed in Leiningen 3.0.0.
If you only need AOT for your uberjar, consider adding :aot :all into your
:uberjar profile instead.
Warning: The Main-Class specified does not exist within the jar. It may not be executable as expected. A gen-class directive may be missing in the namespace which contains the main method.
Created .../target/hello-0.1.0-SNAPSHOT.jar
Created .../target/hello-0.1.0-SNAPSHOT-standalone.jar
Trying to run this either with ./lein run or with java -jar ./target/hello-0.1.0-SNAPSHOT-standalone.jar results in exceptions:
Can't find 'hello.core' as .class or .clj for lein run: please check the spelling.
Exception in thread "main" java.io.FileNotFoundException: Could not locate hello/core__init.class or hello/core.clj on classpath., compiling:(/private/var/folders/28/bk6d4xj123b0xvsvk91_1jg80009rn/T/form-init1007755193774766954.clj:1:125)
So what is my problem here?
:uberjar {:aot :all} -> :profiles {:uberjar {:aot :all}}
And move hello.clj into ./src/hello directory and rename it to core.clj
Okay. I'm trying to muck about with twitter4j inside a Clojure REPL, provided by Leiningen. I've specified twitter4j as a build dependency:
(defproject testproject "0.1.0-SNAPSHOT"
:description "Tryin stuff"
:repositories {
"twitter4j" "http://twitter4j.org/maven2"
}
:dependencies [[org.clojure/clojure "1.5.1"]
[compojure "1.1.6"]
[org.twitter4j/twitter4j-core "3.0.5"]
[org.twitter4j/twitter4j-stream "3.0.5"]]
:plugins [[lein-ring "0.8.8"]]
:ring {:handler testproject.core/app}
:profiles {:dev
{:dependencies [[javax.servlet/servlet-api "2.5"]
[ring-mock "0.1.5"]]}})
So far, so good. lein deps downloads everything without complaint into the default repo in ~/.m2. Awesome. I fire up the REPL, and I get this and only this:
user=> (import '(org.twitter4j.conf ConfigurationBuilder))
ClassNotFoundException org.twitter4j.conf.ConfigurationBuilder java.net.URLClassLoader$1.run (URLClassLoader.java:202)
The twitter4j jars are all present and accounted for, in ~/.m2/org/twitter4j/twitter4j-core/3.0.5/. Is there... something I don't get about importing Java classes? Some extra config I need to provide?
Try this (the proper package name):
user=> (import '(twitter4j.conf ConfigurationBuilder))