close
Skip to content

Latest commit

 

History

History
590 lines (488 loc) · 84.8 KB

File metadata and controls

590 lines (488 loc) · 84.8 KB

Java rules

Rules

java_binary

java_binary(name, deps, srcs, data, resources, add_exports, add_opens, bootclasspath,
            classpath_resources, create_executable, deploy_env, deploy_manifest_lines, env, javacopts,
            jvm_flags, launcher, licenses, main_class, neverlink, plugins, resource_strip_prefix,
            runtime_deps, stamp, use_launcher, use_testrunner)

Builds a Java archive ("jar file"), plus a wrapper shell script with the same name as the rule. The wrapper shell script uses a classpath that includes, among other things, a jar file for each library on which the binary depends. When running the wrapper shell script, any nonempty JAVABIN environment variable will take precedence over the version specified via Bazel's --java_runtime_version flag.

The wrapper script accepts several unique flags. Refer to //src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt for a list of configurable flags and environment variables accepted by the wrapper.

Implicit output targets

  • name.jar: A Java archive, containing the class files and other resources corresponding to the binary's direct dependencies.
  • name-src.jar: An archive containing the sources ("source jar").
  • name_deploy.jar: A Java archive suitable for deployment (only built if explicitly requested).

    Building the <name>_deploy.jar target for your rule creates a self-contained jar file with a manifest that allows it to be run with the java -jar command or with the wrapper script's --singlejar option. Using the wrapper script is preferred to java -jar because it also passes the JVM flags and the options to load native libraries.

    The deploy jar contains all the classes that would be found by a classloader that searched the classpath from the binary's wrapper script from beginning to end. It also contains the native libraries needed for dependencies. These are automatically loaded into the JVM at runtime.

    If your target specifies a launcher attribute, then instead of being a normal JAR file, the _deploy.jar will be a native binary. This will contain the launcher plus any native (C++) dependencies of your rule, all linked into a static binary. The actual jar file's bytes will be appended to that native binary, creating a single binary blob containing both the executable and the Java code. You can execute the resulting jar file directly like you would execute any native binary.

  • name_deploy-src.jar: An archive containing the sources collected from the transitive closure of the target. These will match the classes in the deploy.jar except where jars have no matching source jar.

It is good practice to use the name of the source file that is the main entry point of the application (minus the extension). For example, if your entry point is called Main.java, then your name could be Main.

A deps attribute is not allowed in a java_binary rule without srcs; such a rule requires a main_class provided by runtime_deps.

The following code snippet illustrates a common mistake:


java_binary(
    name = "DontDoThis",
    srcs = [
        ...,
        "GeneratedJavaFile.java",  # a generated .java file
    ],
    deps = [":generating_rule",],  # rule that generates that file
)

Do this instead:


java_binary(
    name = "DoThisInstead",
    srcs = [
        ...,
        ":generating_rule",
    ],
)

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
deps The list of other libraries to be linked in to the target. See general comments about deps at Typical attributes defined by most build rules. List of labels optional []
srcs The list of source files that are processed to create the target. This attribute is almost always required; see exceptions below.

Source files of type .java are compiled. In case of generated .java files it is generally advisable to put the generating rule's name here instead of the name of the file itself. This not only improves readability but makes the rule more resilient to future changes: if the generating rule generates different files in the future, you only need to fix one place: the outs of the generating rule. You should not list the generating rule in deps because it is a no-op.

Source files of type .srcjar are unpacked and compiled. (This is useful if you need to generate a set of .java files with a genrule.)

Rules: if the rule (typically genrule or filegroup) generates any of the files listed above, they will be used the same way as described for source files.



This argument is almost always required, except if a main_class attribute specifies a class on the runtime classpath or you specify the runtime_deps argument.

List of labels optional []
data The list of files needed by this library at runtime. See general comments about data at Typical attributes defined by most build rules. List of labels optional []
resources A list of data files to include in a Java jar.

Resources may be source files or generated files.



If resources are specified, they will be bundled in the jar along with the usual .class files produced by compilation. The location of the resources inside of the jar file is determined by the project structure. Bazel first looks for Maven's standard directory layout, (a "src" directory followed by a "resources" directory grandchild). If that is not found, Bazel then looks for the topmost directory named "java" or "javatests" (so, for example, if a resource is at <workspace root>/x/java/y/java/z, the path of the resource will be y/java/z. This heuristic cannot be overridden, however, the resource_strip_prefix attribute can be used to specify a specific alternative directory for resource files.

List of labels optional []
add_exports Allow this library to access the given module or package.

This corresponds to the javac and JVM --add-exports= flags.

List of strings optional []
add_opens Allow this library to reflectively access the given module or package.

This corresponds to the javac and JVM --add-opens= flags.

List of strings optional []
bootclasspath Restricted API, do not use! Label optional None
classpath_resources DO NOT USE THIS OPTION UNLESS THERE IS NO OTHER WAY)

A list of resources that must be located at the root of the java tree. This attribute's only purpose is to support third-party libraries that require that their resources be found on the classpath as exactly "myconfig.xml". It is only allowed on binaries and not libraries, due to the danger of namespace conflicts.

