Exclude some dependencies with lein uberjar

2,798 views
Skip to first unread message

Warren Lynn

unread,
Jun 12, 2012, 12:59:13 PM6/12/12
to Clojure
Hi,

As titled, I need to exclude certain dependencies from my final .jar
file (because those dependencies will already be there in the final
target Java environment). How can I do that? Thank you.

PS: I am using Leinengen 2

Phil Hagelberg

unread,
Jun 12, 2012, 1:02:49 PM6/12/12
to clo...@googlegroups.com
On Tue, Jun 12, 2012 at 9:59 AM, Warren Lynn <wrn....@gmail.com> wrote:
> As titled, I need to exclude certain dependencies from my final .jar
> file (because those dependencies will already be there in the final
> target Java environment). How can I do that? Thank you.

The uberjar task is designed to create jars meant for standalone
distribution. If you're deploying them to some other environment then
you might want a different plugin or something.

-Phil

Jacek Laskowski

unread,
Jun 12, 2012, 2:37:35 PM6/12/12
to clo...@googlegroups.com
On Tue, Jun 12, 2012 at 6:59 PM, Warren Lynn <wrn....@gmail.com> wrote:

> As titled, I need to exclude certain dependencies from my final .jar
> file (because those dependencies will already be there in the final
> target Java environment). How can I do that? Thank you.

I'm trying to figure out where you're heading with the requirement (I
wish I had stated it more politely :)).

As the name says uberjar is what you should be able to run with no
worries about the classpath - all deps are included in the final jar.
Since you asked to exclude some deps, you likely run the final jar in
a kind of managed environment. Why are there some deps not the others?
What drives the exclusion? Wouldn't lein jar alone be enough? Why?
Just curious and try to understand what you've already done :)

Jacek

--
Jacek Laskowski
Functional languages (Clojure), Java EE, and IBM WebSphere -
http://blog.japila.pl
"Never discourage anyone who continually makes progress, no matter how
slow." Plato

Warren Lynn

unread,
Jun 12, 2012, 6:00:30 PM6/12/12
to clo...@googlegroups.com, ja...@laskowski.net.pl

As the name says uberjar is what you should be able to run with no
worries about the classpath - all deps are included in the final jar.
Since you asked to exclude some deps, you likely run the final jar in
a kind of managed environment. Why are there some deps not the others?
What drives the exclusion? Wouldn't lein jar alone be enough? Why?
Just curious and try to understand what you've already done :)


I plan to deploy the jar as a lib in another Java framework. This clojure .jar file depends on some common lib (API kind of thing) that will be included in the Java framework itself so I don't want to include another copy.

