Bazel: testng macro - bazel

I'm trying to create a macro in Bazel to wrap java_test to run testng, however I'm running into trouble passing TestNG the filename
So far I have
load("#bazel_skylib//:lib.bzl", "paths")
def java_testng(file, deps=[], **kwargs):
native.java_test(
name = paths.split_extension(file)[0],
srcs = [file],
use_testrunner=False,
main_class='org.testng.TestNG',
deps = [
"//third_party:org_testng_testng"
] + deps,
args=[file],
**kwargs
)
However args seems to be a non-existent runfile.
Help appreciated on the correct value for args
Here is a sample usage I would like
java_testng(
file = "SomeFakeTest.java",
deps = [
"//:resources",
"//third_party:com_fasterxml_jackson_core_jackson_databind",
"//third_party:org_assertj_assertj_core",
],
)

Here is the solution I came up with
load("#bazel_skylib//:lib.bzl", "paths")
def java_testng(file, deps=[], size="small", **kwargs):
native.java_library(
name = paths.split_extension(file)[0] + "-lib",
deps = [
"//third_party:org_testng_testng"
] + deps,
srcs = [file]
)
native.java_test(
name = paths.split_extension(file)[0],
use_testrunner=False,
main_class='org.testng.TestNG',
runtime_deps = [
"//third_party:org_testng_testng",
paths.split_extension(file)[0] + "-lib"
],
data = [file],
size = size,
args=["-testclass $(location " + file + ")"],
**kwargs
)

I dont know why you used a macro, I manage to call testng without.
See my solution below:
I create my program jar (using some Annotation Processor)
I create my test jar (using some Annotation Processor)
I call testng via java_test().
The alone thing I didn't figure out: how to not hardcode the "libmy-model-test-lib.jar"
java_library(
name = "my-model",
srcs = glob(["src/main/java/**/*.java"]),
resources = glob(["src/main/resources/**"]),
deps = [
"#commons_logging_jar//jar",
":lombok",
":mysema_query",
...
],
)
java_library(
name = "my-model-test-lib",
srcs = glob(["src/test/java/**/*.java"]),
deps = [
"#org_hamcrest_core_jar//jar",
"#commons_logging_jar//jar",
":lombok",
":mysema_query",
...
"#assertj_jar//jar",
"#mockito_jar//jar",
"#testng_jar//jar",
],
)
java_test(
name = "AllTests",
size = "small",
runtime_deps = [
":my-model-test-lib",
":my-model",
"#org_jboss_logging_jar//jar",
"#org_objenesis_jar//jar",
"#com_beust_jcommander//jar",
],
use_testrunner=False,
main_class='org.testng.TestNG',
args=['-testjar','libmy-model-test-lib.jar','-verbose','2'],
)
java_plugin(
name = "lombok_plugin",
processor_class = "lombok.launch.AnnotationProcessorHider$AnnotationProcessor",
deps = ["#lombok_jar//jar"],
)
java_library(
name = "lombok",
exports = ["#lombok_jar//jar"],
exported_plugins = [":lombok_plugin"],
)
java_plugin(
name = "mysema_query_plugin",
processor_class = "com.mysema.query.apt.jpa.JPAAnnotationProcessor",
deps = [
"#querydsl_apt_jar//jar",
"#mysema_codegen_jar//jar",
"#javax_persistence_jar//jar",
"#querydsl_codegen_jar//jar",
"#guava_jar//jar",
"#querydsl_core_jar//jar",
"#javax_inject_jar//jar",
],
)
java_library(
name = "mysema_query",
exports = ["#querydsl_apt_jar//jar"],
exported_plugins = [":mysema_query_plugin"],
)
java_plugin(
name = "mockito_plugin",
processor_class = "",
deps = ["#mockito_jar//jar"],
)
java_library(
name = "mockito",
exports = ["#mockito_jar//jar"],
exported_plugins = [":mockito_plugin"],
)