List of labels optional []
create_executable Deprecated, use java_single_jar instead. Boolean optional True
deploy_env A list of other java_binary targets which represent the deployment environment for this binary. Set this attribute when building a plugin which will be loaded by another java_binary.
Setting this attribute excludes all dependencies from the runtime classpath (and the deploy jar) of this binary that are shared between this binary and the targets specified in deploy_env.
List of labels optional []
deploy_manifest_lines A list of lines to add to the META-INF/manifest.mf file generated for the *_deploy.jar target. The contents of this attribute are not subject to "Make variable" substitution. List of strings optional []
env - Dictionary: String -> String optional {}
javacopts Extra compiler options for this binary. Subject to "Make variable" substitution and Bourne shell tokenization.

These compiler options are passed to javac after the global compiler options.

List of strings optional []
jvm_flags A list of flags to embed in the wrapper script generated for running this binary. Subject to $(location) and "Make variable" substitution, and Bourne shell tokenization.

The wrapper script for a Java binary includes a CLASSPATH definition (to find all the dependent jars) and invokes the right Java interpreter. The command line generated by the wrapper script includes the name of the main class followed by a "$@" so you can pass along other arguments after the classname. However, arguments intended for parsing by the JVM must be specified before the classname on the command line. The contents of jvm_flags are added to the wrapper script before the classname is listed.



Note that this attribute has no effect on *_deploy.jar outputs.

List of strings optional []
launcher Specify a binary that will be used to run your Java program instead of the normal bin/java program included with the JDK. The target must be a cc_binary. Any cc_binary that implements the Java Invocation API can be specified as a value for this attribute.

By default, Bazel will use the normal JDK launcher (bin/java or java.exe).



The related --java_launcher Bazel flag affects only those java_binary and java_test targets that have not specified a launcher attribute.



Note that your native (C++, SWIG, JNI) dependencies will be built differently depending on whether you are using the JDK launcher or another launcher:



  • If you are using the normal JDK launcher (the default), native dependencies are built as a shared library named {name}_nativedeps.so, where {name} is the name attribute of this java_binary rule. Unused code is not removed by the linker in this configuration.


  • If you are using any other launcher, native (C++) dependencies are statically linked into a binary named {name}_nativedeps, where {name} is the name attribute of this java_binary rule. In this case, the linker will remove any code it thinks is unused from the resulting binary, which means any C++ code accessed only via JNI may not be linked in unless that cc_library target specifies alwayslink = True.


When using any launcher other than the default JDK launcher, the format of the *_deploy.jar output changes. See the main java_binary docs for details.

Label optional None
licenses - List of strings optional []
main_class Name of class with main() method to use as entry point. If a rule uses this option, it does not need a srcs=[...] list. Thus, with this attribute one can make an executable from a Java library that already contains one or more main() methods.

The value of this attribute is a class name, not a source file. The class must be available at runtime: it may be compiled by this rule (from srcs) or provided by direct or transitive dependencies (through runtime_deps or deps). If the class is unavailable, the binary will fail at runtime; there is no build-time check.

String optional ""
neverlink - Boolean optional False
plugins Java compiler plugins to run at compile-time. Every java_plugin specified in this attribute will be run whenever this rule is built. A library may also inherit plugins from dependencies that use exported_plugins. Resources generated by the plugin will be included in the resulting jar of this rule. List of labels optional []
resource_strip_prefix The path prefix to strip from Java resources.

If specified, this path prefix is stripped from every file in the resources attribute. It is an error for a resource file not to be under this directory. If not specified (the default), the path of resource file is determined according to the same logic as the Java package of source files. For example, a source file at stuff/java/foo/bar/a.txt will be located at foo/bar/a.txt.

String optional ""
runtime_deps Libraries to make available to the final binary or test at runtime only. Like ordinary deps, these will appear on the runtime classpath, but unlike them, not on the compile-time classpath. Dependencies needed only at runtime should be listed here. Dependency-analysis tools should ignore targets that appear in both runtime_deps and deps. List of labels optional []
stamp Whether to encode build information into the binary. Possible values:
  • stamp = 1: Always stamp the build information into the binary, even in --nostamp builds. This setting should be avoided, since it potentially kills remote caching for the binary and any downstream actions that depend on it.
  • stamp = 0: Always replace build information by constant values. This gives good build result caching.
  • stamp = -1: Embedding of build information is controlled by the --[no]stamp flag.

Stamped binaries are not rebuilt unless their dependencies change.

Integer optional -1
use_launcher Whether the binary should use a custom launcher.

If this attribute is set to false, the launcher attribute and the related --java_launcher flag will be ignored for this target.

Boolean optional True
use_testrunner Use the test runner (by default com.google.testing.junit.runner.BazelTestRunner) class as the main entry point for a Java program, and provide the test class to the test runner as a value of bazel.test_suite system property.


You can use this to override the default behavior, which is to use test runner for java_test rules, and not use it for java_binary rules. It is unlikely you will want to do this. One use is for AllTest rules that are invoked by another rule (to set up a database before running the tests, for example). The AllTest rule must be declared as a java_binary, but should still use the test runner as its main entry point.

The name of a test runner class can be overridden with main_class attribute.
Boolean optional False

java_import

java_import(name, deps, data, add_exports, add_opens, constraints, exports, jars, licenses,
            neverlink, proguard_specs, runtime_deps, srcjar)

This rule allows the use of precompiled .jar files as libraries for java_library and java_binary rules.

