在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):square/bazel_maven_repository开源软件地址(OpenSource Url):https://github.com/square/bazel_maven_repository开源编程语言(OpenSource Language):Kotlin 63.5%开源软件介绍(OpenSource Introduction):Bazel Rules for Maven RepositoriesA bazel ruleset creating an idiomatic bazel representation of a maven repository using a pinned list of artifacts. Release:
OverviewBazel Rules for Maven Repositories allow the specification of a list of artifacts which constitute maven repository's universe of deps, and exposes these artifacts via a bazel external workspace. The name of the repository specification rule becomes the repository name in Bazel. For instance the following specification: MAVEN_REPOSITORY_RULES_VERSION = "1.2.0"
MAVEN_REPOSITORY_RULES_SHA = "9e23155895d2bfc60b35d2dfd88c91701892a7efba5afacdf00cebc0982229fe"
http_archive(
name = "maven_repository_rules",
urls = ["https://github.com/square/bazel_maven_repository/archive/%s.zip" % MAVEN_REPOSITORY_RULES_VERSION],
type = "zip",
strip_prefix = "bazel_maven_repository-%s" % MAVEN_REPOSITORY_RULES_VERSION,
sha256 = MAVEN_REPOSITORY_RULES_SHA,
)
load("@maven_repository_rules//maven:maven.bzl", "maven_repository_specification")
maven_repository_specification(
name = "maven",
artifacts = {
"com.google.guava:guava:25.0-jre": { "sha256": "3fd4341776428c7e0e5c18a7c10de129475b69ab9d30aeafbb5c277bb6074fa9" },
}
) ... results in deps of the format Dependency versions are resolved in the single artifact list. Only one version is permitted within a repository.
Supported TypesCurrently
For any other types, please file a feature request, or supply a pull request. So long as there exists a proper bazel import or library rule to bring the artifact's file into bazel's dependency graph, it should be possible to support it. Inter-artifact dependenciesThis rule will, in the generated repository, infer inter-artifact dependencies from pom.xml files
of those artifacts (pulling in only All artifacts, even if only transitively depended, must be specified with a pinned version in the
Coordinate TranslationTranslation from maven group/artifact coordinates to bazel package/target coordinates is naive but
orderly. The logic mirrors the layout of a maven repository, with groupId elements (separated by
For example: Artifact ConfigurationSha verificationArtifacts with SHA256 checksums can be added to
Artifacts without SHA headers should configured as insecure, like so:
The rules will reject artifacts without SHAs are not marked as "insecure".
TestonlyAn artifact can be declared testonly - this forces the maven_repository_specification(
name = "maven",
artifacts = {
"com.google.truth:truth:1.0": {"insecure": True, "testonly": True},
# ... all the other deps.
},
) Exclude
An artifact can have some (or all) of its direct dependencies pruned by use of the maven_repository_specification(
name = "maven",
artifacts = {
"com.google.truth:truth:1.0": {
"insecure": True,
"exclude": ["com.google.auto.value:auto-value-annotations"],
},
# ... all the other deps.
},
) This results in truth omitting a dependency on the auto-value annotations - useful to avoid
dependencies not specified as
Include
An artifact can have additional dependencies added using the maven_repository_specification(
name = "maven",
artifacts = {
"com.helpshift:android-helpshift-aar:7.8.0": {
"insecure": True,
"include": [
# Bazel dependencies
"@//some/local/dependency", # in the root workspace of this build
"@some_workspace//some/other/dep", # in some external workspace of this build
# While theoretically in the root workspace, because this occurs inside the maven
# workspace maven may interpret this as being inside the @maven workspace. Use
# the @// prefix to force it into the root workspace
"//some/local/dependency",
# Useful for referencing some custom thing added by a snippet on another artifact.
":some_target_in_the_same_package",
# Maven artifact coordinates, which will be interpreted relative to the presently
# configured maven workspace. These will also result in errors if their versioned
# artifacts have not been pinned in the artifact dictionary.
"androidx.cardview:cardview",
"androidx.recyclerview:recyclerview",
"com.google.android.material:material",
]
},
# ... all the other deps.
},
) This results in the bazel workspace generating additional dependencies, converting maven-style
These additions are performed after automatically detected dependencies are processed (and after excludes are processed). It can be used to add missing deps from badly specified pom files, or it can add-back-in optional dependencies (which are not included by default).
Deps
An artifact can have its dependencies entirely specified (overriding the automated detection
via maven resolution) using the maven_repository_specification(
name = "maven",
artifacts = {
"com.helpshift:android-helpshift-aar:7.8.0": {
"insecure": True,
"deps": [
# Bazel dependencies
"@//some/local/dependency", # in the root workspace of this build
"@some_workspace//some/other/dep", # in some external workspace of this build
# While theoretically in the root workspace, because this occurs inside the maven
# workspace maven may interpret this as being inside the @maven workspace. Use
# the @// prefix to force it into the root workspace
"//some/local/dependency",
# Useful for referencing some custom thing added by a snippet on another artifact.
":some_target_in_the_same_package",
# Maven artifact coordinates, which will be interpreted relative to the presently
# configured maven workspace. These will also result in errors if their versioned
# artifacts have not been pinned in the artifact dictionary.
"androidx.cardview:cardview",
"androidx.recyclerview:recyclerview",
"com.google.android.material:material",
]
},
# ... all the other deps.
},
) This results in the bazel workspace ignoring the dependencies from resolved metadata, and using
the supplied list instead. It converts maven-style
Substitution of build targets
One can provide a A simple use-case would be to substitute a target name (e.g. "mockito-core" -> "mockito") for cleaner/easier use in bazel: maven_repository_specification(
name = "maven",
artifacts = {
"org.mockito:mockito-core:2.20.1": {
"sha256": "blahblahblah",
"build_snippet": """maven_jvm_artifact(name = "mockito", artifact = "org.mockito:mockito-core:2.20.1")""",
},
# ... all the other deps.
},
) This would allow the following use in a java_test(
name = "MyTest",
srcs = "MyTest.java",
deps = [
# ... other deps
"@maven//org/mockito" # instead of "@maven//org/mockito:mockito-core"
],
) More complex use-cases are possible, such as adding substitute targets with annotation processing
DAGGER_PROCESSOR_SNIPPET = """
# use this target
java_library(name = "dagger", exports = [":dagger_api"], exported_plugins = [":dagger_plugin"])
# alternatively-named import of the raw dagger library.
raw_jvm_import(
name = "dagger_api",
jars = "@com_google_dagger_dagger//maven",
)
java_plugin(
name = "dagger_plugin",
processor_class = "dagger.internal.codegen.ComponentProcessor",
generates_api = True,
deps = [":dagger_compiler"],
)
""" The above is given as a substitution in the maven_repository_specification(
name = "maven",
artifacts = {
"com.google.dagger:dagger:2.20": {
"sha256": "blahblahblah",
"build_snippet": DAGGER_PROCESSOR_SNIPPET,
},
"com.google.dagger:dagger-compiler:2.20": { "sha256": "blahblahblah" },
"com.google.dagger:dagger-producers:2.20": { "sha256": "blahblahblah" },
"com.google.dagger:dagger-spi:2.20": { "sha256": "blahblahblah" },
"com.google.code.findbugs:jsr305:3.0.2": { "sha256": "blahblahblah" },
# ... all the other deps.
},
dependency_target_substitutes = {
"com.google.dagger": {"@maven//com/google/dagger:dagger": "@maven//com/google/dagger:dagger_api"},
}
) Thereafter, any target with a dependency on (in this example) Such snippet constants can be extracted into .bzl files and imported to keep the WORKSPACE file tidy. In the future some standard templates may be offered by this project, but not until deps validation is available, as it would be too easy to have templates' deps lists go out of date as versions bumped, if no other validation prevented it or notified about it.
CachingBazel Maven Repository 1.2.0 and prior:
Bazel can cache artifacts if you provide sha256 hashes. These will make the artifacts candidates
for the "content addressable" cache, which is machine-wide, and survives even Pom.xml files are not cached, and will be re-downloaded when your workspace needs regenerating. Bazel Maven Repository 2.0 and later:
The rules will cache maven artifacts and metadata via the Limitations
Other Usage NotesCachesBecause of the nature of bazel repository/workspace operation, updating the list of artifacts invalidates the analysis cache, and force a re-run of workspace operations (and possibly reduce incrementality of the next build). As of 2.0, the workspace generation is drastically faster. Clogged WORKSPACE filesIt may make sense, if one's maven universe gets big, to extract the list of artifacts into a
constant in a separate file (e.g. Kotlinijar (abi-jar) and inline functionsBazel Maven Repository uses a raw_jvm_import target which does not invoke ijar as it is known to break Kotlin artifacts with inlined operators and inlined types. rules_kotlin and maven integration paths[rules_kotlin] currently break when running in full sandbox mode (without the kotlin compilation
worker). Specifically, it misinterprets paths in the sandbox. Therefore, if using [rules_kotlin]
it is crucial to include LicenseLicense Copyright 2018 Square, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论