docs/versions/8.2.0/rules/lib/toplevel/java_common.mdx
{% dynamic setvar source_file "src/main/java/com/google/devtools/build/lib/starlarkbuildapi/java/JavaCommonApi.java" %} {% dynamic setvar version "8.2.0" %} {% dynamic setvar original_path "/rules/lib/toplevel/java_common" %} {% include "_buttons.html" %}
<!-- {% raw %} -->Utilities for Java compilation support in Starlark.
<h2>Members</h2> <ul> <li> <a href="#BootClassPathInfo">BootClassPathInfo</a> </li> <li> <a href="#compile">compile</a> </li> <li> <a href="#JavaRuntimeInfo">JavaRuntimeInfo</a> </li> <li> <a href="#JavaToolchainInfo">JavaToolchainInfo</a> </li> <li> <a href="#merge">merge</a> </li> <li> <a href="#pack_sources">pack_sources</a> </li> <li> <a href="#run_ijar">run_ijar</a> </li> <li> <a href="#stamp_jar">stamp_jar</a> </li> </ul> <h2 id="BootClassPathInfo">BootClassPathInfo</h2>
<p><pre class="rule-signature"><a class="anchor" href="../builtins/Provider.html">Provider</a> java_common.BootClassPathInfo</pre></p>
The provider used to supply bootclasspath information
<h2 id="compile">compile</h2>
<p><pre class="rule-signature"><a class="anchor" href="../builtins/struct.html">struct</a> java_common.compile(ctx, *, source_jars=[], source_files=[], output, output_source_jar=None, javac_opts=[], deps=[], runtime_deps=[], exports=[], plugins=[], exported_plugins=[], native_libraries=[], annotation_processor_additional_inputs=[], annotation_processor_additional_outputs=[], strict_deps='ERROR', java_toolchain, bootclasspath=None, host_javabase=None, sourcepath=[], resources=[], resource_jars=[], classpath_resources=[], neverlink=False, enable_annotation_processing=True, enable_compile_jar_action=True, add_exports=[], add_opens=[])</pre></p>
Compiles Java source files/jars from the implementation of a Starlark rule and returns a provider that represents the results of the compilation and can be added to the set of providers emitted by this rule.
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="compile.ctx">
<code>ctx</code>
</td>
<td>
<a class="anchor" href="../builtins/ctx.html">ctx</a>;
required
The rule context.
</td>
</tr>
<tr>
<td id="compile.source_jars">
<code>source_jars</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/File.html">File</a>s;
default is <code>[]</code>
A list of the jars to be compiled. At least one of source_jars or source_files should be specified.
</td>
</tr>
<tr>
<td id="compile.source_files">
<code>source_files</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/File.html">File</a>s;
default is <code>[]</code>
A list of the Java source files to be compiled. At least one of source_jars or source_files should be specified.
</td>
</tr>
<tr>
<td id="compile.output">
<code>output</code>
</td>
<td>
<a class="anchor" href="../builtins/File.html">File</a>;
required
</td>
</tr>
<tr>
<td id="compile.output_source_jar">
<code>output_source_jar</code>
</td>
<td>
<a class="anchor" href="../builtins/File.html">File</a>; or <code>None</code>;
default is <code>None</code>
The output source jar. Defaults to `{output_jar}-src.jar` if unset.
</td>
</tr>
<tr>
<td id="compile.javac_opts">
<code>javac_opts</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../core/string.html">string</a>s;
default is <code>[]</code>
A list of the desired javac options.
</td>
</tr>
<tr>
<td id="compile.deps">
<code>deps</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/struct.html">struct</a>s;
default is <code>[]</code>
A list of dependencies.
</td>
</tr>
<tr>
<td id="compile.runtime_deps">
<code>runtime_deps</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/struct.html">struct</a>s;
default is <code>[]</code>
A list of runtime dependencies.
</td>
</tr>
<tr>
<td id="compile.exports">
<code>exports</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/struct.html">struct</a>s;
default is <code>[]</code>
A list of exports.
</td>
</tr>
<tr>
<td id="compile.plugins">
<code>plugins</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/struct.html">struct</a>s; or <a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/struct.html">struct</a>s;
default is <code>[]</code>
A list of plugins.
</td>
</tr>
<tr>
<td id="compile.exported_plugins">
<code>exported_plugins</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/struct.html">struct</a>s; or <a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/struct.html">struct</a>s;
default is <code>[]</code>
A list of exported plugins.
</td>
</tr>
<tr>
<td id="compile.native_libraries">
<code>native_libraries</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../providers/CcInfo.html">CcInfo</a>s;
default is <code>[]</code>
CC native library dependencies that are needed for this library.
</td>
</tr>
<tr>
<td id="compile.annotation_processor_additional_inputs">
<code>annotation_processor_additional_inputs</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/File.html">File</a>s;
default is <code>[]</code>
A list of inputs that the Java compilation action will take in addition to the Java sources for annotation processing.
</td>
</tr>
<tr>
<td id="compile.annotation_processor_additional_outputs">
<code>annotation_processor_additional_outputs</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/File.html">File</a>s;
default is <code>[]</code>
A list of outputs that the Java compilation action will output in addition to the class jar from annotation processing.
</td>
</tr>
<tr>
<td id="compile.strict_deps">
<code>strict_deps</code>
</td>
<td>
<a class="anchor" href="../core/string.html">string</a>;
default is <code>'ERROR'</code>
A string that specifies how to handle strict deps. Possible values: 'OFF', 'ERROR', 'WARN' and 'DEFAULT'. For more details see <a href="/versions/8.2.0/docs/user-manual#flag--strict_java_deps"><code>--strict_java_deps<code> flag</a>. By default 'ERROR'.
</td>
</tr>
<tr>
<td id="compile.java_toolchain">
<code>java_toolchain</code>
</td>
<td>
Info;
required
A JavaToolchainInfo to be used for this compilation. Mandatory.
</td>
</tr>
<tr>
<td id="compile.bootclasspath">
<code>bootclasspath</code>
</td>
<td>
default is <code>None</code>
A BootClassPathInfo to be used for this compilation. If present, overrides the bootclasspath associated with the provided java_toolchain.
</td>
</tr>
<tr>
<td id="compile.host_javabase">
<code>host_javabase</code>
</td>
<td>
default is <code>None</code>
<b>Deprecated</b>. This parameter is deprecated and will be removed soon. Please do not depend on it. It is <i>disabled</i> with <code>--incompatible_java_common_parameters</code>. Use this flag to verify your code is compatible with its imminent removal.
Deprecated: You can drop this parameter (host_javabase is provided with java_toolchain) </td> </tr> <tr> <td id="compile.sourcepath"> <code>sourcepath</code> </td> <td> <a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/File.html">File</a>s; default is <code>[]</code>
</td>
</tr>
<tr>
<td id="compile.resources">
<code>resources</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/File.html">File</a>s;
default is <code>[]</code>
</td>
</tr>
<tr>
<td id="compile.resource_jars">
<code>resource_jars</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/File.html">File</a>s;
default is <code>[]</code>
</td>
</tr>
<tr>
<td id="compile.classpath_resources">
<code>classpath_resources</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/File.html">File</a>s;
default is <code>[]</code>
</td>
</tr>
<tr>
<td id="compile.neverlink">
<code>neverlink</code>
</td>
<td>
<a class="anchor" href="../core/bool.html">bool</a>;
default is <code>False</code>
</td>
</tr>
<tr>
<td id="compile.enable_annotation_processing">
<code>enable_annotation_processing</code>
</td>
<td>
<a class="anchor" href="../core/bool.html">bool</a>;
default is <code>True</code>
Disables annotation processing in this compilation, causing any annotation processors provided in plugins or in exported_plugins of deps to be ignored.
</td>
</tr>
<tr>
<td id="compile.enable_compile_jar_action">
<code>enable_compile_jar_action</code>
</td>
<td>
<a class="anchor" href="../core/bool.html">bool</a>;
default is <code>True</code>
Enables header compilation or ijar creation. If set to False, it forces use of the full class jar in the compilation classpaths of any dependants. Doing so is intended for use by non-library targets such as binaries that do not have dependants.
</td>
</tr>
<tr>
<td id="compile.add_exports">
<code>add_exports</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../core/string.html">string</a>s;
default is <code>[]</code>
Allow this library to access the given <module>/<package>.
</td>
</tr>
<tr>
<td id="compile.add_opens">
<code>add_opens</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../core/string.html">string</a>s;
default is <code>[]</code>
Allow this library to reflectively access the given <module>/<package>.
</td>
</tr>
</tbody>
</table>
<h2 id="JavaRuntimeInfo">JavaRuntimeInfo</h2>
<p><pre class="rule-signature"><a class="anchor" href="../builtins/Provider.html">Provider</a> java_common.JavaRuntimeInfo</pre></p>
The key used to retrieve the provider that contains information about the Java runtime being used.
<h2 id="JavaToolchainInfo">JavaToolchainInfo</h2>
<p><pre class="rule-signature"><a class="anchor" href="../builtins/Provider.html">Provider</a> java_common.JavaToolchainInfo</pre></p>
The key used to retrieve the provider that contains information about the Java toolchain being used.
<h2 id="merge">merge</h2>
<p><pre class="rule-signature"><a class="anchor" href="../builtins/struct.html">struct</a> java_common.merge(providers)</pre></p>
Merges the given providers into a single JavaInfo.
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="merge.providers">
<code>providers</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/struct.html">struct</a>s;
required
The list of providers to merge.
</td>
</tr>
</tbody>
</table>
<h2 id="pack_sources">pack_sources</h2>
<p><pre class="rule-signature"><a class="anchor" href="../builtins/File.html">File</a> java_common.pack_sources(actions, *, output_jar=None, output_source_jar=None, sources=[], source_jars=[], java_toolchain, host_javabase=None)</pre></p>
Packs sources and source jars into a single source jar file. The return value is typically passed to<p><code><a class="anchor" href="../providers/JavaInfo.html">JavaInfo</a>#source_jar</code></p>.At least one of parameters output_jar or output_source_jar is required.
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="pack_sources.actions">
<code>actions</code>
</td>
<td>
<a class="anchor" href="../builtins/actions.html">actions</a>;
required
ctx.actions
</td>
</tr>
<tr>
<td id="pack_sources.output_jar">
<code>output_jar</code>
</td>
<td>
<a class="anchor" href="../builtins/File.html">File</a>; or <code>None</code>;
default is <code>None</code>
<b>Deprecated</b>. This parameter is deprecated and will be removed soon. Please do not depend on it. It is <i>disabled</i> with <code>--incompatible_java_common_parameters</code>. Use this flag to verify your code is compatible with its imminent removal.
Deprecated: The output jar of the rule. Used to name the resulting source jar. The parameter sets output_source_jar parameter to {output_jar}-src.jar.Use output_source_jar parameter directly instead.
</td>
</tr>
<tr>
<td id="pack_sources.output_source_jar">
<code>output_source_jar</code>
</td>
<td>
<a class="anchor" href="../builtins/File.html">File</a>; or <code>None</code>;
default is <code>None</code>
The output source jar.
</td>
</tr>
<tr>
<td id="pack_sources.sources">
<code>sources</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/File.html">File</a>s;
default is <code>[]</code>
A list of Java source files to be packed into the source jar.
</td>
</tr>
<tr>
<td id="pack_sources.source_jars">
<code>source_jars</code>
</td>
<td>
<a class="anchor" href="../core/list.html">sequence</a> of <a class="anchor" href="../builtins/File.html">File</a>s;
default is <code>[]</code>
A list of source jars to be packed into the source jar.
</td>
</tr>
<tr>
<td id="pack_sources.java_toolchain">
<code>java_toolchain</code>
</td>
<td>
Info;
required
A JavaToolchainInfo to used to find the ijar tool.
</td>
</tr>
<tr>
<td id="pack_sources.host_javabase">
<code>host_javabase</code>
</td>
<td>
default is <code>None</code>
<b>Deprecated</b>. This parameter is deprecated and will be removed soon. Please do not depend on it. It is <i>disabled</i> with <code>--incompatible_java_common_parameters</code>. Use this flag to verify your code is compatible with its imminent removal.
Deprecated: You can drop this parameter (host_javabase is provided with java_toolchain) </td> </tr> </tbody> </table>
<h2 id="run_ijar">run_ijar</h2>
<p><pre class="rule-signature"><a class="anchor" href="../builtins/File.html">File</a> java_common.run_ijar(actions, *, jar, target_label=None, java_toolchain)</pre></p>
Runs ijar on a jar, stripping it of its method bodies. This helps reduce rebuilding of dependent jars during any recompiles consisting only of simple changes to method implementations. The return value is typically passed to <code><a class="anchor" href="../providers/JavaInfo.html">JavaInfo</a>#compile_jar</code>.
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="run_ijar.actions">
<code>actions</code>
</td>
<td>
<a class="anchor" href="../builtins/actions.html">actions</a>;
required
ctx.actions
</td>
</tr>
<tr>
<td id="run_ijar.jar">
<code>jar</code>
</td>
<td>
<a class="anchor" href="../builtins/File.html">File</a>;
required
The jar to run ijar on.
</td>
</tr>
<tr>
<td id="run_ijar.target_label">
<code>target_label</code>
</td>
<td>
<a class="anchor" href="../builtins/Label.html">Label</a>; or <code>None</code>;
default is <code>None</code>
A target label to stamp the jar with. Used for <code>add_dep</code> support. Typically, you would pass <code>ctx.label</code> to stamp the jar with the current rule's label.
</td>
</tr>
<tr>
<td id="run_ijar.java_toolchain">
<code>java_toolchain</code>
</td>
<td>
Info;
required
A JavaToolchainInfo to used to find the ijar tool.
</td>
</tr>
</tbody>
</table>
<h2 id="stamp_jar">stamp_jar</h2>
<p><pre class="rule-signature"><a class="anchor" href="../builtins/File.html">File</a> java_common.stamp_jar(actions, *, jar, target_label, java_toolchain)</pre></p>
Stamps a jar with a target label for <code>add_dep</code> support. The return value is typically passed to <code><a class="anchor" href="../providers/JavaInfo.html">JavaInfo</a>#compile_jar</code>. Prefer to use <code><a class="anchor" href="#run_ijar">run_ijar</a></code> when possible.
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="stamp_jar.actions">
<code>actions</code>
</td>
<td>
<a class="anchor" href="../builtins/actions.html">actions</a>;
required
ctx.actions
</td>
</tr>
<tr>
<td id="stamp_jar.jar">
<code>jar</code>
</td>
<td>
<a class="anchor" href="../builtins/File.html">File</a>;
required
The jar to run stamp_jar on.
</td>
</tr>
<tr>
<td id="stamp_jar.target_label">
<code>target_label</code>
</td>
<td>
<a class="anchor" href="../builtins/Label.html">Label</a>;
required
A target label to stamp the jar with. Used for <code>add_dep</code> support. Typically, you would pass <code>ctx.label</code> to stamp the jar with the current rule's label.
</td>
</tr>
<tr>
<td id="stamp_jar.java_toolchain">
<code>java_toolchain</code>
</td>
<td>
Info;
required
A JavaToolchainInfo to used to find the stamp_jar tool.
</td>
</tr>
</tbody>
</table>