Examples


    java_import(
        name = "maven_model",
        jars = [
            "maven_model/maven-aether-provider-3.2.3.jar",
            "maven_model/maven-model-3.2.3.jar",
            "maven_model/maven-model-builder-3.2.3.jar",
        ],
    )

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
deps The list of other libraries to be linked in to the target. See java_library.deps. List of labels optional []
data The list of files needed by this rule at runtime. List of labels optional []
add_exports Allow this library to access the given module or package.

This corresponds to the javac and JVM --add-exports= flags.

List of strings optional []
add_opens Allow this library to reflectively access the given module or package.

This corresponds to the javac and JVM --add-opens= flags.

List of strings optional []
constraints Extra constraints imposed on this rule as a Java library. List of strings optional []
exports Targets to make available to users of this rule. See java_library.exports. List of labels optional []
jars The list of JAR files provided to Java targets that depend on this target. List of labels required
licenses - List of strings optional []
neverlink Only use this library for compilation and not at runtime. Useful if the library will be provided by the runtime environment during execution. Examples of libraries like this are IDE APIs for IDE plug-ins or tools.jar for anything running on a standard JDK. Boolean optional False
proguard_specs Files to be used as Proguard specification. These will describe the set of specifications to be used by Proguard. If specified, they will be added to any android_binary target depending on this library.

The files included here must only have idempotent rules, namely -dontnote, -dontwarn, assumenosideeffects, and rules that start with -keep. Other options can only appear in android_binary's proguard_specs, to ensure non-tautological merges.
List of labels optional []
runtime_deps Libraries to make available to the final binary or test at runtime only. See java_library.runtime_deps. List of labels optional []
srcjar A JAR file that contains source code for the compiled JAR files. Label optional None

java_library

java_library(name, deps, srcs, data, resources, add_exports, add_opens, bootclasspath,
             exported_plugins, exports, javabuilder_jvm_flags, javacopts, licenses, neverlink,
             plugins, proguard_specs, resource_strip_prefix, runtime_deps)

This rule compiles and links sources into a .jar file.

Implicit outputs

  • libname.jar: A Java archive containing the class files.
  • libname-src.jar: An archive containing the sources ("source jar").

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
deps The list of libraries to link into this library. See general comments about deps at Typical attributes defined by most build rules.

The jars built by java_library rules listed in deps will be on the compile-time classpath of this rule. Furthermore the transitive closure of their deps, runtime_deps and exports will be on the runtime classpath.

By contrast, targets in the data attribute are included in the runfiles but on neither the compile-time nor runtime classpath.

List of labels optional []
srcs The list of source files that are processed to create the target. This attribute is almost always required; see exceptions below.

Source files of type .java are compiled. In case of generated .java files it is generally advisable to put the generating rule's name here instead of the name of the file itself. This not only improves readability but makes the rule more resilient to future changes: if the generating rule generates different files in the future, you only need to fix one place: the outs of the generating rule. You should not list the generating rule in deps because it is a no-op.

Source files of type .srcjar are unpacked and compiled. (This is useful if you need to generate a set of .java files with a genrule.)

Rules: if the rule (typically genrule or filegroup) generates any of the files listed above, they will be used the same way as described for source files.

Source files of type .properties are treated as resources.



All other files are ignored, as long as there is at least one file of a file type described above. Otherwise an error is raised.



This argument is almost always required, except if you specify the runtime_deps argument.

List of labels optional []
data The list of files needed by this library at runtime. See general comments about data at Typical attributes defined by most build rules.

When building a java_library, Bazel doesn't put these files anywhere; if the data files are generated files then Bazel generates them. When building a test that depends on this java_library Bazel copies or links the data files into the runfiles area.

List of labels optional []
resources A list of data files to include in a Java jar.

Resources may be source files or generated files.



If resources are specified, they will be bundled in the jar along with the usual .class files produced by compilation. The location of the resources inside of the jar file is determined by the project structure. Bazel first looks for Maven's standard directory layout, (a "src" directory followed by a "resources" directory grandchild). If that is not found, Bazel then looks for the topmost directory named "java" or "javatests" (so, for example, if a resource is at <workspace root>/x/java/y/java/z, the path of the resource will be y/java/z. This heuristic cannot be overridden, however, the resource_strip_prefix attribute can be used to specify a specific alternative directory for resource files.

List of labels optional []
add_exports Allow this library to access the given module or package.

This corresponds to the javac and JVM --add-exports= flags.

List of strings optional []
add_opens Allow this library to reflectively access the given module or package.

This corresponds to the javac and JVM --add-opens= flags.

List of strings optional []
bootclasspath Restricted API, do not use! Label optional None
exported_plugins The list of java_plugins (e.g. annotation processors) to export to libraries that directly depend on this library.

The specified list of java_plugins will be applied to any library which directly depends on this library, just as if that library had explicitly declared these labels in plugins.

List of labels optional []
exports Exported libraries.

Listing rules here will make them available to parent rules, as if the parents explicitly depended on these rules. This is not true for regular (non-exported) deps.

Summary: a rule X can access the code in Y if there exists a dependency path between them that begins with a deps edge followed by zero or more exports edges. Let's see some examples to illustrate this.

