Buck: java_library()
Support Ukraine. Help Provide Humanitarian Aid to Ukraine.

java_library()

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.

Arguments

  • name (required) #

    The short name for this build target.

  • srcs (defaults to []) #

    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 a genrule to auto-generate some Java source code that needs to be compiled with some hand-written Java code.

  • resources (defaults to []) #

    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 [java].src_rootsproperty in .buckconfig to determine where resources should be placed within the generated JAR file.

  • resources_root (defaults to None) #

    The path that resources are resolved against. For example, if resources_root is "res" and resources contains the file "res/com/example/foo.txt", that file will end up as "com/example/foo.txt" in the output JAR. This parameter overrides the [java].src_roots property in .buckconfig.

  • deps (defaults to []) #

    Rules (usually other java_library rules) that are used to generate the classpath required to compile this java_library.

  • source (defaults to <global value>) #

    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.

  • target (defaults to <global value>) #

    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.

  • java_version (defaults to <global value>) #

    Equivalent to setting both source and target to the given value. Setting this and source or target (or both!) is an error.

  • javac (defaults to <global value>) #

    Specifies the Java compiler program to use for this rule. The value is a source path (e.g., Only one of "javac" and "javac_jar" may be set for a given rule. Overrides the value in "javac" in the "tools" section of .buckconfig.

  • javac_jar (defaults to <global value>) #

    Specifies the Java compiler program to use for this rule. The value is a source path (e.g., Only one of "javac_jar" and "javac" may be set for a given rule. Overrides the value in "javac_jar" in the "tools" section of .buckconfig.

  • compiler_class_name (defaults to <global value>) #

    Specifies the Java compiler class name to use in tandem with javac_jar. Overrides the value in .buckconfig.

  • extra_arguments (defaults to []) #

    List of additional arguments to pass into the Java compiler. These arguments follow the ones specified in .buckconfig.

  • remove_classes (defaults to []) #

    Specifies a list of Patterns that are used to excludeclasses from the JAR. 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.

  • exported_deps (defaults to []) #

    Other java_library rules that depend on this rule will also include its exported_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 of prebuilt_jar rules as a single target for callers to depend on. Targets in exported_deps are implicitly included in the deps of this rule, so they don't need to be repeated there.

  • provided_deps (defaults to []) #

    These represent dependencies that are known to be provided at run time, but are required in order for the code to compile. Examples of provided_deps include the JEE servlet APIs. When this rule is included in a java_library, the provided_deps will not be packaged into the output.

  • exported_provided_deps (defaults to []) #

    This is a combination of provided_deps and exported_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.

  • abi_generation_mode (defaults to None) #

    Overrides [java].abi_generation_mode for this rule.

  • source_only_abi_deps (defaults to []) #

    These are dependencies that must be present during source-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 about source-only ABI generation.

  • required_for_source_only_abi (defaults to False) #

    Indicates that this rule must be present on the classpath during source-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.

  • on_unused_dependencies (defaults to ignore) #

    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 .buckconfig.

  • tests (defaults to []) #

    List of build targets that identify tests that exercise this target.

  • visibility (defaults to []) #

    List of build target patterns that identify the build rules that can include this rule as a dependency, for example, by listing it in their deps or exported_deps attributes. For more information, see visibility.

  • licenses (defaults to []) #

    Set of license files for this library. To get the list of license files for a given build rule and all of its dependencies, you can use buck query.

  • labels (defaults to []) #

    Set of arbitrary strings which allow you to annotate a build rule with tags that can be searched for over an entire dependency tree using buck query attrfilter().

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',
  ],
)