The sections below include information about the structure of a package directory. It explains the purpose of each file in a package.
. ├── Ballerina.toml ├── Dependencies.toml ├── Package.md ├── Module.md ├── main.bal ├── utils.bal ├── generated/ │ ├── generated_service.bal │ └── model/ ├── tests/ │ ├── main_tests.bal │ └── utils_tests.bal ├── resources/ │ └── app.png ├── modules/ │ ├── model/ │ └── module1.test/ ├── documents/ ├── sample.png ├── icon.png └── target/
Ballerina.toml identifies the directory as a Ballerina package. It contains all the meta information that is needed to build your package.
[package] org = "samjs" name = "winery" version = "0.1.0" export = ["winery", "winery.model"] include = ["documents", "sample.png"] icon = "icon.png" [build-options] observabilityIncluded = true
[package] table contains the meta information about the package. If you wish to publish a private package, you can set the following line to the table to set the package visibility.
visibility = "private"
The organization is a logical name used for grouping modules together under a common namespace within a repository. Building a library package with
bal build -c and pushing a library package into a repository will fail without an organization name.
Organization names can only contain alphanumerics, underscore, and the maximum length is 256 characters.
When you run the
bal new command, the organization name by default will be set to the user name of your machine. You can choose to update the
Ballerina.toml file to amend the organization name appropriately.
As described in Organizations, the restrictions for the organization name of a package when publishing to Ballerina Central should also be considered before choosing an organization name.
The package name is an identifier used to refer to the package.
The name can only contain alphanumerics, underscore, period, and the maximum length is 256 characters.
If the package name is not provided in the
Ballerina.toml file, then the current directory name is set as the package name. If there are any characters in the directory name mismatching the allowed regex, these will be replaced with the
Hierarchical package names
When a package provides multiple functionalities, it is better to split it into multiple packages instead. For scenarios like this, you can give a hierarchical name to the package.
For example, if you need to provide a set of APIs to communicate with AWS, you can choose to support APIs to AWS services using multiple packages such as
The split module condition
split module condition occurs when the latest versions of two different packages contain the same module, resulting in a build failure. When using hierarchical package names, ensure that the package repository does not hold another package containing a module with the same name in its latest version.
For example, if you created and published to Ballerina Central, the
1.0.0 version of
aws.rds package containing
and decide to move the
aws.rds.mysql module to a separate package later, you need to follow the below steps.
- Push a new version(
1.0.1) of the
aws.rdspackage that does not contain the
- Push new
Ballerina strictly follows the rules of Semantic Versioning. Therefore, in general, you should follow the SemVer best practices when versioning a package.
If the package is in the initial stages of development, label the package with the zero major version (0.x.y). This will give the user a hint that API changes are frequent and that the package is far from being production-ready.
Use versions as three numeric parts
- Increment the patch version when only backward compatible bug fixes are introduced.
- Increment the minor version when new backward compatible functionality is introduced to the public API.
- Increment the major version when any backward incompatible changes are introduced to the public API.
When you are stabilizing the package to roll out to production, pre-release versions are suitable for versioning (E.g. 1.0.0-alpha). Pre-release versions are not considered production-ready. Even though not frequent compared to the initial development phase, API changes are possible.
If the changes to pre-release versions are incremental, you can use the numeric pre-release versioning technique (E.g. 1.0.0-alpha.1, 1.0.0-alpha.2).
Once the package is production-ready, you can use a stable version (E.g. 1.0.0). Any subsequent minor or patch releases of the same major version should be backward compatible and, should not break existing builds.
You can specify the modules which should be visible to the outside using the
export field. The export field accepts a string array, which contains the names of the modules which
need to be publicly visible.
The modules should be referred in the
<pacakge_name>.<module_name> format. The default module should be referenced by the package name.
icon field accepts a path to an icon. The specified icon will be packaged into the
docs/ directory of the BALA.
.png format is supported for the icon.
You can provide paths to any additional resources, which need to be packed in the BALA file during the use of the
bal pack command.
include field accepts a string array, which contains the directory or file paths to include in the BALA. The paths should be relative to the package root directory.
The included file paths will be packaged into the root of the BALA preserving its original structure.
[build-options] table specifies options that should be applied when building the package. You can provide build options in the
Ballerina.toml instead of passing them to the
bal build command.
Ballerina supports the following build options.
[build-options] observabilityIncluded = true offline = true skipTests = true testReport = true codeCoverage = true cloud = "k8s"
While the Ballerina compiler resolves dependencies automatically based on the import statements, the
[[dependency]] array can be used to specify a dependency from the local repository or to specify the minimum required version of a dependency.
Specify a dependency from the local repository
The following example shows how a dependency from the local repository can be specified:
[[dependency]] org = "ballerinax" name = "mysql" version = "1.5.0" repository = "local"
This will resolve the specified dependency from the local repository. For more information on how dependency resolution with the local repository works, see [Manage Dependencies] (learn/manage-dependencies/#use-dependencies-from-the-local-repository).
Specify the minimum version for a dependency
The following example shows how the minimum version for a dependency can be specified:
[[dependency]] org = "ballerinax" name = "sql" version = "1.6.0"
With this, the compiler considers
1.6.0 as the minimum required version when resolving
ballerinax/sql. If there are higher versions available in the Ballerina repositories, then, the latest compatible version will be resolved. To learn more about updating versions, see [Manage Dependencies] (learn/manage-dependencies/#update-dependency-versions).
When using the "bal build" command to compile a Ballerina package, the resulting output will either be an executable JAR file or a non-executable JAR file (library package) depending on whether the package contains an entry point. These archives created by the Ballerina compiler are self-contained, meaning that they include all necessary dependencies. It may also be necessary to package external JAR files with these archives.
When working with JAR files, it is considered a best practice to keep them organized within the package. This makes it easier to manage and maintain the dependencies.
Note: Additionally, it is important that Java libraries are considered platform-specific, and thereby, their location and usage should be specified in the “Ballerina.toml” file. This can be done by including a dependency on the specific JAR file as demonstrated below in the “Ballerina.toml” file. This helps the Ballerina compiler to include the relevant JAR files when creating the archive.
There are two ways to include the JAR dependency.
Use remote repositories
The following example shows how a dependency from a public Maven repository can be specified.
[[platform.java11.dependency]] # Group ID of the Maven dependency. groupId = "<group-id>" # Artifact ID of the Maven dependency. artifactId = "<artifact-id>" # Version of the Maven dependency. version = "<version>"
When building the package, these specified Maven dependencies will be resolved and can be found in the
target/platform-libs directory. To specify a Maven dependency in the
Ballerina.toml file, you can use the following format.
It is also possible to use a custom repository such as a private Maven repository or Github Package repository for your dependencies by specifying it in the
[[platform.java11.repository]] id = "<maven-repository-id>" url = "<maven-repository-url>" username = "<maven-repository-username>" password = "<maven-repository-password>"
When working with JAR file dependencies, it is a best practice to attach them to the default root module of your package if your package has only the default root module. This makes it easy to manage and maintain the dependencies. However, if your package is a Ballerina library package, it is recommended that you specify the JAR file dependencies in each Ballerina module that depends on the JAR file.
By default, the
bal build command will package all JAR files specified in the
Ballerina.toml file along with the executable JAR file. This ensures that the executable JAR file includes all necessary dependencies making the package self-contained and easy to use. This approach allows for a more organized and efficient way of managing dependencies and ensures that the
Provide the path of JAR file
You may also store the JAR files anywhere in your file system and provide the path as shown below.
[[platform.java11.dependency]] # Group ID of the dependency. groupId = "<group-id>" # Artifact ID of the dependency. artifactId = "<artifact-id>" # Version of the dependency. version = "<version>" # Absolute or relative path of the JAR file. path = "<path-to-jar-file-1>"
The Ballerina compiler will copy the specified JAR file from the provided path when creating the archive.
Restrict usage to specific modules
The following example shows how you can optionally restrict the visibility of a platform dependency to a selected set of modules.
[[platform.java11.dependency]] # Absolute or relative path of the JAR file. path = "<path-to-jar-file-1>" # An optional comma-separated list of Ballerina module names (to restrict the usage of this JAR). modules = ["<ballerina-module-1>"]
It is considered a best practice to provide the names of the modules that use the JAVA library as a comma-separated list. This ensures that the JAR library is only used in the specified modules.
Resolve multiple versions of the same JAR
There can be use cases where two different Ballerina dependencies may use the same platform library. Two platform dependency entries having the same
groupId and the
artifactId is considered to be the same where the latest out of the two will be picked by the compiler for creating the executable JAR.
The following example shows the recommended way for specifying a platform dependency which will help with version resolution.
[[platform.java11.dependency]] # Absolute or relative path of the JAR file. path = "<path-to-jar-file-1>" # Optional details about the dependency (to handle conflicting JAR files). groupId = "<dependency-group-id>" artifactId = "<dependency-artifact-id>" version = "<dependency-version>"
With the above approach, for example, if you are using
commons-logging-1.2.jar in your package but there is another package in the dependency graph that uses
commons-logging-1.1.1.jar, the compiler will pick the
commons-logging-1.2.jar since it is the latest version. A warning will be reported in addition as shown below.
WARNING [mypackage] detected conflicting jar files. 'commons-logging-1.1.1.jar' dependency of 'myorg/pkg2' conflicts with 'commons-logging-1.2.jar' dependency of 'myorg/pkg1'. Picking 'commons-logging-1.2.jar' over 'commons-logging-1.1.1.jar'.
Note: Ignoring the
artifactId will result in picking a random jar with the following warning reported by the compiler.
warning: Detected conflicting jar files: 'commons-logging-1.1.1.jar' dependency of 'myorg/pkg1' conflict with 'commons-logging-1.2.jar' dependency of 'myorg/pkg2'
Define the scope for a dependency
By default, the scope takes the value
default which will add it to the final executable JAR file. If you want to restrict a certain platform dependency to be used only for testing, specify the scope as
testOnly. This will add the platform dependncy to the test runtime but will avoid packing it into the final executable JAR file.
The following example shows a platform dependency entry with the
[[platform.java11.dependency]] # Absolute or relative path of the JAR file. path = "<path-to-jar-file-1>" # Scope of the JAR file scope = "<scope-of-the-jar-file>"
Dependencies.toml locks the versions of the dependencies to support repeatable builds.
This file is auto-generated and managed by the Ballerina CLI. It does not need user intervention.
Package.md file provides a human-readable description of a package. This file is required for publishing a package to a repository.
It is the first page you will see when you navigate to the package in Ballerina Central.
This file is in markdown format. It will be auto-generated when you create a library package. For steps to create a library package, see Create a Library Package
target/ directory contains artifacts generated by building a package.
tests/ directories, and
These are directories related to the default module. For detailed information, see Module layout.
This directory contains the other modules. The layout of this directory is explained in the Module layout.
. ├── app.bal ├── utils.bal ├── tests/ │ ├── main_tests.bal │ ├── utils_tests.bal │ └── resources/ │ └── test_resource.json └── resources/ └── app.png
The root directory of the default module is the root of the package directory. The top-level
modules/ directory contains all the other modules. Each immediate subdirectory of the
modules/ directory becomes a Ballerina module. The subdirectory name becomes the module name. Therefore, the subdirectory name should be a valid Ballerina identifier.
Module names can only contain alphanumerics, underscores, and periods and the maximum length is 256 characters. For the modules belonging to the same package, the value of the first identifier is the package name.
You can add other modules using the
bal add command.
$ bal add util
.bal source files
The root of the module directory contains the source files of that module.
The package sources are the
.bal files in the
root directory, and
tests/ directory of the module. All other
.bal files are treated as standalone Ballerina files.
Module.md file provides a human-readable description of a module. When you visit a package in Ballerina Central, you should see all the exported modules of that package. It is the first page you will see when you navigate to an exported module of a package.
resources/ directory stores all module resources such as images, default configs, etc.
tests/ directory contains unit tests for the module and tests the module in isolation. The module-level test cases have access to the symbols with module-level visibility.
This directory contains generated Ballerina code. The
.bal files at the root of the generated directory become a part of the default module. Any direct subdirectory becomes a module in the package. The files will logically merge into the existing modules during compilation.