Assume A depends on B and B depends on C. In this case C is a transitive dependency of A, so changing C's sources and rebuilding A will correctly rebuild everything. However A will not be able to use classes in C. To allow that, either A has to declare C in its deps, or B can make it easier for A (and anything that may depend on A) by declaring C in its (B's) exports attribute.

The closure of exported libraries is available to all direct parent rules. Take a slightly different example: A depends on B, B depends on C and D, and also exports C but not D. Now A has access to C but not to D. Now, if C and D exported some libraries, C' and D' respectively, A could only access C' but not D'.

Important: an exported rule is not a regular dependency. Sticking to the previous example, if B exports C and wants to also use C, it has to also list it in its own deps.

List of labels optional []
javabuilder_jvm_flags Restricted API, do not use! List of strings optional []
javacopts Extra compiler options for this library. Subject to "Make variable" substitution and Bourne shell tokenization.

These compiler options are passed to javac after the global compiler options.

List of strings optional []
licenses - List of strings optional []
neverlink Whether this library should only be used for compilation and not at runtime. Useful if the library will be provided by the runtime environment during execution. Examples of such libraries are the IDE APIs for IDE plug-ins or tools.jar for anything running on a standard JDK.

Note that neverlink = True does not prevent the compiler from inlining material from this library into compilation targets that depend on it, as permitted by the Java Language Specification (e.g., static final constants of String or of primitive types). The preferred use case is therefore when the runtime library is identical to the compilation library.

If the runtime library differs from the compilation library then you must ensure that it differs only in places that the JLS forbids compilers to inline (and that must hold for all future versions of the JLS).

Boolean optional False
plugins Java compiler plugins to run at compile-time. Every java_plugin specified in this attribute will be run whenever this rule is built. A library may also inherit plugins from dependencies that use exported_plugins. Resources generated by the plugin will be included in the resulting jar of this rule. List of labels optional []
proguard_specs Files to be used as Proguard specification. These will describe the set of specifications to be used by Proguard. If specified, they will be added to any android_binary target depending on this library.

The files included here must only have idempotent rules, namely -dontnote, -dontwarn, assumenosideeffects, and rules that start with -keep. Other options can only appear in android_binary's proguard_specs, to ensure non-tautological merges.
List of labels optional []
resource_strip_prefix The path prefix to strip from Java resources.

If specified, this path prefix is stripped from every file in the resources attribute. It is an error for a resource file not to be under this directory. If not specified (the default), the path of resource file is determined according to the same logic as the Java package of source files. For example, a source file at stuff/java/foo/bar/a.txt will be located at foo/bar/a.txt.

String optional ""
runtime_deps Libraries to make available to the final binary or test at runtime only. Like ordinary deps, these will appear on the runtime classpath, but unlike them, not on the compile-time classpath. Dependencies needed only at runtime should be listed here. Dependency-analysis tools should ignore targets that appear in both runtime_deps and deps. List of labels optional []

java_package_configuration

java_package_configuration(name, data, javacopts, output_licenses, packages, system)

Configuration to apply to a set of packages. Configurations can be added to java_toolchain.javacoptss.

Example:



java_package_configuration(
    name = "my_configuration",
    packages = [":my_packages"],
    javacopts = ["-Werror"],
)

package_group(
    name = "my_packages",
    packages = [
        "//com/my/project/...",
        "-//com/my/project/testing/...",
    ],
)

java_toolchain(
    ...,
    package_configuration = [
        ":my_configuration",
    ]
)


ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
data The list of files needed by this configuration at runtime. List of labels optional []
javacopts Java compiler flags. List of strings optional []
output_licenses - List of strings optional []
packages The set of package_groups the configuration should be applied to. List of labels optional []
system Corresponds to javac's --system flag. Label optional None

java_plugin

java_plugin(name, deps, srcs, data, resources, add_exports, add_opens, bootclasspath, generates_api,
            javabuilder_jvm_flags, javacopts, licenses, neverlink, output_licenses, plugins,
            processor_class, proguard_specs, resource_strip_prefix)

java_plugin defines plugins for the Java compiler run by Bazel. The only supported kind of plugins are annotation processors. A java_library or java_binary rule can run plugins by depending on them via the plugins attribute. A java_library can also automatically export plugins to libraries that directly depend on it using exported_plugins.

Implicit output targets

  • libname.jar: A Java archive.

Arguments are identical to java_library, except for the addition of the processor_class argument.

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
deps The list of libraries to link into this library. See general comments about deps at Typical attributes defined by most build rules.

The jars built by java_library rules listed in deps will be on the compile-time classpath of this rule. Furthermore the transitive closure of their deps, runtime_deps and exports will be on the runtime classpath.

By contrast, targets in the data attribute are included in the runfiles but on neither the compile-time nor runtime classpath.

List of labels optional []
srcs The list of source files that are processed to create the target. This attribute is almost always required; see exceptions below.

Source files of type .java are compiled. In case of generated .java files it is generally advisable to put the generating rule's name here instead of the name of the file itself. This not only improves readability but makes the rule more resilient to future changes: if the generating rule generates different files in the future, you only need to fix one place: the outs of the generating rule. You should not list the generating rule in deps because it is a no-op.

Source files of type .srcjar are unpacked and compiled. (This is useful if you need to generate a set of .java files with a genrule.)

Rules: if the rule (typically genrule or filegroup) generates any of the files listed above, they will be used the same way as described for source files.

