Generate bazel dependencies transitively for maven artifacts, with scala
support.
Quickstart
This repo can be cloned and built locally, or you can download pre-build binaries for MacOS and Linux in the releases page. Automatic releases are generated for every commit against master.
We also include a bash script in the releases which will let you easily download/run on mac/linux a default configuration for running bazel-deps.
A flow like:
Download the bash script paired with the release, it has the expected per platform sha256's embedded in it
Place in your repo and chmod +x update_dependencies.sh, maybe in a scripts folder if you wish.
Copy the dependencies.yaml from this repo, or write your own
Run the script, it should produce some files in 3rdparty
You can now add dependencies to your BUILD files as described below.
eg deps = ["@third_party//3rdparty/jvm/org/scalaj:scala_http"].
Usage
First, list all of your maven artifact dependencies in a Dependencies file.
Next, run parseproject on your project yaml file. This will create a tree of BUILD files that
match the maven group id, and the artifact id will be a label in a BUILD file. You should not
edit these by hand, and instead have a separate directory for any exceptions that you manage
along with Replacements. For instance, this project is setup with:
We give three arguments: the path to the file we will include in our workspace. The path to the root
of our bazel repo. The path to the dependencies file. You can also run with --help.
For example, if your project is located at MY_PROJ_DIR, your dependencies file is at
MY_PROJ_DIR/dependencies.yaml, and your checkout of bazel-deps is at BAZEL_DEPS, to generate the
dependencies you need to do the following:
The final result in MY_PROJ_DIR will look like this
MY_PROJ_DIR
├── 3rdparty <-- everything under here is generated by running gen_maven_deps.sh
│ ├── workspace.bzl <-- load() this from main WORKSPACE
│ └── jvm/ <-- generated BUILD files in this directory.
├── BUILD
├── WORKSPACE
└── dependencies.yaml <-- your project's dependencies are declared in here.
Whenever you update the dependencies declared in dependencies.yaml you will need to regenerate the
contents of the 3rdparty directory by re-running $BAZEL_DEPS/gen_maven_deps.sh generate.
Alternate outputs, external repo
Bazel-deps can also prepare the outputs, not as a file tree but an external repo. With this one would refer to targets as
@third_party//foo:bar rather than //3rdparty/jvm/foo/bar. This is useful if you do not want to check in generated code to your
repo. Also if multiple repos are depending upon one another and using bazel deps this can avoid broken transitive dependencies.
That is if there are two repos A and B where B depends on A:
where A has Foo 1.0 dependson Jackson27
and B has Foo 2.0 depends on circe
with the checked in version both will compile from source against the local copy of Foo, but transitively on the classpath
in the repo BJackson27 will be on the classpath rather than circe.
To use this option you would execute bazel-deps like:
This tool will generate one canonical version for every jar in the transitive dependencies of
the root dependencies declared. You have three conflict resolution modes currently (which currently
apply globally):
fail: if more than one version is found transitively, fail.
fixed: for all artifacts explicitly added, use that version, otherwise fail if any other artifact has multiple versions.
highest: for all artifacts explicitly added, use that version, otherwise take the highest version.
In any case, we add a comment for any duplicates found in the workspace loading file.
To declare dependencies, add items to the dependencies key in your yaml file. The format
should be yaml or json. It should have dependencies and it may have replacements
and options. Important: only dependencies explicitly named have public visibility,
transitive dependencies not listed in the dependencies file have visibility limited to the third
party directory.
Language is always required and may be one of java, scala, scala/unmangled. To control the scala
version, see the Options section. A common case are projects with many modules. For instance in
the scalding project there are many modules: -core, -date, -args, -db, -avro to name a few. To reduce duplication you can do:
The version field is optional. If it is absent, it means this jar is expected to be found by
transitive dependencies, and it is available to be used outside of the thirdparty directory, but the
exact version used can be selected according to the version resolution rules. It is an error to have
an unversioned dependency that is not a transitive dependency of another versioned dependency.
A target may optionally add exports and exclude lists to a dependency. exports should be just the group and
artifact (such as: com.twitter:scalding-core in the above), and they should be listed in the dependencies. exclude
list should also be only the group and artifact.
It's possible to add generateNeverlink option to a dependency, which will make the generator to generate this dependency twice:
With the normalized name as usual.
With the name ${normalized}_neverlink and neverlink is set as true.
This option should be used only for java dependencies, it will be ignored in any other lang.
Each group id can only appear once, so you should collocate dependencies by group. WARNING the parsing library
we are using does not fail on duplicate keys, it just takes the last one, so watch out. It would be good
to fix that, but writing a new yaml parser is out of scope.
Note: Currently, only jar packaging is supported for dependencies. More work is needed on the bazel-deps backend
to ensure that non-jar dependencies are written as data attributes, instead of regular jar dependencies.
Excluding artifacts with packaging or classifiers is similar to including dependencies. Non-jar packaging is supported
for exclude.
A target may also optionally add processorClasses to a dependency. This is for annotation processors.
bazel-deps will generate a java_library and a java_plugin for each annotation processor defined. For example, we can define Google's auto-value annotation processor via:
If there is only a single processorClasses defined, the java_plugin rule is named <java_library_name>_plugin. If there are multiple
processorClasses defined, each one is named <java_library_name>_plugin_<processor_class_to_snake_case>.
There are a number of ways to customize the generated build files. These are controlled
by the options dictionary at the root-level of the dependencies file. This is a list of
all of the supported options.
buildHeader: usually you will want to configure your scala support here:
languages: an array of languages to be supported either Java or a specific version of Scala, e.g. [ "java", "scala:2.12.8" ].
thirdPartyDirectory: path to where we write the BUILD files for thirdparty. The default is 3rdparty/jvm. If you choose the Google default of third_party you will need to configure the licenses option as well.
versionConflictPolicy: fixed, fail or highest
transitivity: runtime_deps or exports
resolvers: the maven servers to use. Each resolver is defined by three keys, an "id", a "type", and a "url".
resolverCache: (with resolverType: aether) where bazel-deps should cache resolved packages. local (target/local-repo in the repository root)
or bazel_output_base (bazel-deps/local-repo inside the repository's Bazel output base -- from bazel info output_base). Coursier ignores this option and uses ~/.cache/coursier.
namePrefix: a string added to the generated workspace names, to avoid conflicts. The external repository names and
binding targets of each dependency are prefixed.
strictVisibility: this is enabled by default, when enabled a target must be explicitly declared in the
dependencies.yaml file or it will not be visible to the rest of the workspace. If it is set to false all targets
will be generated with public visibility.
licenses: a set of strings added a licenses rule to each generated bazel target. Required by
bazel if your build targets are under third_party/. See the licenses function in Bazel.
resolverType: aether or coursier. Note that aether is slower and seems to silently miss some dependencies for
reasons we don't yet understand.
buildFileName: filename of the generated build files
In the default case, with no options given, we use:
Some maven jars should not be used and instead are replaced by internal targets. Here are
some examples of this:
A subproject in the repo is published as a maven artifact (A). Others (B) depend on this artifact (B -> A) and in turn we depend on those (we have added B to our dependencies file). We don't want to pull A from a maven repo, since we build it internally, so we replace that artifact with an internal target.
We get some scala artifacts directly from the sdk. So, if a jar says it needs org.scala-lang:scala-library we already have that (and a few other jars) declared, and we don't want to risk having two potentially incompatible versions.
A small external project has both a bazel build and a maven publishing. We prefer to use the bazel build so we can easily pull more recent versions by bumping up a gitsha rather than waiting for jar to be published.
The replacements work on the level of artifacts. An artifact is replaced one-for-one with a local
bazel target. For instance:
replacements:
org.scala-lang:
scala-library:
lang: scala/unmangled # scala-library is not mangled like sbt does with other jarstarget: "@io_bazel_rules_scala_scala_library"scala-reflect:
lang: scala/unmangledtarget: "@io_bazel_rules_scala_scala_reflect"
In this way, we redirect maven deps to those providers.
Note, we stop walking the graph when we see a replaced node, so the replacement target is now
responsible for building correctly, and correctly exporting any dependencies that need to be
on the compile classpath.
请发表评论