"uberjar" might not be the right command (BTW: i don't know what that name means), but is there any way to do it, without manually pick-and-package? Thanks.

Phil Hagelberg

unread,
Jun 12, 2012, 6:55:49 PM6/12/12
to clo...@googlegroups.com
On Tue, Jun 12, 2012 at 3:00 PM, Warren Lynn <wrn....@gmail.com> wrote:
> I plan to deploy the jar as a lib in another Java framework. This clojure
> .jar file depends on some common lib (API kind of thing) that will be
> included in the Java framework itself so I don't want to include another
> copy.
>
> "uberjar" might not be the right command (BTW: i don't know what that name
> means), but is there any way to do it, without manually pick-and-package?
> Thanks.

If you don't need AOT then you can include the common lib in
dependencies in the :dev profile.

If you need AOT then you'll have to write a plugin that works like a
more selective variant of uberjar.

-Phil

Warren Lynn

unread,
Jun 12, 2012, 7:38:11 PM6/12/12
to clo...@googlegroups.com
If you don't need AOT then you can include the common lib in
dependencies in the :dev profile.

If you need AOT then you'll have to write a plugin that works like a
more selective variant of uberjar.

-Phil

I do need AOT, and I am not ready yet to write any plugins. Maybe I can find some tools to remove the unwanted jars after "lein uberjar", just another thought.
 

Jacek Laskowski

unread,
Jun 13, 2012, 7:35:34 AM6/13/12
to clo...@googlegroups.com
On Wed, Jun 13, 2012 at 1:38 AM, Warren Lynn <wrn....@gmail.com> wrote:
> If you don't need AOT then you can include the common lib in

Could :uberjar-exclusions help in any way? It's a vector of regexps to
exclude files from the uberjar. It could be a starting point to try
out.

Jacek Laskowski

unread,
Jun 13, 2012, 4:42:40 PM6/13/12
to clo...@googlegroups.com
On Wed, Jun 13, 2012 at 12:55 AM, Phil Hagelberg <ph...@hagelb.org> wrote:

> If you don't need AOT then you can include the common lib in
> dependencies in the :dev profile.

Hi Phil,

Thanks for the idea - I blogged about the solution and it worked great!

http://blog.japila.pl/2012/06/exclude-dependencies-from-uberjar-in-leiningen-2/

> If you need AOT then you'll have to write a plugin that works like a
> more selective variant of uberjar.

That's the part I didn't understand. Could you elaborate what wouldn't work?

Phil Hagelberg

unread,
Jun 13, 2012, 4:47:55 PM6/13/12
to clo...@googlegroups.com
On Wed, Jun 13, 2012 at 1:42 PM, Jacek Laskowski <ja...@laskowski.net.pl> wrote:
> That's the part I didn't understand. Could you elaborate what wouldn't work?

If you try to create an uberjar with AOT using a profile that excludes
the dependencies you don't want in your uberjar then the AOT will fail
because it tries to references classes that don't exist. You might be
able to get around this by resolving classes at runtime, but it would
be messy.

-Phil

Jacek Laskowski

unread,
Jun 13, 2012, 4:52:44 PM6/13/12
to clo...@googlegroups.com
On Wed, Jun 13, 2012 at 10:47 PM, Phil Hagelberg <ph...@hagelb.org> wrote:

> If you try to create an uberjar with AOT using a profile that excludes
> the dependencies you don't want in your uberjar then the AOT will fail
> because it tries to references classes that don't exist.

Why does AOT happen after exclusion? Shouldn't exclusion be the last
step in the process which would hardly break anything and would
eventually fix the issue?

Phil Hagelberg

unread,
Jun 13, 2012, 5:23:52 PM6/13/12
to clo...@googlegroups.com
On Wed, Jun 13, 2012 at 1:52 PM, Jacek Laskowski <ja...@laskowski.net.pl> wrote:
>> If you try to create an uberjar with AOT using a profile that excludes
>> the dependencies you don't want in your uberjar then the AOT will fail
>> because it tries to references classes that don't exist.
>
> Why does AOT happen after exclusion? Shouldn't exclusion be the last
> step in the process which would hardly break anything and would
> eventually fix the issue?

Oh, I meant this would be a problem if you used profiles for
separating dependencies. If you use :uberjar-exclusions you'll be
fine.

-Phil

Warren Lynn

unread,
Jun 13, 2012, 8:15:49 PM6/13/12
to clo...@googlegroups.com

> Why does AOT happen after exclusion? Shouldn't exclusion be the last
> step in the process which would hardly break anything and would
> eventually fix the issue?

Oh, I meant this would be a problem if you used profiles for
separating dependencies. If you use :uberjar-exclusions you'll be
fine.

-Phil
 
Thank you all for the suggestions. I just tried to put my unwanted dependencies into the :dev profile, at the beginning AOT seems to work fine and lein uberjar does not include that .jar file. But once I started to use one of the functions in the .jar file (not just putting "import" in the ns), AOT failed.

Although not ideal, :uberjar-exclusions works for me now as all the files in my issue .jar file has a common package prefix.



dongyong wang

unread,
Oct 16, 2013, 11:14:07 PM10/16/13
to clo...@googlegroups.com
I meet the same problem,try the following setting:

:profiles {:provided {:dependencies [
                                        [org.apache.hadoop/hadoop-core "2.0.0-mr1-cdh4.4.0" :scope "runtime"]
                                        [org.apache.hadoop/hadoop-common "2.0.0-cdh4.4.0" :scope "runtime"]
                                        ]

                        }}

When I run 'lein ubjerjar',the standlone jar does not contain the hadoop jars.

You can try it.


在 2012年6月14日星期四UTC+8上午8时15分49秒,Warren Lynn写道:
Reply all
Reply to author
Forward
0 new messages