Source files of type .properties are treated as resources.



All other files are ignored, as long as there is at least one file of a file type described above. Otherwise an error is raised.



This argument is almost always required, except if you specify the runtime_deps argument.

List of labels optional []
data The list of files needed by this library at runtime. See general comments about data at Typical attributes defined by most build rules.

When building a java_library, Bazel doesn't put these files anywhere; if the data files are generated files then Bazel generates them. When building a test that depends on this java_library Bazel copies or links the data files into the runfiles area.

List of labels optional []
resources A list of data files to include in a Java jar.

Resources may be source files or generated files.



If resources are specified, they will be bundled in the jar along with the usual .class files produced by compilation. The location of the resources inside of the jar file is determined by the project structure. Bazel first looks for Maven's standard directory layout, (a "src" directory followed by a "resources" directory grandchild). If that is not found, Bazel then looks for the topmost directory named "java" or "javatests" (so, for example, if a resource is at <workspace root>/x/java/y/java/z, the path of the resource will be y/java/z. This heuristic cannot be overridden, however, the resource_strip_prefix attribute can be used to specify a specific alternative directory for resource files.

List of labels optional []
add_exports Allow this library to access the given module or package.

This corresponds to the javac and JVM --add-exports= flags.

List of strings optional []
add_opens Allow this library to reflectively access the given module or package.

This corresponds to the javac and JVM --add-opens= flags.

List of strings optional []
bootclasspath Restricted API, do not use! Label optional None
generates_api This attribute marks annotation processors that generate API code.

If a rule uses an API-generating annotation processor, other rules depending on it can refer to the generated code only if their compilation actions are scheduled after the generating rule. This attribute instructs Bazel to introduce scheduling constraints when --java_header_compilation is enabled.

WARNING: This attribute affects build performance, use it only if necessary.

Boolean optional False
javabuilder_jvm_flags Restricted API, do not use! List of strings optional []
javacopts Extra compiler options for this library. Subject to "Make variable" substitution and Bourne shell tokenization.

These compiler options are passed to javac after the global compiler options.

List of strings optional []
licenses - List of strings optional []
neverlink Whether this library should only be used for compilation and not at runtime. Useful if the library will be provided by the runtime environment during execution. Examples of such libraries are the IDE APIs for IDE plug-ins or tools.jar for anything running on a standard JDK.

Note that neverlink = True does not prevent the compiler from inlining material from this library into compilation targets that depend on it, as permitted by the Java Language Specification (e.g., static final constants of String or of primitive types). The preferred use case is therefore when the runtime library is identical to the compilation library.

If the runtime library differs from the compilation library then you must ensure that it differs only in places that the JLS forbids compilers to inline (and that must hold for all future versions of the JLS).

Boolean optional False
output_licenses - List of strings optional []
plugins Java compiler plugins to run at compile-time. Every java_plugin specified in this attribute will be run whenever this rule is built. A library may also inherit plugins from dependencies that use exported_plugins. Resources generated by the plugin will be included in the resulting jar of this rule. List of labels optional []
processor_class The processor class is the fully qualified type of the class that the Java compiler should use as entry point to the annotation processor. If not specified, this rule will not contribute an annotation processor to the Java compiler's annotation processing, but its runtime classpath will still be included on the compiler's annotation processor path. (This is primarily intended for use by Error Prone plugins, which are loaded from the annotation processor path using java.util.ServiceLoader.) String optional ""
proguard_specs Files to be used as Proguard specification. These will describe the set of specifications to be used by Proguard. If specified, they will be added to any android_binary target depending on this library.

The files included here must only have idempotent rules, namely -dontnote, -dontwarn, assumenosideeffects, and rules that start with -keep. Other options can only appear in android_binary's proguard_specs, to ensure non-tautological merges.
List of labels optional []
resource_strip_prefix The path prefix to strip from Java resources.

If specified, this path prefix is stripped from every file in the resources attribute. It is an error for a resource file not to be under this directory. If not specified (the default), the path of resource file is determined according to the same logic as the Java package of source files. For example, a source file at stuff/java/foo/bar/a.txt will be located at foo/bar/a.txt.

String optional ""

java_runtime

java_runtime(name, srcs, default_cds, hermetic_srcs, hermetic_static_libs, java, java_home,
             lib_ct_sym, lib_modules, output_licenses, version)

Specifies the configuration for a Java runtime.

Example:



java_runtime(
    name = "jdk-9-ea+153",
    srcs = glob(["jdk9-ea+153/**"]),
    java_home = "jdk9-ea+153",
)


ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
srcs All files in the runtime. List of labels optional []
default_cds Default CDS archive for hermetic java_runtime. When hermetic is enabled for a java_binary target and if the target does not provide its own CDS archive by specifying the classlist attribute, the java_runtime default CDS is packaged in the hermetic deploy JAR. Label optional None
hermetic_srcs Files in the runtime needed for hermetic deployments. List of labels optional []
hermetic_static_libs The libraries that are statically linked with the launcher for hermetic deployments List of labels optional []
java The path to the java executable. Label optional None
java_home The path to the root of the runtime. Subject to "Make" variable substitution. If this path is absolute, the rule denotes a non-hermetic Java runtime with a well-known path. In that case, the srcs and java attributes must be empty. String optional ""
lib_ct_sym The lib/ct.sym file needed for compilation with --release. If not specified and there is exactly one file in srcs whose path ends with /lib/ct.sym, that file is used. Label optional None
lib_modules The lib/modules file needed for hermetic deployments. Label optional None
output_licenses - List of strings optional []
version The feature version of the Java runtime. I.e., the integer returned by Runtime.version().feature(). Integer optional 0

