lib/lucene: merge jars using a java_binary rule

In I42fd1a130e ("Merge Lucene core and backward-codecs jars"), Dave
Borowitz introduced a script to concatenate META-INF/services/ files
for Codecs.

Buck would randomly pick one of the two service files. The solution
was to explicitly pick the first entry for class files, and
concatenate service files.

In Bazel, we can have Bazel do the work of merging jars. This is done
with the java_binary rule. This concatenates the service files, and
picks a single .class file from each input jar, as determined by the
ordering in the jars attribute.

Delete tools/merge_jars.py, as this was its only use. This also fixes
an issue where Mac users need to specify --host_force_python=PY2 if
they don't have Python 3.

Change-Id: Ibe62917e20eeb1824967782d4d510f3f63775fce
This commit is contained in:
Han-Wen Nienhuys
2020-01-08 17:14:57 +01:00
committed by David Ostrovsky
parent 8790fa9a07
commit 1fdd3f7b27
3 changed files with 18 additions and 64 deletions

View File

@@ -1,17 +1,28 @@
load("@rules_java//java:defs.bzl", "java_library")
load("@rules_java//java:defs.bzl", "java_binary", "java_import", "java_library")
load("//tools/bzl:maven.bzl", "merge_maven_jars")
package(default_visibility = ["//visibility:public"])
# core and backward-codecs both provide
# META-INF/services/org.apache.lucene.codecs.Codec, so they must be merged.
merge_maven_jars(
name = "lucene-core-and-backward-codecs",
srcs = [
# Merge jars so
# META-INF/services/org.apache.lucene.codecs.Codec
# contains the union of both Codec collections.
java_binary(
name = "lucene-core-and-backward-codecs-merged",
data = ["//lib:LICENSE-Apache2.0"],
main_class = "NotImportant",
runtime_deps = [
# in case of conflict, we want the implementation of backwards-codecs
# first.
"@backward-codecs//jar",
"@lucene-core//jar",
],
data = ["//lib:LICENSE-Apache2.0"],
)
java_import(
name = "lucene-core-and-backward-codecs",
jars = [
":lucene-core-and-backward-codecs-merged_deploy.jar",
],
)
java_library(

View File

@@ -4,17 +4,9 @@ load(
"default_java_toolchain",
)
load("@rules_java//java:defs.bzl", "java_package_configuration")
load("@rules_python//python:defs.bzl", "py_binary")
exports_files(["nongoogle.bzl"])
py_binary(
name = "merge_jars",
srcs = ["merge_jars.py"],
main = "merge_jars.py",
visibility = ["//visibility:public"],
)
default_java_toolchain(
name = "error_prone_warnings_toolchain",
bootclasspath = ["@bazel_tools//tools/jdk:platformclasspath.jar"],

View File

@@ -1,49 +0,0 @@
#!/usr/bin/env python
# Copyright (C) 2015 The Android Open Source Project
#
# 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.
from __future__ import print_function
import collections
import sys
import zipfile
if len(sys.argv) < 3:
print('usage: %s <out.zip> <in.zip>...' % sys.argv[0], file=sys.stderr)
exit(1)
outfile = sys.argv[1]
infiles = sys.argv[2:]
seen = set()
SERVICES = 'META-INF/services/'
try:
with zipfile.ZipFile(outfile, 'w') as outzip:
services = collections.defaultdict(lambda: '')
for infile in infiles:
with zipfile.ZipFile(infile) as inzip:
for info in inzip.infolist():
n = info.filename
if n in seen:
continue
elif n.startswith(SERVICES):
# Concatenate all provider configuration files.
services[n] += inzip.read(n).decode("UTF-8")
continue
outzip.writestr(info, inzip.read(n))
seen.add(n)
for n, v in list(services.items()):
outzip.writestr(n, v)
except Exception as err:
exit('Failed to merge jars: %s' % err)