If you look at a typical Kotlin project, a significant amount of attention is drawn to Gradle for technical matters. Instead of just one file in the root of the project, there’s a whole bunch:

  • gradle/
  • buildSrc/
  • build.gradle.kts
  • gradle.properties
  • gradlew
  • gradlew.bat
  • settings.gradle.kts

The first problem is that on other platforms (such as TypeScript/Python), there’s usually just one file (and maybe an additional file for specific dependency versions). Even in Maven, there’s only one file.

The second issue is that if we have microservices, then there are multiple projects. It would be good to update them and somehow synchronize them.

So what should we do? Ideally, a new build system:

  • 1-2 configuration files
  • keep the possibility of easy scripting (why not Maven)
  • add SemVer dependency resolution like on other platforms

However, this is long, difficult, and probably beyond the capabilities of one person in a few days.

Therefore, a simpler approach would be to build a utility on top of Gradle that generates the files for Gradle. This way, Gradle will be the engine used but we’ll work directly with kbre (the build tool).

KBRE (Kotlin Build Rule Enforcer):

  • Generates Gradle files (including updates)
  • Generates a new project
  • Minimal technical details (more like start.spring.io than Gradle)
  • Confident: configured for its own preferences, not for everyone (otherwise minimal technical details won’t be achieved)
  • Easily configurable: preferences vary in different organizations/divisions, so it’s easy to customize
  • It’s important to separate common settings and project settings
  • Stores settings in ~/.kbre - recommended to put them under git and make them uniform for the organization/division
  • Explicit versions in generated files (then IDE can easily suggest that a dependency is outdated, but it needs to be fixed in kbre.yaml or ~/.kbre)
  • Potentially all Gradle files can be added to gitignore, but currently it’s recommended to keep them in git so that kbre only works when the build changes, not for the actual build itself.

Example of a settings file in kbre.yaml:

group: com.example
artifact: demo
name: Demo
description: Demo app
preset: spring
type: root
extensions:
  - webflux
  - mysql
  - redis
dependencies: |
  implementation("as:asd:1.2.3")
extra: |
  # any text

Structure of ~/.kbre:

preset1/
  root/
  extensions/
    yaml/
      gradle/
        libs.versions.toml
	  deps.kts
  extension2/
preset2/

Description of the settings directory (briefly, more details in the repository):

  • Presets come first (for example, spring, cli, or ktor) - these are like templates in IDE
  • Types - root for the root project, others can be defined for subprojects of Gradle
  • Extensions. For each preset, there are its own extensions. They allow you to enable additional functionality: for example, we connect jooq with its own files in the bin folder, dependencies and custom code in build.gradle.kts.

To install KBRE, you can follow these steps:

cd ~
git clone https://github.com/stepin/kbre-default-config .krbe

alias kbre='docker run --rm -it -v $PWD:/data -w /data --user "$(id -u)" stepin/kbre'
kbre version

Default configuration repository: https://github.com/stepin/kbre-default-config

When new code is added to configuration repo It’s important to select following options:

  • if it’s obligatory or not (i.e. is it part of extension or not)
  • it it will be used for new or update stages (i.e. just folder or additional folder with -new suffix)

Create new project:

mkdir my-app
cd my-app

cat > kbre.yaml << \EOF
group: name.stepin
artifact: myapp
name: My app
description: Some description
preset: spring
type: root
extensions:
  - graphql
  - postgres
  - flyway
  - jooq
  - dokka
  - jib
  - local-dev
  - systemd-deployment
variables:
  REPO: 'http://localhost:3000/stepin/kotlin-bootstrap-app/src/branch/main/src/main/kotlin'
  SONAR_HOST_URL: 'http://localhost:9000'
  SONAR_PROJECT_KEY: kotlin-bootstrap-app
  SONAR_PROJECT_NAME: kotlin-bootstrap-app
  SONAR_TOKEN: sqp_821b1d3209761625bdd29259674237d429bce626
EOF

kbre new

Update of existing code:

cd my-app

kbre update

Code: https://github.com/stepin/kbre