java_test

java_test(name, deps, srcs, data, resources, add_exports, add_opens, bootclasspath,
          classpath_resources, create_executable, deploy_manifest_lines, env, env_inherit, javacopts,
          jvm_flags, launcher, licenses, main_class, neverlink, plugins, resource_strip_prefix,
          runtime_deps, stamp, test_class, use_launcher, use_testrunner)

A java_test() rule compiles a Java test. A test is a binary wrapper around your test code. The test runner's main method is invoked instead of the main class being compiled.

Implicit output targets

  • name.jar: A Java archive.
  • name_deploy.jar: A Java archive suitable for deployment. (Only built if explicitly requested.) See the description of the name_deploy.jar output from java_binary for more details.

See the section on java_binary() arguments. This rule also supports all attributes common to all test rules (*_test).

Examples



java_library(
    name = "tests",
    srcs = glob(["*.java"]),
    deps = [
        "//java/com/foo/base:testResources",
        "//java/com/foo/testing/util",
    ],
)

java_test(
    name = "AllTests",
    size = "small",
    runtime_deps = [
        ":tests",
        "//util/mysql",
    ],
)

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
deps The list of other libraries to be linked in to the target. See general comments about deps at Typical attributes defined by most build rules. List of labels optional []
srcs The list of source files that are processed to create the target. This attribute is almost always required; see exceptions below.

Source files of type .java are compiled. In case of generated .java files it is generally advisable to put the generating rule's name here instead of the name of the file itself. This not only improves readability but makes the rule more resilient to future changes: if the generating rule generates different files in the future, you only need to fix one place: the outs of the generating rule. You should not list the generating rule in deps because it is a no-op.

Source files of type .srcjar are unpacked and compiled. (This is useful if you need to generate a set of .java files with a genrule.)

Rules: if the rule (typically genrule or filegroup) generates any of the files listed above, they will be used the same way as described for source files.



This argument is almost always required, except if a main_class attribute specifies a class on the runtime classpath or you specify the runtime_deps argument.

List of labels optional []
data The list of files needed by this library at runtime. See general comments about data at Typical attributes defined by most build rules. List of labels optional []
resources A list of data files to include in a Java jar.

Resources may be source files or generated files.



If resources are specified, they will be bundled in the jar along with the usual .class files produced by compilation. The location of the resources inside of the jar file is determined by the project structure. Bazel first looks for Maven's standard directory layout, (a "src" directory followed by a "resources" directory grandchild). If that is not found, Bazel then looks for the topmost directory named "java" or "javatests" (so, for example, if a resource is at <workspace root>/x/java/y/java/z, the path of the resource will be y/java/z. This heuristic cannot be overridden, however, the resource_strip_prefix attribute can be used to specify a specific alternative directory for resource files.

List of labels optional []
add_exports Allow this library to access the given module or package.

This corresponds to the javac and JVM --add-exports= flags.

List of strings optional []
add_opens Allow this library to reflectively access the given module or package.

This corresponds to the javac and JVM --add-opens= flags.

List of strings optional []
bootclasspath Restricted API, do not use! Label optional None
classpath_resources DO NOT USE THIS OPTION UNLESS THERE IS NO OTHER WAY)

A list of resources that must be located at the root of the java tree. This attribute's only purpose is to support third-party libraries that require that their resources be found on the classpath as exactly "myconfig.xml". It is only allowed on binaries and not libraries, due to the danger of namespace conflicts.

List of labels optional []
create_executable Deprecated, use java_single_jar instead. Boolean optional True
deploy_manifest_lines A list of lines to add to the META-INF/manifest.mf file generated for the *_deploy.jar target. The contents of this attribute are not subject to "Make variable" substitution. List of strings optional []
env - Dictionary: String -> String optional {}
env_inherit - List of strings optional []
javacopts Extra compiler options for this binary. Subject to "Make variable" substitution and Bourne shell tokenization.

These compiler options are passed to javac after the global compiler options.

List of strings optional []
jvm_flags A list of flags to embed in the wrapper script generated for running this binary. Subject to $(location) and "Make variable" substitution, and Bourne shell tokenization.

The wrapper script for a Java binary includes a CLASSPATH definition (to find all the dependent jars) and invokes the right Java interpreter. The command line generated by the wrapper script includes the name of the main class followed by a "$@" so you can pass along other arguments after the classname. However, arguments intended for parsing by the JVM must be specified before the classname on the command line. The contents of jvm_flags are added to the wrapper script before the classname is listed.



Note that this attribute has no effect on *_deploy.jar outputs.

List of strings optional []
launcher Specify a binary that will be used to run your Java program instead of the normal bin/java program included with the JDK. The target must be a cc_binary. Any cc_binary that implements the Java Invocation API can be specified as a value for this attribute.

By default, Bazel will use the normal JDK launcher (bin/java or java.exe).



The related --java_launcher Bazel flag affects only those java_binary and java_test targets that have not specified a launcher attribute.



