As of April 2019:
An update: rules_jvm_external
is a new ruleset by the Bazel team to fetch and resolve artifacts transitively.
In this case, the WORKSPACE file will contain something like this:
load("@rules_jvm_external//:defs.bzl", "maven_install")
maven_install(
artifacts = [
"com.android.volley:volley:1.1.0",
"com.google.code.gson:gson:2.6.2",
"com.android.support:design:27.0.2",
"com.android.support:support_annotations:jar:27.0.2",
],
repositories = [
"https://jcenter.bintray.com",
"https://maven.google.com",
]
)
Then in the BUILD file, you can directly depend on Volley and Gson like this:
android_library(
name = "my_lib",
srcs = # ...
deps = [
"@maven//:com_android_volley_volley",
"@maven//:com_google_code_gson_gson",
"@maven//:com_android_support_design",
"@maven//:com_android_support_support_annotations",
],
)
As of May 2018:
Both gmaven_rules
and maven_jar
are deprecated. Please do not use them anymore.
From the documentation on docs.bazel.build, edited with additional relevant instructions:
Maven dependencies hosted on Maven Central Repository
Use the maven_jar
repository rule for Maven dependencies not hosted on
Google Maven. For example, to use Volley 1.1.0 and Gson 2.6.2, add the
following lines to the WORKSPACE
file at the top level of the project directory:
maven_jar(
name = "com_android_volley_volley",
artifact = "com.android.volley:volley:1.1.0",
)
maven_jar(
name = "com_google_code_gson",
artifact = "com.google.code.gson:gson:2.6.2",
)
Then, you can depend on them in your BUILD
files:
android_library(
name = "my_app_lib",
srcs = [..],
deps = [
"@com_android_volley_volley//jar",
"@com_google_code_gson//jar",
],
)
Note that maven_jar
is not transitive, so it does not download the dependencies of the specified jar. You will need to manually specify additional maven_jar
rules for the transitive dependencies, or use a tool like bazel-deps
to generate them automatically.
Maven dependencies hosted on Google Maven Repository (https://maven.google.com)
For dependencies hosted on Google's Maven repository,
@gmaven_rules
provides a simple way to fetch dependencies
hosted with gmaven_artifact
by specifying the artifact coordinate directly.
To use @gmaven_rules
, add these lines to the WORKSPACE
file:
# Google Maven Repository
GMAVEN_TAG = "20180513-1"
http_archive(
name = "gmaven_rules",
strip_prefix = "gmaven_rules-%s" % GMAVEN_TAG,
url = "https://github.com/bazelbuild/gmaven_rules/archive/%s.tar.gz" % GMAVEN_TAG,
)
load("@gmaven_rules//:gmaven.bzl", "gmaven_rules")
gmaven_rules()
Then, load the gmaven_artifact
macro at the beginning of your BUILD file
to use it:
load("@gmaven_rules//:defs.bzl", "gmaven_artifact")
android_library(
name = "my_app_lib",
srcs = glob(["java/**/*.java"]),
deps = [
gmaven_artifact("com.android.support:design:aar:27.0.2"),
gmaven_artifact("com.android.support:support_annotations:jar:27.0.2"),
]
# ...
)
Unlike maven_jar
, gmaven_artifact
is transitive, so you only need to specify the coordinate of the artifact and @gmaven_rules
will resolve the dependencies automatically.