Buck: java_library()


A java_library() rule is used to define 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 and resources.


  • name (required) #

    The name of the rule.

  • 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 among the compiled .class files. These files can be loaded via Class.getResource().

    Note: If resources_root isn't set, Buck uses the src_roots property in .buckconfig to help 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 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.

  • autodeps (defaults to False) #

    Should be set to True when using buck autodeps to generate the deps for this rule.

  • generated_symbols (defaults to []) #

    When using buck autodeps, it may be necessary to declare types or symbols that are provided by this rule that cannot be statically inferred from the srcs alone. This is most common when you include generated files among the srcs.

    For example, when generating a .java file via a genrule that is wrapped in a java_library, you would want to declare the type of the generated file in the generated_symbols of the java_library.

      name = 'make_java',
      cmd = 'createJavaClass > $OUT',
      # Creates this file in the package com.example.
      out = 'CreatedClass.java',
      name = 'created',
      srcs = [ ':make_java' ],
      generated_symbols = [
    In this way, if buck autodeps sees a reference to com.example.CreatedClass in the srcs of another java_library rule, it would know to add :created to that rule's deps.

  • 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_binary, the provided_deps will not be packaged into the output.

  • tests (defaults to []) #

    List of build targets that identify the test rules that exercise this target.

  • visibility (defaults to []) #

    List of build target patterns that identify the build rules that can include this rule in its deps.

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


# A rule that compiles a single .java file.
  name = 'JsonUtil',
  srcs = ['JsonUtil.java'],
  deps = [

# 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.
  name = 'messenger',
  srcs = glob(['**/*.java'], excludes = ['MessengerModule.java']),
  deps = [

  name = 'MessengerModule',
  srcs = ['MessengerModule.java'],
  deps = [

# A rule that builds a library with both relative and
# fully-qualified deps.
  name = 'testutil',
  srcs = glob(['tests/**/*.java'], excludes = 'tests/**/*Test.java'),
  deps = [