Note that your native (C++, SWIG, JNI) dependencies will be built differently depending on whether you are using the JDK launcher or another launcher:



  • If you are using the normal JDK launcher (the default), native dependencies are built as a shared library named {name}_nativedeps.so, where {name} is the name attribute of this java_binary rule. Unused code is not removed by the linker in this configuration.


  • If you are using any other launcher, native (C++) dependencies are statically linked into a binary named {name}_nativedeps, where {name} is the name attribute of this java_binary rule. In this case, the linker will remove any code it thinks is unused from the resulting binary, which means any C++ code accessed only via JNI may not be linked in unless that cc_library target specifies alwayslink = True.


When using any launcher other than the default JDK launcher, the format of the *_deploy.jar output changes. See the main java_binary docs for details.

Label optional None
licenses - List of strings optional []
main_class Name of class with main() method to use as entry point. If a rule uses this option, it does not need a srcs=[...] list. Thus, with this attribute one can make an executable from a Java library that already contains one or more main() methods.

The value of this attribute is a class name, not a source file. The class must be available at runtime: it may be compiled by this rule (from srcs) or provided by direct or transitive dependencies (through runtime_deps or deps). If the class is unavailable, the binary will fail at runtime; there is no build-time check.

String optional ""
neverlink - Boolean optional False
plugins Java compiler plugins to run at compile-time. Every java_plugin specified in this attribute will be run whenever this rule is built. A library may also inherit plugins from dependencies that use exported_plugins. Resources generated by the plugin will be included in the resulting jar of this rule. List of labels optional []
resource_strip_prefix The path prefix to strip from Java resources.

If specified, this path prefix is stripped from every file in the resources attribute. It is an error for a resource file not to be under this directory. If not specified (the default), the path of resource file is determined according to the same logic as the Java package of source files. For example, a source file at stuff/java/foo/bar/a.txt will be located at foo/bar/a.txt.

String optional ""
runtime_deps Libraries to make available to the final binary or test at runtime only. Like ordinary deps, these will appear on the runtime classpath, but unlike them, not on the compile-time classpath. Dependencies needed only at runtime should be listed here. Dependency-analysis tools should ignore targets that appear in both runtime_deps and deps. List of labels optional []
stamp Whether to encode build information into the binary. Possible values:
  • stamp = 1: Always stamp the build information into the binary, even in --nostamp builds. This setting should be avoided, since it potentially kills remote caching for the binary and any downstream actions that depend on it.
  • stamp = 0: Always replace build information by constant values. This gives good build result caching.
  • stamp = -1: Embedding of build information is controlled by the --[no]stamp flag.

Stamped binaries are not rebuilt unless their dependencies change.

Integer optional 0
test_class The Java class to be loaded by the test runner.

By default, if this argument is not defined then the legacy mode is used and the test arguments are used instead. Set the --nolegacy_bazel_java_test flag to not fallback on the first argument.

This attribute specifies the name of a Java class to be run by this test. It is rare to need to set this. If this argument is omitted, it will be inferred using the target's name and its source-root-relative path. If the test is located outside a known source root, Bazel will report an error if test_class is unset.

For JUnit3, the test class needs to either be a subclass of junit.framework.TestCase or it needs to have a public static suite() method that returns a junit.framework.Test (or a subclass of Test). For JUnit4, the class needs to be annotated with org.junit.runner.RunWith.

This attribute allows several java_test rules to share the same Test (TestCase, TestSuite, ...). Typically additional information is passed to it (e.g. via jvm_flags=['-Dkey=value']) so that its behavior differs in each case, such as running a different subset of the tests. This attribute also enables the use of Java tests outside the javatests tree.

String optional ""
use_launcher Whether the binary should use a custom launcher.

If this attribute is set to false, the launcher attribute and the related --java_launcher flag will be ignored for this target.

Boolean optional True
use_testrunner Use the test runner (by default com.google.testing.junit.runner.BazelTestRunner) class as the main entry point for a Java program, and provide the test class to the test runner as a value of bazel.test_suite system property.


You can use this to override the default behavior, which is to use test runner for java_test rules, and not use it for java_binary rules. It is unlikely you will want to do this. One use is for AllTest rules that are invoked by another rule (to set up a database before running the tests, for example). The AllTest rule must be declared as a java_binary, but should still use the test runner as its main entry point.

The name of a test runner class can be overridden with main_class attribute.
Boolean optional True

java_toolchain

java_toolchain(name, android_lint_data, android_lint_jvm_opts, android_lint_opts,
               android_lint_package_configuration, android_lint_runner, bootclasspath,
               compatible_javacopts, deps_checker, forcibly_disable_header_compilation, genclass,
               header_compiler, header_compiler_builtin_processors, header_compiler_direct, ijar,
               jacocorunner, java_runtime, javabuilder, javabuilder_data, javabuilder_jvm_opts,
               javac_supports_multiplex_workers, javac_supports_worker_cancellation,
               javac_supports_worker_multiplex_sandboxing, javac_supports_workers, javacopts,
               jspecify_implicit_deps, jspecify_javacopts, jspecify_packages, jspecify_processor,
               jspecify_processor_class, jspecify_stubs, jvm_opts, licenses, misc, oneversion,
               oneversion_allowlist, oneversion_allowlist_for_tests, oneversion_whitelist,
               package_configuration, proguard_allowlister, reduced_classpath_incompatible_processors,
               singlejar, source_version, target_version, timezone_data, tools, turbine_data,
               turbine_jvm_opts, xlint)

