summaryrefslogtreecommitdiffstats
path: root/tools/maven/package.bzl
blob: eacb02b9f4be997f2d0ba2e7730e039a621c7d3c (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
111
112
113
114
115
116
117
118
# Copyright (C) 2016 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.

sh_bang_template = (" && ".join([
    "echo '#!/usr/bin/env bash' > $@",
    "echo \"# this script should run from the root of your workspace.\" >> $@",
    "echo \"set -e\" >> $@",
    "echo \"\" >> $@",
    "echo 'function bazel_cmd() {' >> $@",
    "echo '  if [[ `which bazelisk` ]]; then' >> $@",
    "echo '    bazelisk \"$$@\"' >> $@",
    "echo '  else' >> $@",
    "echo '    bazel \"$$@\"' >> $@",
    "echo '  fi' >> $@",
    "echo '}' >> $@",
    "echo \"\" >> $@",
    "echo 'if [[ \"$$VERBOSE\" ]]; then set -x ; fi' >> $@",
    "echo \"\" >> $@",
    "echo %s >> $@",
    "echo \"\" >> $@",
    "echo %s >> $@",
]))

def maven_package(
        version,
        repository = None,
        url = None,
        jar = {},
        src = {},
        doc = {},
        war = {}):
    build_cmd = ["bazel_cmd", "build", "--java_toolchain=//tools:error_prone_warnings_toolchain_java11"]
    mvn_cmd = ["python", "tools/maven/mvn.py", "-v", version]
    api_cmd = mvn_cmd[:]
    api_targets = []
    for type, d in [("jar", jar), ("java-source", src), ("javadoc", doc)]:
        for a, t in sorted(d.items()):
            api_cmd.append("-s %s:%s:$(location %s)" % (a, type, t))
            api_targets.append(t)

    native.genrule(
        name = "gen_api_install",
        cmd = sh_bang_template % (
            " ".join(build_cmd + api_targets),
            " ".join(api_cmd + ["-a", "install"]),
        ),
        srcs = api_targets,
        outs = ["api_install.sh"],
        executable = True,
        testonly = True,
    )

    if repository and url:
        native.genrule(
            name = "gen_api_deploy",
            cmd = sh_bang_template % (
                " ".join(build_cmd + api_targets),
                " ".join(api_cmd + [
                    "-a",
                    "deploy",
                    "--repository",
                    repository,
                    "--url",
                    url,
                ]),
            ),
            srcs = api_targets,
            outs = ["api_deploy.sh"],
            executable = True,
            testonly = True,
        )

    war_cmd = mvn_cmd[:]
    war_targets = []
    for a, t in sorted(war.items()):
        war_cmd.append("-s %s:war:$(location %s)" % (a, t))
        war_targets.append(t)

    native.genrule(
        name = "gen_war_install",
        cmd = sh_bang_template % (
            " ".join(build_cmd + war_targets),
            " ".join(war_cmd + ["-a", "install"]),
        ),
        srcs = war_targets,
        outs = ["war_install.sh"],
        executable = True,
    )

    if repository and url:
        native.genrule(
            name = "gen_war_deploy",
            cmd = sh_bang_template % (
                " ".join(build_cmd + war_targets),
                " ".join(war_cmd + [
                    "-a",
                    "deploy",
                    "--repository",
                    repository,
                    "--url",
                    url,
                ]),
            ),
            srcs = war_targets,
            outs = ["war_deploy.sh"],
            executable = True,
        )