java_library
java_library
def java_library(
*,
name: str,
default_target_platform: None | str = None,
target_compatible_with: list[str] = [],
compatible_with: list[str] = [],
exec_compatible_with: list[str] = [],
visibility: list[str] = [],
within_view: list[str] = ["PUBLIC"],
metadata: OpaqueMetadata = {},
tests: list[str] = [],
modifiers: OpaqueMetadata = [],
_apple_platforms: dict[str, str] = {},
_build_only_native_code: bool = select({"prelude//android/constraints:build_only_native_code": True, "DEFAULT": False}),
_dex_min_sdk_version: None | int = select({"prelude//android/constraints:min_sdk_version_19": 19, "prelude//android/constraints:min_sdk_version_20": 20, "prelude//android/constraints:min_sdk_version_21": 21, "prelude//android/constraints:min_sdk_version_22": 22, "prelude//android/constraints:min_sdk_version_23": 23, "prelude//android/constraints:min_sdk_version_24": 24, "prelude//android/constraints:min_sdk_version_25": 25, "prelude//android/constraints:min_sdk_version_26": 26, "prelude//android/constraints:min_sdk_version_27": 27, "prelude//android/constraints:min_sdk_version_28": 28, "prelude//android/constraints:min_sdk_version_29": 29, "prelude//android/constraints:min_sdk_version_30": 30, "prelude//android/constraints:min_sdk_version_31": 31, "prelude//android/constraints:min_sdk_version_32": 32, "prelude//android/constraints:min_sdk_version_33": 33, "prelude//android/constraints:min_sdk_version_34": 34, "prelude//android/constraints:min_sdk_version_35": 35, "DEFAULT": None}),
_dex_toolchain: str = "gh_facebook_buck2_shims_meta//:dex",
_exec_os_type: str = "prelude//os_lookup/targets:os_lookup",
_is_building_android_binary: bool = select({"prelude//os:building_android_binary": True, "DEFAULT": False}),
_java_toolchain: str = "gh_facebook_buck2_shims_meta//:java",
abi_generation_mode: None | str = None,
annotation_processor_deps: list[str] = [],
annotation_processor_params: list[str] = [],
annotation_processors: list[str] = [],
attrs_validators: None | list[str] = None,
concat_resources: bool = False,
contacts: list[str] = [],
default_host_platform: None | str = None,
deps: list[str] = [],
exported_deps: list[str] = [],
exported_provided_deps: list[str] = [],
extra_arguments: list[str] = [],
jar_postprocessor: None | str = None,
java_version: None | str = None,
javac: None | str = None,
labels: list[str] = [],
licenses: list[str] = [],
manifest_file: None | str = None,
maven_coords: None | str = None,
never_mark_as_unused_dependency: None | bool = None,
on_unused_dependencies: None | str = None,
plugins: list[str | (str, list[str])] = [],
proguard_config: None | str = None,
provided_deps: list[str] = [],
remove_classes: list[str] = [],
required_for_source_only_abi: bool = False,
resources: list[str] = [],
resources_root: None | str = None,
runtime_deps: list[str] = [],
source: None | str = None,
source_abi_verification_mode: None | str = None,
source_only_abi_deps: list[str] = [],
srcs: list[str] = [],
target: None | str = None,
validation_deps: list[str] = [],
) -> None
A java_library()
rule defines a set of Java files that can be compiled together. The main output of a java_library()
rule is a single JAR file containing all of the compiled class files, as well as the static files specified in the resources
argument.
Parameters
-
name
: name of the target -
default_target_platform
: specifies the default target platform, used when no platforms are specified on the command line -
target_compatible_with
: a list of constraints that are required to be satisfied for this target to be compatible with a configuration -
compatible_with
: a list of constraints that are required to be satisfied for this target to be compatible with a configuration -
exec_compatible_with
: a list of constraints that are required to be satisfied for this target to be compatible with an execution platform -
visibility
: a list of visibility patterns restricting what targets can depend on this one -
within_view
: a list of visibility patterns restricting what this target can depend on -
metadata
: a key-value map of metadata associated with this target -
tests
: a list of targets that provide tests for this one -
modifiers
: an array of modifiers associated with this target -
concat_resources
: Use parallel compression and concatenation of intermediary jars to speed up jar time generation. -
deps
: Rules (usually otherjava_library
rules) that are used to generate the classpath required to compile thisjava_library
. -
exported_deps
: Other rules that depend on this rule will also include itsexported_deps
in their classpaths. This is useful when the public API of a rule has return types or checked exceptions that are defined in another rule, which would otherwise require callers to add an extra dependency. It's also useful for exposing e.g. a collection ofprebuilt_jar
rules as a single target for callers to depend on. Targets inexported_deps
are implicitly included in thedeps
of this rule, so they don't need to be repeated there. -
exported_provided_deps
: This is a combination ofprovided_deps
andexported_deps
. Rules listed in this parameter will be added to classpath of rules that depend on this rule, but they will not be included in a binary if binary depends on a such target. -
extra_arguments
: List of additional arguments to pass into the Java compiler. These arguments follow the ones specified in.buckconfig
. -
java_version
: Equivalent to setting bothsource
andtarget
to the given value. Setting this andsource
ortarget
(or both!) is an error. -
javac
: Specifies the Java compiler program to use for this rule. The value is a source path or an execution dep (e.g., //foo/bar:bar). Overrides the value in "javac" in the "tools" section of.buckconfig
. -
on_unused_dependencies
: Action performed when Buck detects that some dependencies are not used during Java compilation.Note that this feature is experimental and does not handle runtime dependencies.
The valid values are:
ignore
(default): ignore unused dependencies,warn
: emit a warning to the console,fail
: fail the compilation.
This option overrides the default value from .
-
provided_deps
: These represent dependencies that are known to be provided at run time, but are required in order for the code to compile. Examples ofprovided_deps
include the JEE servlet APIs. When this rule is included in a , theprovided_deps
will not be packaged into the output. -
remove_classes
: Specifies a list ofPatterns
that are used to excludeclasses
from theJAR
. The pattern matching is based on the name of the class. This can be used to exclude a member class or delete a local view of a class that will be replaced during a later stage of the build. -
required_for_source_only_abi
: Indicates that this rule must be present on the classpath duringsource-only ABI generation
of any rule that depends on it. Typically this is done when a rule contains annotations, enums, constants, or interfaces.Having rules present on the classpath during source-only ABI generation prevents Buck from completely flattening the build graph, thus reducing the performance win from source-only ABI generation. These rules should be kept small (ideally just containing annotations, constants, enums, and interfaces) and with minimal dependencies of their own.
-
resources
: Static files to include with the compiled.class
files. These files can be loaded via Class.getResource().Note: If
resources_root
isn't set, Buck uses the.buckconfig
property in.buckconfig
to determine where resources should be placed within the generated JAR file. -
source
: Specifies the version of Java (as a string) to interpret source files as. Overrides the value in "source_level" in the "java" section of.buckconfig
. -
source_only_abi_deps
: These are dependencies that must be present duringsource-only ABI generation
. Typically such dependencies are added when some property of the code in this rule prevents source-only ABI generation from being correct without these dependencies being present.Having
source_only_abi_deps
prevents Buck from completely flattening the build graph, thus reducing the performance win from source-only ABI generation. They should be avoided when possible. Often only a small code change is needed to avoid them. For more information on such code changes, read aboutsource-only ABI generation
. -
srcs
: The set of.java
files to compile for this rule. If any of the files in this list end in.src.zip
, then the entries in the ZIP file that end in.java
will be included as ordinary inputs to compilation. This is common when using agenrule()
to auto-generate some Java source code that needs to be compiled with some hand-written Java code. -
target
: Specifies the version of Java (as a string) for which to generate code. Overrides the value in "target_level" in the "java" section of.buckconfig
.
Details
Examples:
# A rule that compiles a single .java file.
java_library(
name = 'JsonUtil',
srcs = ['JsonUtil.java'],
deps = [
'//third_party/guava:guava',
'//third_party/jackson:jackson',
],
)
# A rule that compiles all of the .java files under the directory in
# which the rule is defined using glob(). It also excludes an
# individual file that may have additional dependencies, so it is
# compiled by a separate rule.
java_library(
name = 'messenger',
srcs = glob(['**/*.java'], excludes = ['MessengerModule.java']),
deps = [
'//src/com/facebook/base:base',
'//third_party/guava:guava',
],
)
java_library(
name = 'MessengerModule',
srcs = ['MessengerModule.java'],
deps = [
'//src/com/facebook/base:base',
'//src/com/google/inject:inject',
'//third_party/guava:guava',
'//third_party/jsr-330:jsr-330',
],
)
# A rule that builds a library with both relative and
# fully-qualified deps.
java_library(
name = 'testutil',
srcs = glob(['tests/**/*.java'], excludes = 'tests/**/*Test.java'),
deps = [
':lib-fb4a',
'//java/com/facebook/base:base',
],
)