Specifies the configuration for the Java compiler. Which toolchain to be used can be changed through the --java_toolchain argument. Normally you should not write those kind of rules unless you want to tune your Java compiler.

Examples

A simple example would be:



java_toolchain(
    name = "toolchain",
    source_version = "7",
    target_version = "7",
    bootclasspath = ["//tools/jdk:bootclasspath"],
    xlint = [ "classfile", "divzero", "empty", "options", "path" ],
    javacopts = [ "-g" ],
    javabuilder = ":JavaBuilder_deploy.jar",
)

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
android_lint_data Labels of tools available for label-expansion in android_lint_jvm_opts. List of labels optional []
android_lint_jvm_opts The list of arguments for the JVM when invoking Android Lint. List of strings optional []
android_lint_opts The list of Android Lint arguments. List of strings optional []
android_lint_package_configuration Android Lint Configuration that should be applied to the specified package groups. List of labels optional []
android_lint_runner Label of the Android Lint runner, if any. Label optional None
bootclasspath The Java target bootclasspath entries. Corresponds to javac's -bootclasspath flag. List of labels optional []
compatible_javacopts Internal API, do not use! Dictionary: String -> List of strings optional {}
deps_checker Label of the ImportDepsChecker deploy jar. Label optional None
forcibly_disable_header_compilation Overrides --java_header_compilation to disable header compilation on platforms that do not support it, e.g. JDK 7 Bazel. Boolean optional False
genclass Label of the GenClass deploy jar. Label optional None
header_compiler Label of the header compiler. Required if --java_header_compilation is enabled. Label optional None
header_compiler_builtin_processors Internal API, do not use! List of strings optional []
header_compiler_direct Optional label of the header compiler to use for direct classpath actions that do not include any API-generating annotation processors.

This tool does not support annotation processing.

Label optional None
ijar Label of the ijar executable. Label optional None
jacocorunner Label of the JacocoCoverageRunner deploy jar. Label optional None
java_runtime The java_runtime to use with this toolchain. It defaults to java_runtime in execution configuration. Label optional None
javabuilder Label of the JavaBuilder deploy jar. Label optional None
javabuilder_data Labels of data available for label-expansion in javabuilder_jvm_opts. List of labels optional []
javabuilder_jvm_opts The list of arguments for the JVM when invoking JavaBuilder. List of strings optional []
javac_supports_multiplex_workers True if JavaBuilder supports running as a multiplex persistent worker, false if it doesn't. Boolean optional True
javac_supports_worker_cancellation True if JavaBuilder supports cancellation of persistent workers, false if it doesn't. Boolean optional True
javac_supports_worker_multiplex_sandboxing True if JavaBuilder supports running as a multiplex persistent worker with sandboxing, false if it doesn't. Boolean optional False
javac_supports_workers True if JavaBuilder supports running as a persistent worker, false if it doesn't. Boolean optional True
javacopts The list of extra arguments for the Java compiler. Please refer to the Java compiler documentation for the extensive list of possible Java compiler flags. List of strings optional []
jspecify_implicit_deps Experimental, do not use! Label optional None
jspecify_javacopts Experimental, do not use! List of strings optional []
jspecify_packages Experimental, do not use! List of labels optional []
jspecify_processor Experimental, do not use! Label optional None
jspecify_processor_class Experimental, do not use! String optional ""
jspecify_stubs Experimental, do not use! List of labels optional []
jvm_opts The list of arguments for the JVM when invoking the Java compiler. Please refer to the Java virtual machine documentation for the extensive list of possible flags for this option. List of strings optional []
licenses - List of strings optional []
misc Deprecated: use javacopts instead List of strings optional []
oneversion Label of the one-version enforcement binary. Label optional None
oneversion_allowlist Label of the one-version allowlist. Label optional None
oneversion_allowlist_for_tests Label of the one-version allowlist for tests. Label optional None
oneversion_whitelist Deprecated: use oneversion_allowlist instead Label optional None
package_configuration Configuration that should be applied to the specified package groups. List of labels optional []
proguard_allowlister Label of the Proguard allowlister. Label optional "@bazel_tools//tools/jdk:proguard_whitelister"
reduced_classpath_incompatible_processors Internal API, do not use! List of strings optional []
singlejar Label of the SingleJar deploy jar. Label optional None
source_version The Java source version (e.g., '6' or '7'). It specifies which set of code structures are allowed in the Java source code. String optional ""
target_version The Java target version (e.g., '6' or '7'). It specifies for which Java runtime the class should be build. String optional ""
timezone_data Label of a resource jar containing timezone data. If set, the timezone data is added as an implicitly runtime dependency of all java_binary rules. Label optional None
tools Labels of tools available for label-expansion in jvm_opts. List of labels optional []
turbine_data Labels of data available for label-expansion in turbine_jvm_opts. List of labels optional []
turbine_jvm_opts The list of arguments for the JVM when invoking turbine. List of strings optional []
xlint The list of warning to add or removes from default list. Precedes it with a dash to removes it. Please see the Javac documentation on the -Xlint options for more information. List of strings optional []