Skip to main content

Write Your First Script (Java)

This page walks through the Java flow for adding a script to your build. For what a script is, why bleep models scripts as a core concept, and when to choose a script over a source generation script, see Scripts and source generation.

Other languages: Kotlin → · Scala →

Prerequisites

Step 1: Add a scripts project

Add a scripts project to your bleep.yaml. The scripts project depends on bleepscript and is registered under the top-level scripts: section.

$schema: https://raw.githubusercontent.com/oyvindberg/bleep/master/schema.json
$version: 1.0.0-M9
jvm:
name: graalvm-community:25.0.1
projects:
myapp:
platform:
name: jvm
mainClass: com.example.Main
scripts:
dependencies:
- build.bleep:bleepscript:${BLEEP_VERSION}
java:
options: -proc:none --release 17
platform:
name: jvm
sources: ./src/main/java
scripts:
hello:
main: scripts.HelloScript
project: scripts

Key points:

  • ${BLEEP_VERSION} is interpolated by bleep to your installed version, so you don't have to keep the script project's dependency in sync by hand.
  • The top-level scripts: section maps a CLI name (hello) to a fully qualified class name (scripts.HelloScript) plus the project that contains it.
  • sources: ./src/main/java keeps the conventional Maven-style layout for Java code.

Step 2: Write the script

Extend bleepscript.BleepScript and implement run:

package scripts;

import bleepscript.BleepScript;
import bleepscript.Commands;
import bleepscript.Started;
import java.util.List;

public final class HelloScript extends BleepScript {
public HelloScript() {
super("hello");
}

@Override
public void run(Started started, Commands commands, List<String> args) {
int projectCount = started.build().explodedProjects().size();
started.logger().info("This build has " + projectCount + " projects");
if (args.isEmpty()) {
started.logger().info("Hello, world!");
} else {
started.logger().info("Hello, " + String.join(" ", args) + "!");
}
}
}

BleepScript provides the main method via reflection, so you don't need any explicit public static void main boilerplate. Just extend, pass a name to the super constructor, and override run.

Step 3: Run it

bleep hello
This build has 2 projects
Hello, world!
bleep hello Alice
This build has 2 projects
Hello, Alice!

Where to go from here