Related

Bazel convert tar to zip

Is there a way to convert a tar target (created using rules_pkg) and turn it into a zip target in Bazel?
pkg_tar(
name = "bundle-tar",
srcs = [
":aws-lambda",
],
include_runfiles = True,
package_dir = "/",
)
pkg_zip(
name = "bundle-zip",
srcs = [
":bundle-tar", # ???
],
)
I imagine this could be accomplished using #bazel_tools//tools/zip:zipper?
This kind of works, but it is not deterministic:
genrule(
name = "bundle-zip",
srcs = [ ':bundle-tar' ],
outs = [ "bundle.zip" ],
cmd = " && ".join([
"tar xf $(location :bundle-tar)",
"zip $# $$(tar tf $(location :bundle-tar))",
]),
)
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_pkg",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.7.0/rules_pkg-0.7.0.tar.gz",
"https://github.com/bazelbuild/rules_pkg/releases/download/0.7.0/rules_pkg-0.7.0.tar.gz",
],
sha256 = "8a298e832762eda1830597d64fe7db58178aa84cd5926d76d5b744d6558941c2",
)
load("#rules_pkg//:deps.bzl", "rules_pkg_dependencies")
rules_pkg_dependencies()
Please refer to this issue for a detailed explanation on how to convert tar to zip files. Thank you.

How can I build SBTUITestTunnel for a Swift app using Buck?

