aboutsummaryrefslogtreecommitdiff
path: root/java_src/tools/gen_maven_jar_rules.bzl
blob: 7706fb5c23946577eb632a855877633eb10e4855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright 2020 Google LLC
#
# 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.

""" Definition of gen_maven_jar_rules. """

load("//tools:jar_jar.bzl", "jar_jar")
load("//tools:java_single_jar.bzl", "java_single_jar")
load("//tools:javadoc.bzl", "javadoc_library")

_EXTERNAL_JAVADOC_LINKS = [
    "https://docs.oracle.com/javase/8/docs/api/",
    "https://developer.android.com/reference/",
]

_TINK_PACKAGES = [
    "com.google.crypto.tink",
]

def gen_maven_jar_rules(
        name,
        deps = [],
        resources = [],
        root_packages = _TINK_PACKAGES,
        shaded_packages = [],
        shading_rules = "",
        exclude_packages = [],
        doctitle = "",
        android_api_level = 23,
        bottom_text = "",
        external_javadoc_links = _EXTERNAL_JAVADOC_LINKS,
        manifest_lines = []):
    """
    Generates rules that generate Maven jars for a given package.

    Args:
      name: Given a name, this function generates 3 rules: a compiled package
        name.jar, a source package name-src.jar and a Javadoc package
        name-javadoc.jar.
      deps: A combination of the deps of java_single_jar and javadoc_library
      resources: A list of resource files. Files must be stored in
        src/main/resources. Mapping rules: src/main/resources/a/b/c.txt will be
        copied to a/b/c.txt in the output jar.
      root_packages: See javadoc_library
      shaded_packages: These packages will be shaded, according to the rules
        specified in shading_rules.
      shading_rules: The shading rules, must specified when shaded_packages is present.
        Rules file format can be found at https://github.com/bazelbuild/bazel/blob/master/third_party/jarjar/java/com/tonicsystems/jarjar/help.txt.
      exclude_packages: See javadoc_library
      doctitle: See javadoc_library
      android_api_level: See javadoc_library
      bottom_text: See javadoc_library
      external_javadoc_links: See javadoc_library
      manifest_lines: lines to put in the output manifest file (manifest
        files in the input jars are ignored)
    """

    if shaded_packages:
        unshaded_jar = name + "-unshaded"
        java_single_jar(
            name = unshaded_jar,
            deps = deps,
            resources = resources,
            root_packages = root_packages + shaded_packages,
            manifest_lines = manifest_lines,
        )
        jar_jar(
            name = name,
            input_jar = unshaded_jar,
            rules = shading_rules,
        )
    else:
        java_single_jar(
            name = name,
            deps = deps,
            resources = resources,
            root_packages = root_packages,
            manifest_lines = manifest_lines,
        )

    source_jar_name = name + "-src"
    java_single_jar(
        name = source_jar_name,
        deps = deps,
        root_packages = root_packages,
        source_jar = True,
    )

    javadoc_name = name + "-javadoc"
    javadoc_library(
        name = javadoc_name,
        deps = deps,
        root_packages = root_packages,
        srcs = [":%s" % source_jar_name],
        doctitle = doctitle,
        exclude_packages = exclude_packages,
        android_api_level = android_api_level,
        bottom_text = bottom_text,
        external_javadoc_links = external_javadoc_links,
    )