exclude certain clj namespaces from compilation in leiningen
Asked Answered
E

3

9

I have a project that works fine using lein run. Now I want to compile it into a standalone jar using lein uberjar. However, there are a couple of source files in my src/projectname/ directory called e.g. playground.clj and stats.clj that I use for experimenting with emacs & the repl, but that I don't want to compile for the final project.

With something like make, I would specify all files that should be compiled. With clojure/leiningen, it seems, all files are compiled by default - how can I exclude files? I haven't found anything in the leiningen docs.

I am currently using :aot :all. Is this the place to change something? Again, I couldn't find detailed documentation on this.

UPDATE:

The suggestions so far haven't worked. What has worked, however, is to include all desired namespaces instead of excluding the ones that should not be compiled. E.g.:

(defproject myproject "version"
  ;; ...
  :profiles {:uberjar {:aot [myproject.data
                             myproject.db
                             myproject.util]}})
Emblematize answered 2/12, 2014 at 10:10 Comment(0)
M
3

Have a look at leiningen's sample project.clj, which describes how to use :jar-exclusions or :uberjar-exclusions to exclude arbitrary paths when creating jars (resp. uberjars).

  ;; Files with names matching any of these patterns will be excluded from jars.
  :jar-exclusions [#"(?:^|/).svn/"]
  ;; Files with names matching any of these patterns will included in the jar
  ;; even if they'd be skipped otherwise.
  :jar-inclusions [#"^\.ebextensions"]
  ;; Same as :jar-exclusions, but for uberjars.
  :uberjar-exclusions [#"META-INF/DUMMY.SF"]
Mushy answered 3/12, 2014 at 21:21 Comment(1)
tried :uberjar-exclusions [#".*stats.clj" #".*playground.clj"], didn't change anythingEmblematize
H
1

Old question, but I think I found the answer for those coming after me.

I found the answer in the link to the sample leiningen project from @amalloy's answer, except instead of :jar-exclusions I use source-paths, here.

The idea is to create two separate source directories, one for stuff you don't care to spread around and one for stuff you do:

dev-src/<your-project>/playground.clj
dev-src/<your-project>/stats.clj
src/<your-project>/<everything-else>

Then, in your project.clj, include src in source-paths normally, and include emacs-src in a special profile where your want it visible, say the usual :dev profile:

{
   ;; ...
   :source-paths ["src"]
   :profiles {
      :dev {
         :source-paths ["src" "dev-src"]
      }
   }
}

That way when you're messing around on your machine those files will be in the jar, and when you deploy to clojars or compile with uberjar they will not be included in the jar, nor compiled.

Hesperian answered 22/6, 2020 at 3:49 Comment(0)
C
0

Try this (ns ^:skip-aot my-ns)

You can also do

(ns ^{:skip-aot true} my-ns
    (require [...]))

Source

Cockatrice answered 2/12, 2014 at 10:11 Comment(10)
Didn't work for me. Does this have anything to do with it? -> From the same source file: "^:skip-aot will not disable AOT compilation of :main when :aot is :all or contains the main class." I.e. I'd then need to replace :all by a vector of all files to include?Emblematize
That's correct. You can move code from the main ns to another and add ^:skip-aot to the namespaceCockatrice
Well, my main ns is called myproject.core. I added ^:skip-aot to the ns directive in myproject.stats but when I run lein uberjar it still wants to compile stats. I could only resolve the issue by renaming stats.clj to stats.bak.Emblematize
I assume that your main ns, myproject.core, depends on code in myproject.stats. Is there any reason why you can't define which namespaces need :aot instead of using :all?Cockatrice
I checked, of course. There is no such dependency. stats is not referenced anywhere.Emblematize
What version of lein are you using?Cockatrice
Leiningen 2.3.4 on Java 1.7.0_65 OpenJDK 64-Bit Server VMEmblematize
Can you try upgrading to Lein 2.5 and then check if the issue still occurs?Cockatrice
As mentioned in the linked-to project.clj, ^:skip-aot is only relevant for the namespace listed as :main in project.clj: it doesn't do anything elsewhere, because other namespaces are either (a) not AOT compiled to begin with, or (b) AOT compiled iff an AOT-compiled :main namespace depends on them.Mushy
:aot :all seems to cancel the ^:skip-aot flag. Leiningen 2.5 doesn't change this.Emblematize

© 2022 - 2024 — McMap. All rights reserved.