Using variables in Bazel
Tags: bazel, java, devops
Published: 2023-02-16 16:20:16


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jqyvqlburuntc0idv1oi.png)


[Bazel](https://bazel.build/) is a free and open-source build and test tool developed by Google. Bazel uses a declarative language (Starlark) to describe the build and test steps. 

This simple guide shows how to use variables in Bazel. This can be simple done in 3 steps. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pvh92ht4kgrugdxmu0tc.png)



We'll use an fictional example of setting Java 17 as the default version of the project to show you how to use variables in Bazel.

## 1: Define the Variable in a .bzl File

In this example we define the Variable in `rules/jdk/compatibility.bzl`

```bzl
JDK17_UPWARDS = select({
    "@nasa_atlantis//rules/jdk:jdk11": ["@platforms//:incompatible"],
    "@nasa_atlantis//rules/jdk:jdk8": ["@platforms//:incompatible"],
    "//conditions:default": [],
})

```
We use the `select` function to choose the suitable value for the **JDK17_UPWARDS** variable based on the current platform. In the example, we use Java 17 upwards if the platform supports it, or an empty list otherwise.

## 2: Import the Variable in your BUILD File

We use the `load` function to load specific Bazel file and import the variable from it. So, to do this we just add the line below to the BUILD file:

```bzl
load("//rules/jdk:compatibility.bzl", "JDK17_UPWARDS")
```
It loads the `compatibility.bzl` file and imports the **JDK17_UPWARDS** variable.

## 3: Use the Variable

Just use the variable in your BUILD file:

```bzl
java_library(
    name = "nasa_atlantis-web",
    srcs = glob(["src/main/java/**/*.java"]),
    resources = glob(["src/main/resources/**/*"]),
    tags = [
        "maven_coordinates=com.nasa.spring:nasa_atlantis-web:{nasa-project}",
    ],
    target_compatible_with = JDK17_UPWARDS,
    visibility = ["//visibility:public"],
    deps = [
        "//nasa-core/nasa-events:nasa-events-sb3",
        "//libs/b-spring/nasa_atlantis-core",
        "//libs/nasa-tracing:nasa-tracing-sb3",
        "@maven_spring//:jakarta_servlet_jakarta_servlet_api",
        "@maven_spring//:org_slf4j_slf4j_api",
        "@maven_spring//:org_springframework_boot_spring_boot",
        "@maven_spring//:org_springframework_boot_spring_boot_autoconfigure",
        "@maven_spring//:org_springframework_boot_spring_boot_starter",
        "@maven_spring//:org_springframework_boot_spring_boot_starter_web",
        "@maven_spring//:org_springframework_spring_beans",
        "@maven_spring//:org_springframework_spring_context",
        "@maven_spring//:org_springframework_spring_core",
    ],
)

```
In our example, we use the **JDK17_UPWARDS variable** in the **java_library** rule as the attribute named  **target_compatible_with**. This attribute specifies the versions of the Java language that the target is compatible with.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uws6ppurzp3snbtcg8as.png)


## ERROR: name is not defined

You could have the issue below:

```
ERROR: /builds/platform/nasa/libs/nasa_atlantis-web/BUILD:22:30: name 'JDK17_UPWARDS' is not defined
```

The reason is likely because you forgot to import the variable in your BUILD file or the path is wrong. For example you use 

```bzl
load("///rules/jdk:compatibility.bzl", "JDK17_UPWARDS")
```

instead of

```bzl
load("//rules/jdk:compatibility.bzl", "JDK17_UPWARDS")
```

Thats it.


Follow me in the group in [telegram](https://t.me/enterprise_developer)

[My blog](http://enterprise-developer.com)