I am trying to use Buck to include the SBTUITestTunnel library in a couple of iOS apps I am working on. This library extends the standard Xcode UI-testing framework to offer additional features such as monitoring network calls. While SBTUITestTunnel is written mostly in Objective-C, The apps I hope to include this library in are written in Swift and use Buck as their build tool. I'd also like to be able to use both Buck and Xcode to execute the tests once I have them written.
Unfortunately, the SBTUITestTunnel Installation/Setup/Usage docs do not seem to provide any guidance on building them with Buck. The repo does include CocoaPods config files, which I have tried to convert to a BUCK file. However, I've been unable to get that working properly after several attempts. I've even tried grabbing the prebuilt .frameworks from the build output of the provided SBTUITestTunnel Example App, but still no luck.
Here are the attempts I've made so far at creating a working BUCK file:
Attempt 1 - Building as Libraries from Source:
Code:
apple_library(
name = 'SBTUITestTunnelCommon',
visibility = ['PUBLIC'],
swift_version = '5',
exported_headers = glob([
'Pod/Common/**/*.h',
]),
srcs = glob([
'Pod/Common/**/*.swift',
'Pod/Common/**/*.m',
]),
deps = [
'//Path/GCDWebServer:GCDWebServer',
]
)
apple_library(
name = 'SBTUITestTunnelClient',
visibility = ['PUBLIC'],
swift_version = '5',
exported_headers = glob([
'Pod/Client/**/*.h',
]),
srcs = glob([
'Pod/Client/**/*.swift',
'Pod/Client/**/*.m',
]),
deps = [
'//Path/GCDWebServer:GCDWebServer',
'//Path/SBTUITestTunnel:SBTUITestTunnelCommon',
]
)
apple_library(
name = 'SBTUITestTunnelServer',
visibility = ['PUBLIC'],
swift_version = '5',
exported_headers = glob([
'Pod/Server/**/*.h',
]),
srcs = glob([
'Pod/Server/**/*.swift',
'Pod/Server/**/*.m',
]),
deps = [
'//Path/GCDWebServer:GCDWebServer',
'//Path/SBTUITestTunnel:SBTUITestTunnelCommon',
]
)
Result:
Buck build completes successfully, but running tests in Buck and trying to build in Xcode both fail due with errors like this one (when trying to run #import SBTUITestTunnelCommon; from a file in the SBTUITestTunnelServer class):
Module 'SBTUITestTunnelCommon' not found
Attempt 2 - Building as Frameworks from Source:
Code:
sbt_ui_test_tunnel_common_bundle_name = 'SBTUITestTunnelCommon'
sbt_ui_test_tunnel_common_binary_name = sbt_ui_test_tunnel_common_bundle_name + 'Binary'
sbt_ui_test_tunnel_common_module_name = sbt_ui_test_tunnel_common_bundle_name
sbt_ui_test_tunnel_common_product_name = sbt_ui_test_tunnel_common_bundle_name
# Copied `Info.plist` from the Example app to this Pod folder:
sbt_ui_test_tunnel_common_info_plist = 'Pod/Common/' + sbt_ui_test_tunnel_common_bundle_name + '-Info.plist'
sbt_ui_test_tunnel_client_bundle_name = 'SBTUITestTunnelClient'
sbt_ui_test_tunnel_client_binary_name = sbt_ui_test_tunnel_client_bundle_name + 'Binary'
sbt_ui_test_tunnel_client_module_name = sbt_ui_test_tunnel_client_bundle_name
sbt_ui_test_tunnel_client_product_name = sbt_ui_test_tunnel_client_bundle_name
# Copied `Info.plist` from the Example app to this Pod folder:
sbt_ui_test_tunnel_client_info_plist = 'Pod/Client/' + sbt_ui_test_tunnel_client_bundle_name + '-Info.plist'
sbt_ui_test_tunnel_server_bundle_name = 'SBTUITestTunnelServer'
sbt_ui_test_tunnel_server_binary_name = sbt_ui_test_tunnel_server_bundle_name + 'Binary'
sbt_ui_test_tunnel_server_module_name = sbt_ui_test_tunnel_server_bundle_name
sbt_ui_test_tunnel_server_product_name = sbt_ui_test_tunnel_server_bundle_name
# Copied `Info.plist` from the Example app to this Pod folder:
sbt_ui_test_tunnel_server_info_plist = 'Pod/Server/' + sbt_ui_test_tunnel_server_bundle_name + '-Info.plist'
apple_binary(
name = sbt_ui_test_tunnel_common_binary_name,
visibility = ['PUBLIC'],
module_name = sbt_ui_test_tunnel_common_module_name,
swift_version = '5',
linker_flags = [
'-ObjC', # Have tried with and without this
],
link_style = 'shared',
exported_headers = glob([
'Pod/Common/**/*.h',
]),
srcs = glob([
'Pod/Common/**/*.swift',
'Pod/Common/**/*.m',
]),
deps = [
'//Path/GCDWebServer:GCDWebServer',
]
)
apple_bundle(
name = sbt_ui_test_tunnel_common_bundle_name,
visibility = ['PUBLIC'],
product_name = sbt_ui_test_tunnel_common_product_name,
binary = ':' + sbt_ui_test_tunnel_common_binary_name,
extension = 'framework',
xcode_product_type = 'com.apple.product-type.framework',
info_plist = sbt_ui_test_tunnel_common_info_plist,
)
apple_binary(
name = sbt_ui_test_tunnel_client_binary_name,
visibility = ['PUBLIC'],
module_name = sbt_ui_test_tunnel_client_module_name,
swift_version = '5',
linker_flags = [
'-ObjC', # Have tried with and without this
],
link_style = 'shared',
exported_headers = glob([
'Pod/Client/**/*.h',
]),
srcs = glob([
'Pod/Client/**/*.swift',
'Pod/Client/**/*.m',
]),
deps = [
'//Path/GCDWebServer:GCDWebServer',
'//Path/SBTUITestTunnel:' + sbt_ui_test_tunnel_common_binary_name,
]
)
apple_bundle(
name = sbt_ui_test_tunnel_client_bundle_name,
visibility = ['PUBLIC'],
product_name = sbt_ui_test_tunnel_client_product_name,
binary = ':' + sbt_ui_test_tunnel_client_binary_name,
extension = 'framework',
xcode_product_type = 'com.apple.product-type.framework',
info_plist = sbt_ui_test_tunnel_client_info_plist,
)
apple_binary(
name = sbt_ui_test_tunnel_server_binary_name,
visibility = ['PUBLIC'],
module_name = sbt_ui_test_tunnel_server_module_name,
swift_version = '5',
enter code here
linker_flags = [
'-ObjC', # Have tried with and without this
],
link_style = 'shared',
exported_headers = glob([
'Pod/Server/**/*.h',
]),
srcs = glob([
'Pod/Server/**/*.swift',
'Pod/Server/**/*.m',
]),
deps = [
'//Path/GCDWebServer:GCDWebServer',
'//Path/SBTUITestTunnel:' + sbt_ui_test_tunnel_common_binary_name,
]
)
apple_bundle(
name = sbt_ui_test_tunnel_server_bundle_name,
visibility = ['PUBLIC'],
product_name = sbt_ui_test_tunnel_server_product_name,
binary = ':' + sbt_ui_test_tunnel_server_binary_name,
extension = 'framework',
xcode_product_type = 'com.apple.product-type.framework',
info_plist = sbt_ui_test_tunnel_server_info_plist,
)
Result:
Buck build fails with this error message:
Command failed with exit code 1.
stderr: ld: warning: -sdk_version and -platform_version are not compatible, ignoring -
sdk_version
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Attempt 3 - Building as prebuilt_apple_frameworks:
Code:
sbt_ui_test_tunnel_common_framework_name = 'SBTUITestTunnelCommon'
# Copied .framework file from build output of the Example app to this Frameworks folder:
sbt_ui_test_tunnel_common_framework_path = 'Frameworks/' + sbt_ui_test_tunnel_common_framework_name + '/' + sbt_ui_test_tunnel_common_framework_name + '.framework'
sbt_ui_test_tunnel_client_framework_name = 'SBTUITestTunnelClient'
# Copied .framework file from build output of the Example app to this Frameworks folder:
sbt_ui_test_tunnel_client_framework_path = 'Frameworks/' + sbt_ui_test_tunnel_common_framework_name + '/' + sbt_ui_test_tunnel_common_framework_name + '.framework'
sbt_ui_test_tunnel_server_framework_name = 'SBTUITestTunnelServer'
# Copied .framework file from build output of the Example app to this Frameworks folder:
sbt_ui_test_tunnel_server_framework_path = 'Frameworks/' + sbt_ui_test_tunnel_common_framework_name + '/' + sbt_ui_test_tunnel_common_framework_name + '.framework'
prebuilt_apple_framework(
name = sbt_ui_test_tunnel_common_framework_name,
framework = sbt_ui_test_tunnel_common_framework_path,
preferred_linkage = 'stat',
visibility = [
'PUBLIC'
],
deps = [
'//Path/GCDWebServer:GCDWebServer',
],
)
prebuilt_apple_framework(
name = sbt_ui_test_tunnel_client_framework_name,
framework = sbt_ui_test_tunnel_client_framework_path,
preferred_linkage = 'shared',
visibility = [
'PUBLIC'
],
deps = [
'//Path/GCDWebServer:GCDWebServer',
'//Path/SBTUITestTunnel:' + sbt_ui_test_tunnel_common_framework_name,
],
)
prebuilt_apple_framework(
name = sbt_ui_test_tunnel_server_framework_name,
framework = sbt_ui_test_tunnel_server_framework_path,
preferred_linkage = 'shared',
visibility = [
'PUBLIC'
],
deps = [
'//Path/GCDWebServer:GCDWebServer',
'//Path/SBTUITestTunnel:' + sbt_ui_test_tunnel_common_framework_name,
],
)
Result:
Buck build fails with this error message:
Command failed with exit code 1.
stderr: /Users/me/path/BridgingHeader.h:12:9: error: 'SBTUITestTunnelServer/SBTUITestTunnelServer.h' file not found
#import <SBTUITestTunnelServer/SBTUITestTunnelServer.h>
^
<unknown>:0: error: failed to import bridging header 'path/BridgingHeader.h'
Additional Notes:
I've followed all steps in the docs, including setting the DEBUG/DEBUG=1 settings and adding the necessary dependencies in the BridgingHeaders.h files like so:
In the Bridging Header file for the library that is to be tested:
#import <SBTUITestTunnelCommon/SBTUITestTunnelCommon.h>
#import <SBTUITestTunnelServer/SBTUITestTunnelServer.h>
And in the Bridging Header file for the library that has the tests:
#import <SBTUITestTunnelClient/SBTUITunneledApplication.h>
#import <SBTUITestTunnelClient/XCTestCase+AppExtension.h>
#import <SBTUITestTunnelCommon/SBTUITestTunnelCommon.h> // Have tried with and without this line
I included the SBTUITestTunnel libraries/frameworks as dependencies in my main app's BUCK file like this:
apple_library(
...
bridging_header = 'BridgingHeader.h',
deps = [
'//Path/SBTUITestTunnel:SBTUITestTunnelCommon',
'//Path/SBTUITestTunnel:SBTUITestTunnelServer',
'//Path/GCDWebServer:GCDWebServer',
]
)
apple_test(
...
bridging_header = 'UITests/BridgingHeader.h',
deps = [
'//Path/SBTUITestTunnel:SBTUITestTunnelClient',
]
)
SBTUITestTunnel depends on another library, GCDWebServer, which also does not provide a BUCK file. However, I have been able to get that one working with using this:
apple_library(
name = 'GCDWebServer',
visibility = ['PUBLIC'],
swift_version = '5',
exported_headers = glob([
'GCDWebServer/**/*.h',
]),
srcs = glob([
'GCDWebServer/**/*.swift',
'GCDWebServer/**/*.m',
])
)
apple_library(
name = 'GCDWebDAVServer',
visibility = ['PUBLIC'],
swift_version = '5',
exported_headers = glob([
'GCDWebDAVServer/**/*.h',
]),
srcs = glob([
'GCDWebDAVServer/*.swift',
'GCDWebDAVServer/*.m',
]),
deps = [
'//Path/GCDWebServer:GCDWebServer',
]
)
apple_library(
name = 'GCDWebUploader',
visibility = ['PUBLIC'],
swift_version = '5',
exported_headers = glob([
'GCDWebUploader/**/*.h',
]),
srcs = glob([
'GCDWebUploader/**/*.swift',
'GCDWebUploader/**/*.m',
]),
deps = [
'//Path/GCDWebServer:GCDWebServer',
]
)
Any idea what I am doing wrong? Or at least maybe some advice on which of these options is most likely the best path?

What is the most efficient way to extract/collect files from a list of targets/providers in Bazel?

I'm writing some rules and learning Starlark as I progress.
Assume I have my own provider:
ModularResources = provider(
doc = "Modular resources",
fields = {
"artifactId": "Former Maven artifact id (don't ask me why)",
"srcs": "List of labels (a glob(..) thing)",
},
)
def _modular_resources_impl(ctx):
return ModularResources(
artifactId = ctx.attr.artifactId,
srcs = ctx.attr.srcs,
)
modular_resources = rule(
implementation = _modular_resources_impl,
attrs = {
"artifactId": attr.string(
mandatory = True,
),
"srcs": attr.label_list(
allow_files = True,
mandatory = True,
),
},
)
Then I have a generator rule which requires these:
some_generator = rule(
attrs = {
"deps": attr.label_list(
providers = [ ModularResources ]
),
...
},
...
)
In my implementation I discovered that I need to do a couple of unwraps to get the files:
def _get_files(deps):
result = []
for dep in deps:
for target in dep[ModularResources].srcs:
result += target.files.to_list()
return result
Is there a more efficient way to perform the collection?
As to why I'm doing this, the generator actually needs a special list of files like this:
def _format_files(deps):
formatted = ""
for dep in deps:
for target in dep[ModularResources].srcs:
formatted += ",".join([dep[ModularResources].artifactId + ":" + f.path for f in target.files.to_list()])
return formatted
FWIW, here is an example how this is used:
a/BUILD:
modular_resources(
name = "generator_resources",
srcs = glob(
["some/path/**/*.whatever"],
),
artifactId = "a",
visibility = ["//visibility:public"],
)
b/BUILD:
some_generator(
name = "...",
deps = [
"//a:generator_resources"
]
)
If you want to trade memory for better performance, maybe the operation can more easily be parallelised by blaze if it's done in the provider instead:
def _modular_resources_impl(ctx):
return ModularResources(
artifactId = ctx.attr.artifactId,
formatted_srcs = ",".join([artifactId + ":" + f.path for f in ctx.files.src])
)

bazel print the versions/tags/hash for all dependencies of a target

I am running into a build error for a bazel target. I have checked the code and I could not find anything wrong. I suspect I might be looking at the wrong version of the code. Is there a way to print out tag/versions/hash of the all the code packages a target is dependent on?
bazel query 'deps(//my:target)' --nohost_deps --noimplicit_deps --output=build
This will print out the BUILD targets of all explicit dependencies a target depends on in the BUILD file format. Here's an example output of running that command in a real project:
# /home/user/code/rules_jvm_external/tests/integration/BUILD:12:1
java_test(
name = "GlobalArtifactExclusionsTest",
deps = ["#global_exclusion_testing//:com_diffplug_durian_durian_core", "#global_exclusion_testing//:com_google_guava_guava", "#global_exclusion_testing//:com_squareup_okhttp3_okhttp", "#maven//:org_hamcrest_hamcrest", "#maven//:org_hamcrest_hamcrest_core"],
srcs = ["//tests/integration:GlobalArtifactExclusionsTest.java"],
test_class = "com.jvm.external.GlobalArtifactExclusionsTest",
)
# /home/user/.cache/bazel/_bazel_user/8484bc4fff18ee4a905b69a9ddb0e143/external/maven/BUILD:103:1
jvm_import(
name = "org_hamcrest_hamcrest_core",
tags = ["maven_coordinates=org.hamcrest:hamcrest-core:2.1"],
jars = ["#maven//:v1/https/jcenter.bintray.com/org/hamcrest/hamcrest-core/2.1/hamcrest-core-2.1.jar"],
deps = ["#maven//:org_hamcrest_hamcrest"],
)
# /home/user/.cache/bazel/_bazel_user/8484bc4fff18ee4a905b69a9ddb0e143/external/maven/BUILD:115:1
jvm_import(
name = "org_hamcrest_hamcrest",
tags = ["maven_coordinates=org.hamcrest:hamcrest:2.1"],
jars = ["#maven//:v1/https/jcenter.bintray.com/org/hamcrest/hamcrest/2.1/hamcrest-2.1.jar"],
deps = [],
)
# /home/user/.cache/bazel/_bazel_user/8484bc4fff18ee4a905b69a9ddb0e143/external/global_exclusion_testing/BUILD:79:1
jvm_import(
name = "com_squareup_okhttp3_okhttp",
tags = ["maven_coordinates=com.squareup.okhttp3:okhttp:3.14.1"],
jars = ["#global_exclusion_testing//:v1/https/repo1.maven.org/maven2/com/squareup/okhttp3/okhttp/3.14.1/okhttp-3.14.1.jar"],
deps = ["#global_exclusion_testing//:com_squareup_okio_okio"],
)
# /home/user/.cache/bazel/_bazel_user/8484bc4fff18ee4a905b69a9ddb0e143/external/global_exclusion_testing/BUILD:91:1
jvm_import(
name = "com_squareup_okio_okio",
tags = ["maven_coordinates=com.squareup.okio:okio:1.17.2"],
jars = ["#global_exclusion_testing//:v1/https/repo1.maven.org/maven2/com/squareup/okio/okio/1.17.2/okio-1.17.2.jar"],
deps = [],
)
# /home/user/.cache/bazel/_bazel_user/8484bc4fff18ee4a905b69a9ddb0e143/external/global_exclusion_testing/BUILD:52:1
jvm_import(
name = "com_google_guava_guava",
tags = ["maven_coordinates=com.google.guava:guava:27.0-jre"],
jars = ["#global_exclusion_testing//:v1/https/repo1.maven.org/maven2/com/google/guava/guava/27.0-jre/guava-27.0-jre.jar"],
deps = ["#global_exclusion_testing//:com_google_guava_listenablefuture", "#global_exclusion_testing//:com_google_code_findbugs_jsr305", "#global_exclusion_testing//:com_google_guava_failureaccess", "#global_exclusion_testing//:com_google_errorprone_error_prone_annotations", "#global_exclusion_testing//:org_checkerframework_checker_qual"],
)
# /home/user/.cache/bazel/_bazel_user/8484bc4fff18ee4a905b69a9ddb0e143/external/global_exclusion_testing/BUILD:102:1
jvm_import(
name = "org_checkerframework_checker_qual",
tags = ["maven_coordinates=org.checkerframework:checker-qual:2.5.2"],
jars = ["#global_exclusion_testing//:v1/https/repo1.maven.org/maven2/org/checkerframework/checker-qual/2.5.2/checker-qual-2.5.2.jar"],
deps = [],
)
# /home/user/.cache/bazel/_bazel_user/8484bc4fff18ee4a905b69a9ddb0e143/external/global_exclusion_testing/BUILD:68:1
jvm_import(
name = "com_google_guava_listenablefuture",
tags = ["maven_coordinates=com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava"],
jars = ["#global_exclusion_testing//:v1/https/repo1.maven.org/maven2/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar"],
deps = [],
)
# /home/user/.cache/bazel/_bazel_user/8484bc4fff18ee4a905b69a9ddb0e143/external/global_exclusion_testing/BUILD:41:1
jvm_import(
name = "com_google_guava_failureaccess",
tags = ["maven_coordinates=com.google.guava:failureaccess:1.0"],
jars = ["#global_exclusion_testing//:v1/https/repo1.maven.org/maven2/com/google/guava/failureaccess/1.0/failureaccess-1.0.jar"],
deps = [],
)
# /home/user/.cache/bazel/_bazel_user/8484bc4fff18ee4a905b69a9ddb0e143/external/global_exclusion_testing/BUILD:30:1
jvm_import(
name = "com_google_errorprone_error_prone_annotations",
tags = ["maven_coordinates=com.google.errorprone:error_prone_annotations:2.2.0"],
jars = ["#global_exclusion_testing//:v1/https/repo1.maven.org/maven2/com/google/errorprone/error_prone_annotations/2.2.0/error_prone_annotations-2.2.0.jar"],
deps = [],
)
# /home/user/.cache/bazel/_bazel_user/8484bc4fff18ee4a905b69a9ddb0e143/external/global_exclusion_testing/BUILD:19:1
jvm_import(
name = "com_google_code_findbugs_jsr305",
tags = ["maven_coordinates=com.google.code.findbugs:jsr305:3.0.2"],
jars = ["#global_exclusion_testing//:v1/https/repo1.maven.org/maven2/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar"],
deps = [],
)
# /home/user/.cache/bazel/_bazel_user/8484bc4fff18ee4a905b69a9ddb0e143/external/global_exclusion_testing/BUILD:8:1
jvm_import(
name = "com_diffplug_durian_durian_core",
tags = ["maven_coordinates=com.diffplug.durian:durian-core:1.2.0"],
jars = ["#global_exclusion_testing//:v1/https/repo1.maven.org/maven2/com/diffplug/durian/durian-core/1.2.0/durian-core-1.2.0.jar"],
deps = [],
)

How to organize third-party java_library rules?

I have many BUILD files that require Jetty or other common Java libraries (SLF4J, Lucene, Guava, etc.). Each of these has a set of other JARs that it references. I would like to know the best practice for organizing these declarations and their dependencies in a large project.
For example, using generate_workspace via bazel run //src/tools/generate_workspace -- --artifact=org.eclipse.jetty:jetty-server:9.3.8.v20160314 I get the following BUILD
# The following dependencies were calculated from:
# org.eclipse.jetty:jetty-server:9.3.8.v20160314
java_library(
name = "org_eclipse_jetty_jetty_http",
visibility = ["//visibility:public"],
exports = [
"#org_eclipse_jetty_jetty_http//jar",
"#org_eclipse_jetty_jetty_util//jar",
],
)
java_library(
name = "org_eclipse_jetty_jetty_util",
visibility = ["//visibility:public"],
exports = [
"#org_eclipse_jetty_jetty_util//jar",
],
)
java_library(
name = "javax_servlet_javax_servlet_api",
visibility = ["//visibility:public"],
exports = [
"#javax_servlet_javax_servlet_api//jar",
],
)
java_library(
name = "org_eclipse_jetty_jetty_server",
visibility = ["//visibility:public"],
exports = [
"#org_eclipse_jetty_jetty_server//jar",
"#javax_servlet_javax_servlet_api//jar",
"#org_eclipse_jetty_jetty_http//jar",
"#org_eclipse_jetty_jetty_io//jar",
"#org_eclipse_jetty_jetty_util//jar",
],
)
java_library(
name = "org_eclipse_jetty_jetty_io",
visibility = ["//visibility:public"],
exports = [
"#org_eclipse_jetty_jetty_io//jar",
"#org_eclipse_jetty_jetty_util//jar",
],
)
and WORKSPACE
# The following dependencies were calculated from:
# org.eclipse.jetty:jetty-server:9.3.8.v20160314
# org.eclipse.jetty:jetty-server:jar:9.3.8.v20160314
maven_jar(
name = "org_eclipse_jetty_jetty_http",
artifact = "org.eclipse.jetty:jetty-http:9.3.8.v20160314",
)
# org.eclipse.jetty:jetty-http:jar:9.3.8.v20160314
# org.eclipse.jetty:jetty-io:jar:9.3.8.v20160314
maven_jar(
name = "org_eclipse_jetty_jetty_util",
artifact = "org.eclipse.jetty:jetty-util:9.3.8.v20160314",
)
# org.eclipse.jetty:jetty-server:jar:9.3.8.v20160314
maven_jar(
name = "javax_servlet_javax_servlet_api",
artifact = "javax.servlet:javax.servlet-api:3.1.0",
)
maven_jar(
name = "org_eclipse_jetty_jetty_server",
artifact = "org.eclipse.jetty:jetty-server:9.3.8.v20160314",
)
# org.eclipse.jetty:jetty-server:jar:9.3.8.v20160314
maven_jar(
name = "org_eclipse_jetty_jetty_io",
artifact = "org.eclipse.jetty:jetty-io:9.3.8.v20160314",
)
files.
I have a dependency on jetty-server and jetty-util in many projects. Is there a better practice than repeating this information in each BUILD file?
Generally you'd put the generate_workspace-generated BUILD file in the root of your workspace (next to your WORKSPACE file) and then, in other BUILD files, you'd reference whatever target they needed to depend on. For example, in src/main/java/com/your-project/subcomponent/BUILD, you might say:
java_library(
name = "my-servlet",
srcs = glob(["*.java"]),
deps = [
"//:javax_servlet_javax_servlet_api",
# other deps...
],
)

Resources