|
| 1 | +# Copyright 2026 The Bazel Authors. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Definition of the java_single_jar rule.""" |
| 15 | + |
| 16 | +load("//java/common:java_common.bzl", "java_common") |
| 17 | +load("//java/common:java_info.bzl", "JavaInfo") |
| 18 | + |
| 19 | +# copybara: default visibility |
| 20 | + |
| 21 | +def _single_jar_inputs(deps, deploy_env): |
| 22 | + transitive_inputs = [] |
| 23 | + for dep in deps: |
| 24 | + if JavaInfo in dep: |
| 25 | + info = dep[JavaInfo] |
| 26 | + transitive_inputs.append(info.transitive_runtime_jars) |
| 27 | + if hasattr(info, "compilation_info"): |
| 28 | + compilation_info = info.compilation_info |
| 29 | + if hasattr(compilation_info, "runtime_classpath"): |
| 30 | + transitive_inputs.append(compilation_info.runtime_classpath) |
| 31 | + else: |
| 32 | + files = [] |
| 33 | + for f in dep[DefaultInfo].files.to_list(): |
| 34 | + if not f.extension == "jar": |
| 35 | + fail("unexpected file type in java_single_jar.deps: %s" % f.path) |
| 36 | + files.append(f) |
| 37 | + transitive_inputs.append(depset(files)) |
| 38 | + inputs = depset(transitive = transitive_inputs) |
| 39 | + |
| 40 | + if hasattr(java_common, "JavaRuntimeClasspathInfo"): |
| 41 | + deploy_env_jars = depset(transitive = [ |
| 42 | + dep[java_common.JavaRuntimeClasspathInfo].runtime_classpath |
| 43 | + for dep in deploy_env |
| 44 | + ]) |
| 45 | + excluded_jars = {jar: None for jar in deploy_env_jars.to_list()} |
| 46 | + if excluded_jars: |
| 47 | + inputs = depset([jar for jar in inputs.to_list() if jar not in excluded_jars]) |
| 48 | + return inputs |
| 49 | + |
| 50 | +def _bazel_java_single_jar_impl(ctx): |
| 51 | + inputs = _single_jar_inputs(ctx.attr.deps, ctx.attr.deploy_env) |
| 52 | + |
| 53 | + args = ctx.actions.args() |
| 54 | + args.add_all("--sources", inputs) |
| 55 | + args.use_param_file("@%s") |
| 56 | + args.set_param_file_format("multiline") |
| 57 | + args.add_all("--deploy_manifest_lines", ctx.attr.deploy_manifest_lines) |
| 58 | + args.add("--output", ctx.outputs.output) |
| 59 | + args.add("--normalize") |
| 60 | + |
| 61 | + # Deal with limitation of singlejar flags: tool's default behavior is |
| 62 | + # "no", but you get that behavior only by absence of compression flags. |
| 63 | + if ctx.attr.compress == "preserve": |
| 64 | + args.add("--dont_change_compression") |
| 65 | + elif ctx.attr.compress == "yes": |
| 66 | + args.add("--compression") |
| 67 | + elif ctx.attr.compress == "no": |
| 68 | + pass |
| 69 | + else: |
| 70 | + fail("\"compress\" attribute (%s) must be: yes, no, preserve." % ctx.attr.compress) |
| 71 | + |
| 72 | + if ctx.attr.exclude_build_data: |
| 73 | + args.add("--exclude_build_data") |
| 74 | + if ctx.attr.multi_release: |
| 75 | + args.add("--multi_release") |
| 76 | + |
| 77 | + if ctx.attr.exclude_pattern: |
| 78 | + args.add("--exclude_pattern", ctx.attr.exclude_pattern) |
| 79 | + |
| 80 | + ctx.actions.run( |
| 81 | + inputs = inputs, |
| 82 | + outputs = [ctx.outputs.output], |
| 83 | + arguments = [args], |
| 84 | + progress_message = "Merging into %s" % ctx.outputs.output.short_path, |
| 85 | + mnemonic = "JavaSingleJar", |
| 86 | + executable = ctx.executable._singlejar, |
| 87 | + use_default_shell_env = True, |
| 88 | + ) |
| 89 | + |
| 90 | + files = depset([ctx.outputs.output]) |
| 91 | + providers = [DefaultInfo( |
| 92 | + files = files, |
| 93 | + runfiles = ctx.runfiles(transitive_files = files), |
| 94 | + )] |
| 95 | + if hasattr(java_common, "JavaRuntimeClasspathInfo"): |
| 96 | + providers.append(java_common.JavaRuntimeClasspathInfo(runtime_classpath = inputs)) |
| 97 | + return providers |
| 98 | + |
| 99 | +bazel_java_single_jar = rule( |
| 100 | + attrs = { |
| 101 | + "deps": attr.label_list( |
| 102 | + allow_files = True, |
| 103 | + doc = """ |
| 104 | + The Java targets (including java_import and java_library) to collect |
| 105 | + transitive dependencies from. Runtime dependencies are collected via |
| 106 | + deps, exports, and runtime_deps. Resources are also collected. |
| 107 | + Native cc_library or java_wrap_cc dependencies are not.""", |
| 108 | + ), |
| 109 | + "deploy_manifest_lines": attr.string_list(doc = """ |
| 110 | + A list of lines to add to the <code>META-INF/manifest.mf</code> file."""), |
| 111 | + "deploy_env": attr.label_list( |
| 112 | + providers = [java_common.JavaRuntimeClasspathInfo] if hasattr(java_common, "JavaRuntimeClasspathInfo") else [], |
| 113 | + allow_files = False, |
| 114 | + doc = """ |
| 115 | + A list of `java_binary` or `java_single_jar` targets which represent |
| 116 | + the deployment environment for this binary. |
| 117 | +
|
| 118 | + Set this attribute when building a plugin which will be loaded by another |
| 119 | + `java_binary`. |
| 120 | +
|
| 121 | + `deploy_env` dependencies are excluded from the jar built by this rule.""", |
| 122 | + ), |
| 123 | + "compress": attr.string(default = "preserve", doc = """ |
| 124 | + Whether to always deflate ("yes"), always store ("no"), or pass |
| 125 | + through unmodified ("preserve"). The default is "preserve", and is the |
| 126 | + most efficient option -- no extra work is done to inflate or deflate."""), |
| 127 | + "exclude_build_data": attr.bool(default = True, doc = """ |
| 128 | + Whether to omit the build-data.properties file generated |
| 129 | + by default."""), |
| 130 | + "multi_release": attr.bool(default = True, doc = """Whether to enable Multi-Release output jars."""), |
| 131 | + "exclude_pattern": attr.string(default = "", doc = """ |
| 132 | + A regex pattern of files to exclude from the jar. |
| 133 | + """), |
| 134 | + "_singlejar": attr.label( |
| 135 | + default = Label("//toolchains:singlejar"), |
| 136 | + cfg = "exec", |
| 137 | + allow_single_file = True, |
| 138 | + executable = True, |
| 139 | + ), |
| 140 | + "output": attr.output(), |
| 141 | + }, |
| 142 | + implementation = _bazel_java_single_jar_impl, |
| 143 | + doc = """ |
| 144 | +Collects Java dependencies and jar files into a single jar |
| 145 | +
|
| 146 | +`java_single_jar` collects Java dependencies and jar files into a single jar. |
| 147 | +This is similar to java_binary with everything related to executables disabled, |
| 148 | +and provides an alternative to the java_binary "deploy jar hack". |
| 149 | +
|
| 150 | +## Example |
| 151 | +
|
| 152 | +```skylark |
| 153 | +load("//tools/build_defs/java_single_jar:java_single_jar.bzl", "java_single_jar") |
| 154 | +
|
| 155 | +java_single_jar( |
| 156 | + name = "my_single_jar", |
| 157 | + deps = [ |
| 158 | + "//java/com/google/foo", |
| 159 | + "//java/com/google/bar", |
| 160 | + ], |
| 161 | +) |
| 162 | +``` |
| 163 | +
|
| 164 | +Outputs: |
| 165 | + {name}.jar: A single jar containing all of the inputs. |
| 166 | +""", |
| 167 | +) |
0 commit comments