Projects and dependencies
A program that grows past one file, or uses libraries, becomes a project: a folder with a build.zyn that names the program and lists the libraries it uses. Zyn downloads those libraries from Maven repositories, the same ones Java, Kotlin and Gradle use, so every Java library is available.
#Making a project
zyn init hello
cd hello
zyn runzyn init makes this:
hello/
├── build.zyn ← the project and its dependencies
├── src/
│ └── main.zyn ← the program
└── .gitignore ← ignores build/Inside a project, the commands need no file name:
| Command | What it does |
|---|---|
zyn run [args...] | Runs the project's main file, with its libraries |
zyn test | Runs the tests of every file in src/ |
zyn build | Builds build/<name>-<version>.jar, with the libraries inside |
zyn build --native | Builds build/<name>, an executable (if the project uses no Java libraries) |
zyn deps | Shows the dependencies as a tree |
They work from any folder inside the project. Naming a file still works too: zyn run src/tool.zyn runs another program of the project with the project's libraries, and zyn build src/tool.zyn builds build/tool.jar. Outside a project, Zyn works on single files as always.
#The build file
build.zyn is written in Zyn, in the style of Gradle's Kotlin build files:
project {
name = "weather"
version = "1.0.0"
}
let gsonVersion = "2.11.0"
repositories {
mavenCentral()
}
dependencies {
implementation("com.google.code.gson:gson:{gsonVersion}")
testImplementation(files("test-libs/fixtures.jar"))
}A build file only describes the project: it can set properties, list repositories and dependencies, and use lets of plain values (strings, numbers, booleans and lists, with {interpolation}). It can't run code, so building a project never runs anything you didn't write in src/.
Mistakes are found like in any Zyn file:
project {
name = "weather"
}
dependencies {
implemntation("com.google.code.gson:gson:2.11.0")
}Build file error: Unknown function 'implemntation' in 'dependencies { }'. Did you mean 'implementation'?
--> build.zyn:6:5
|
6 | implemntation("com.google.code.gson:gson:2.11.0")
| ^#project
| Property | Default | What it is |
|---|---|---|
name | The folder's name | The project's name, used for the jar's name |
version | "0.1.0" | Its version, also in the jar's name |
main | "src/main.zyn" | The program's entry point. Imports start from its folder: import util.text is src/util/text.zyn |
group | "zynproject" | The start of the package of the project's classes, like "dev.emir" |
#repositories
Where libraries are downloaded from. Without a repositories block, it's Maven Central.
| Function | Repository |
|---|---|
mavenCentral() | Maven Central, where most Java libraries are |
maven("https://...") | Any Maven repository, like PaperMC's or a company's own |
mavenLocal() | ~/.m2/repository, where Maven installs what you build locally |
Only these repositories are used: repositories that libraries list in their own POM files are ignored, as in Gradle. What your program runs is decided by your build file alone.
#dependencies
A dependency is a Maven library, written "group:artifact:version" (as on Maven Central's pages, in the Gradle tab), or local jars with files(...):
dependencies {
implementation("com.google.guava:guava:33.4.0-jre")
compileOnly("io.papermc.paper:paper-api:1.21.11-R0.1-SNAPSHOT")
implementation(files("libs/legacy.jar", "vendor/*"))
}Each kind of dependency is used in different places:
| Kind | Compiled against | Used by zyn run | Used by zyn test | Inside the jar |
|---|---|---|---|---|
implementation | yes | yes | yes | yes |
compileOnly | yes | no | yes | no |
testImplementation | no | no | yes | no |
compileOnly is for libraries that whatever runs your program already has: a server's API for a server plugin, for example.
The libraries a dependency needs come along by themselves, following Maven's rules: when two libraries want different versions of a third, the one nearest to your build file wins. zyn deps shows what came from where:
$ zyn deps
implementation: compiled against, used when running, and bundled in the jar
\--- com.google.guava:guava:33.4.0-jre
+--- com.google.guava:failureaccess:1.0.2
+--- com.google.code.findbugs:jsr305:3.0.2
+--- org.checkerframework:checker-qual:3.43.0
+--- com.google.errorprone:error_prone_annotations:2.36.0
\--- com.google.j2objc:j2objc-annotations:3.0.0Libraries are downloaded once, into ~/.zyn/maven (or $ZYN_HOME/maven), and shared by all projects.
#Class names on the JVM
A project's classes are in a package of their own, zynproject.<project name>, followed by the folders under src/. A class keeps its name:
Code in the project hello-zyn | JVM class |
|---|---|
class HelloPlugin in src/hello.zyn | zynproject.hello_zyn.HelloPlugin |
class Dog in src/pets/dogs.zyn | zynproject.hello_zyn.pets.Dog |
The functions and lets of src/pets/dogs.zyn | zynproject.hello_zyn.pets.DogsZyn |
These are the names to use wherever something outside Zyn loads a class by name, like a plugin's plugin.yml or a framework's configuration. A project name that isn't a valid Java name is made into one: - becomes _, so hello-zyn is hello_zyn.
To use your own package instead of zynproject, set group, as in Maven and Gradle:
project {
name = "hello-zyn"
group = "dev.emir"
}Then HelloPlugin is dev.emir.hello_zyn.HelloPlugin. Single files outside a project have no package: class Dog in app.zyn is just Dog.
#Using a library
Once a library is in build.zyn, its classes are imported like any Java class, and editors with Zyn's language server know them as you type:
import com.google.gson.{Gson, JsonParser}
fn main() {
let json = JsonParser.parseString("""\{"name": "Zyn", "tags": ["fun", "fast"]\}""").asJsonObject
println(json.get("name").asString)
println(Gson().toJson(listOf("a", "b")))
}#zyn.lock
The first time a project's dependencies are resolved, Zyn writes zyn.lock: every library the project uses, at its exact version, with its size and SHA-256 checksum. Commit it with your code.
While zyn.lock matches build.zyn, nothing is resolved again: the program starts at once, works offline, and uses exactly the same libraries on every computer, even for snapshot versions that change on the server. A library whose download doesn't match its checksum is refused. When build.zyn's dependencies or repositories change, they're resolved again and the lock is rewritten.
| Command | What it does |
|---|---|
zyn deps | Resolves the dependencies, rewrites zyn.lock, and shows the tree |
zyn deps --update | Also checks the repositories for newer snapshots |
--offline | With run, test, build or deps: uses only what's already downloaded |
#Building the jar
zyn build makes one jar with everything the program needs: your code, Zyn's runtime, the resources/ folder, and every implementation library unpacked inside it. It runs anywhere with Java 17 or newer:
zyn build
java -jar build/weather-1.0.0.jarLibraries' service files (META-INF/services) are merged, and their signature files left out, since a signature only fits its original jar. compileOnly libraries stay out: the program's host provides them.
#An example: a server plugin
The repository's examples/minecraft-plugin is a plugin for Paper servers, built as a project:
project {
name = "hello-zyn"
version = "1.0.0"
main = "src/hello.zyn"
}
let paperVersion = "1.21.11-R0.1-SNAPSHOT"
repositories {
mavenCentral()
maven("https://repo.papermc.io/repository/maven-public/")
}
dependencies {
compileOnly("io.papermc.paper:paper-api:{paperVersion}")
}zyn build makes build/hello-zyn-1.0.0.jar, with the plugin, Zyn's runtime and plugin.yml, but not Paper's API, which the server provides. Its plugin.yml names the plugin's class as main: zynproject.hello_zyn.HelloPlugin.