preferred method for relocation (shading) libraries?

953 views
Skip to first unread message

ssuc...@pepperdata.com

unread,
Oct 4, 2017, 5:37:19 PM10/4/17
to bazel-discuss
Hi all -

I'm building a project that depends on other projects. Originally, this project used maven. I am using maven_jar successfully to get dependencies. However, there are some dependencies that are transitively used by two immediate dependencies and they cannot agree on the same version of this share dependence. (aws-sdk wants some versions of the apache http library and jackson library, and google play wants different versions of the same libraries.)

In maven, I successfully was using maven-shade-plugin to relocate one copy of the shared dependence, so that I could effectively have two different versions in my final environment. (This is a common reason to use shading.)

I'm unclear what the current best recommended way to accomplish this in Bazel. I found some references in 2015, but I wonder if there has been any further work on this topic?

https://groups.google.com/forum/#!searchin/bazel-discuss/shade|sort:relevance/bazel-discuss/nwUHQOui5PE/s_Ju9W3RBQAJ

https://groups.google.com/forum/#!searchin/bazel-discuss/shade|sort:relevance/bazel-discuss/MbMt3c6YHl4/tL6xyx4aDQAJ

I tried to look carefully through the Java Rules page:

https://docs.bazel.build/versions/master/be/java.html

but couldn't find anything about shading or relocation.

Any guidance appreciated! (That isn't "go modify aws-sdk or google play")

Thanks, Sean

P. Oscar Boykin

unread,
Oct 5, 2017, 1:29:36 PM10/5/17
to bazel-discuss
We use this rule I wrote at Stripe:


it uses jarjar to build a new jar. You can do it to shade a dependency, or transform a jar before you use it in a binary for deploy.

Sean Suchter

unread,
Oct 5, 2017, 2:16:59 PM10/5/17
to P. Oscar Boykin, bazel-discuss
Thanks! What JDK do you use? I saw some comments online about jar jar not being compatible with JDK 8+, but maybe that’s not true in your version?

Thanks again,
Sean
--
You received this message because you are subscribed to a topic in the Google Groups "bazel-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bazel-discuss/WStUENkLJzo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/b5ef253a-f059-414e-8d73-efa69ad23568%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

P. Oscar Boykin

unread,
Oct 5, 2017, 2:23:56 PM10/5/17
to Sean Suchter, bazel-discuss
We are using jdk8.

I haven't seen a problem, but maybe there are some features jdk8 uses we aren't hitting how we are using it.


To unsubscribe from this group and all its topics, send an email to bazel-discuss+unsubscribe@googlegroups.com.



--

ssuc...@pepperdata.com

unread,
Oct 10, 2017, 7:34:39 PM10/10/17
to bazel-discuss
A followup -

This worked well for me - thanks for the pointer.

For posterity, here's how I incorporated it into my build. I downloaded the bazel_jar_jar-master.zip from github and put it in our Artifactory server. (As to not have a direct compile time dependence on Github)

I then put this in my WORKSPACE:

# Make bazel_jar_jar available
http_archive(
name = "com_github_johnynek_bazel_jar_jar",
url = "http://pep01.pepperdata.com:8081/artifactory/libs-release-local/com_github_johnynek/bazel_jar_jar/20171010/bazel_jar_jar-20171010.zip",
sha256 = "b23121bab94c445c4668f4824e8e95a76502341b3487b93779e82e051ca7cad3",
strip_prefix = "bazel_jar_jar-master",
)

load("@com_github_johnynek_bazel_jar_jar//:jar_jar.bzl", "jar_jar_repositories")

jar_jar_repositories()

Then in my BUILD file I used this:

java_library(
name = "compile_jars_neverlink",
exports = [
"//my_project/artifacts:compile_jars"
],
neverlink = 1,
)

# A new rule is necessary for the standalone jar build
java_binary(
name = "MyProjectStandalone",
srcs = glob(["src/main/java/**/*.java"]),
deps = [
":compile_jars_neverlink",
"@com_pepperdata_common_PepperdataCommon_1_13//jar",
"@com_amazonaws_aws_java_sdk_core_1_11_30//jar",
"@com_amazonaws_aws_java_sdk_kms_1_11_30//jar",
...
],
create_executable = 0,
)

jar_jar(
name = "MyProjectShaded",
input_jar = ":MyProjectStandalone_deploy.jar",
rules = "shading_rules.txt"
)
> bazel-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages