java_common

Utilities for Java compilation support in Starlark.

compile

JavaInfo java_common.compile(ctx, *, source_jars=[], source_files=[], output, output_source_jar=None, javac_opts=[], deps=[], exports=[], plugins=[], exported_plugins=[], strict_deps='ERROR', java_toolchain, host_javabase, sourcepath=[], resources=[], neverlink=False)

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.

Parameters

Parameter Description
ctx

ctx

The rule context.

source_jars

sequence of Files

A list of the jars to be compiled. At least one of source_jars or source_files should be specified.

source_files

sequence of Files

A list of the Java source files to be compiled. At least one of source_jars or source_files should be specified.

output

File

output_source_jar

File

The output source jar. Optional. Defaults to `{output_jar}-src.jar` if unset.

javac_opts

sequence of strings

A list of the desired javac options. Optional.

deps

sequence of JavaInfos

A list of dependencies. Optional.

exports

sequence of JavaInfos

A list of exports. Optional.

plugins

sequence of JavaInfos

A list of plugins. Optional.

exported_plugins

sequence of JavaInfos

A list of exported plugins. Optional.

strict_deps

string

A string that specifies how to handle strict deps. Possible values: 'OFF', 'ERROR', 'WARN' and 'DEFAULT'. For more details see https://docs.bazel.build/versions/master/bazel-user-manual.html#flag--strict_java_deps. By default 'ERROR'.

java_toolchain

Target; or ToolchainInfo

A JavaToolchainInfo to be used for this compilation. Mandatory.

host_javabase

Target; or JavaRuntimeInfo

A JavaRuntimeInfo to be used for this compilation. Mandatory.

sourcepath

sequence of Files

resources

sequence of Files

bool

create_provider

JavaInfo java_common.create_provider(actions=None, *, compile_time_jars=[], runtime_jars=[], use_ijar=True, java_toolchain=None, transitive_compile_time_jars=[], transitive_runtime_jars=[], source_jars=[])

This API is deprecated. It will be disabled by default in Bazel 0.23. Please use JavaInfo() instead.Creates a JavaInfo from jars. compile_time/runtime_jars are the outputs of the target providing a JavaInfo, while transitive_*_jars represent their dependencies.

Note: compile_time_jars and runtime_jars are not automatically merged into the transitive jars (unless the given transitive_*_jars are empty) - if this is the desired behaviour the user should merge the jars before creating the provider.

This function also creates actions to generate interface jars by default.

When use_ijar is True, ijar will be run on the given compile_time_jars and the resulting interface jars will be stored as compile_jars, while the initial jars will be stored as full_compile_jars.

When use_ijar=False, the given compile_time_jars will be stored as both compile_jars and full_compile_jars. No actions are created. See JavaInfo#compile_jars and JavaInfo#full_compile_jars for more details.

Currently only ijar is supported for generating interface jars. Header compilation is not yet supported.

Parameters

Parameter Description
actions

actions

The ctx.actions object, used to register the actions for creating the interface jars. Only set if use_ijar=True.

compile_time_jars

sequence of Files; or depset of Files

A list or a set of jars that should be used at compilation for a given target.

runtime_jars

sequence of Files; or depset of Files

A list or a set of jars that should be used at runtime for a given target.

use_ijar

bool

If True it will generate interface jars for every jar in compile_time_jars.The generating interface jars will be stored as compile_jars and the initial (full) compile_time_jars will be stored as full_compile_jars. If False the given compile_jars will be stored as both compile_jars and full_compile_jars.

java_toolchain

Target; or ToolchainInfo

A JavaToolchainInfo to be used for retrieving the ijar tool. Only set when use_ijar is True.

transitive_compile_time_jars

sequence of Files; or depset of Files

A list or set of compile time jars collected from the transitive closure of a rule.

transitive_runtime_jars

sequence of Files; or depset of Files

A list or set of runtime jars collected from the transitive closure of a rule.

source_jars

sequence of Files; or depset of Files

A list or set of output source jars that contain the uncompiled source files including the source files generated by annotation processors if the case.

JavaRuntimeInfo

Provider java_common.JavaRuntimeInfo

The key used to retrieve the provider that contains information about the Java runtime being used.

JavaToolchainInfo

Provider java_common.JavaToolchainInfo

The key used to retrieve the provider that contains information about the Java toolchain being used.

make_non_strict

JavaInfo java_common.make_non_strict(java_info)

Returns a new Java provider whose direct-jars part is the union of both the direct and indirect jars of the given Java provider.

Parameters

Parameter Description
java_info

JavaInfo

The java info.

merge

JavaInfo java_common.merge(providers)

Merges the given providers into a single JavaInfo.

Parameters

Parameter Description
providers

sequence of JavaInfos

The list of providers to merge.

pack_sources

File java_common.pack_sources(actions, *, output_jar, sources=[], source_jars=[], java_toolchain, host_javabase)

Packs sources and source jars into a single source jar file. The return value is typically passed to

JavaInfo#source_jar

.

Parameters

Parameter Description
actions

actions

ctx.actions

output_jar

File

The output jar of the rule. Used to name the resulting source jar.

sources

sequence of Files

A list of Java source files to be packed into the source jar.

source_jars

sequence of Files

A list of source jars to be packed into the source jar.

java_toolchain

Target; or ToolchainInfo

A JavaToolchainInfo to used to find the ijar tool.

host_javabase

Target; or JavaRuntimeInfo

A JavaRuntimeInfo to be used for packing sources.

May return None.

provider

Provider java_common.provider

Returns the Java declared provider.
The same value is accessible as JavaInfo.
Prefer using JavaInfo in new code.

run_ijar

File java_common.run_ijar(actions, *, jar, target_label=None, java_toolchain)

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 JavaInfo#compile_jar.

Parameters

Parameter Description
actions

actions

ctx.actions

jar

File

The jar to run ijar on.

target_label

Label

A target label to stamp the jar with. Used for add_dep support. Typically, you would pass ctx.label to stamp the jar with the current rule's label.

java_toolchain

Target; or ToolchainInfo

A JavaToolchainInfo to used to find the ijar tool.

stamp_jar

File java_common.stamp_jar(actions, *, jar, target_label, java_toolchain)

Stamps a jar with a target label for add_dep support. The return value is typically passed to JavaInfo#compile_jar. Prefer to use run_ijar when possible.

Parameters

Parameter Description
actions

actions

ctx.actions

jar

File

The jar to run stamp_jar on.

target_label

Label

A target label to stamp the jar with. Used for add_dep support. Typically, you would pass ctx.label to stamp the jar with the current rule's label.

java_toolchain

Target; or ToolchainInfo

A JavaToolchainInfo to used to find the stamp_jar tool.