Skip to content
rife2Public

Latest commit

 

History

89 Commits

Folders and files

Repository files navigation

JBang Extension for bld

License Java bld Release Snapshot GitHub CI

To install the latest version, add the following to the lib/bld/bld-wrapper.properties file:

bld.extension-jbang=com.uwyn.rife2:bld-jbang

For more information, please refer to the extensions documentation.

Execute a JBang script

@BuildCommand(summary = "Runs JBang script")
public void jbang() throws Exception {
    new JBangOperation()
            .fromProject(this)
            .jBangArgs("--quiet")
            .script("path/to/script.java")
            .args("foo", "bar")
            .execute();
}
./bld jbang

To set trusts before running a script:

@BuildCommand(summary = "Runs JBang script")
public void jbang() throws Exception {
    var trusts = List.of("https://github.com/", "https://jbang.dev/");
    var op = new JBangOperation().fromProject(this);

    op.jBangArgs("trust", "add")
            .jBangArgs(trusts)
            .execute();

    op.reset();

    op.script("https://github.com/jbangdev/jbang-examples/blob/main/examples/helloworld.java")
            .execute();
}

Choosing the JBang launcher

By default, the extension uses an existing JBang installation when available, or downloads the latest release into the sandbox when none is found. Downloading requires a project.

To use a specific local installation:

new JBangOperation()
        .fromProject(this)
        .jBangHome("/path/to/.jbang")
        .script("path/to/script.java")
        .execute();

To always use a downloaded JBang, ignoring any local installation:

new JBangOperation()
        .fromProject(this)
        .useDownloadedJBang()
        .script("path/to/script.java")
        .execute();

See jBangHome(...) and useDownloadedJBang() for details.

Pinning the JBang version

To download and use a specific JBang version via version(...), regardless of any local installation:

new JBangOperation()
        .fromProject(this)
        .version("0.143.0")
        .script("path/to/script.java")
        .execute();

Setting JBang options

Common JBang options have dedicated setters such as: verbose(...), quiet(...), offline(...), fresh(...), insecure(...), stacktrace(...), config(...), netrc(...), etc.

new JBangOperation()
        .fromProject(this)
        .offline(true)
        .fresh(true)
        .config("custom-jbang.properties")
        .script("path/to/script.java")
        .execute();

Anything else can be passed through jBangArgs(...):

new JBangOperation()
        .fromProject(this)
        .jBangArgs("--debug")
        .script("path/to/script.java")
        .execute();

Controlling execution

The child JBang process can be tuned without changing the JBang arguments:

new JBangOperation()
        .fromProject(this)
        .env("JAVA_HOME", "/path/to/jdk")
        .timeout(120)
        .inheritIO(false)
        .outputConsumer(System.out::println)
        .exitOnFailure(false)
        .script("path/to/script.java")
        .execute();

Reusing an operation

Call reset() to clear per-script settings while keeping the launcher, version pin, timeout and I/O configuration:

var op = new JBangOperation()
        .fromProject(this)
        .jBangHome("/path/to/.jbang")
        .timeout(120);

op.script("scripts/first.java")
        .args("input.txt")
        .execute();

op.reset();

op.script("scripts/second.java")
        .args("other.txt")
        .execute();

Or just create a fresh operation per script — reset() is only a convenience.

API Reference

Please check the documentation for all available configuration options.

Releases

Packages

Contributors

Languages