Skip to content

Build Base

In this tutorial, we'll use the base KBean to build a Java application or library.

This mode provides a layout between a single-file style like JBang and a full Maven/Gradle project.

Visit this repository to have a concrete example.

Prerequisite: Jeka must be installed.

Tip

Run jeka base: --doc to see all available options.

Scaffold a New Code Base

Run jeka base: scaffold scaffold.kind=APP to create a base structure, ready for you to start coding right away.

You’ll get the following project structure:

. 
├── jeka-src             <- Source root directory
│   ├── _dev             <- Optional package containing all non-prod (build and test)
│   │   ├── test
│   │   └── Build.java  
│   └── app              <- Sugested base package for production code/resources
│       └── App.java     
├── jeka-output          <- Generated dir where artifacts as jars, classes, reports or doc are generated
├── jeka.properties      <- Build configuration  (Java and jeka version, kben configurations, ...)
└── README.md            <- Describes available build commands

All your Java code is supposed to be in the jeka-src folder.

_dev is a special package for source code and dependencies used only for development (e.g., tests, builds). If you're new to Java, you can ignore or delete it.

The scaffolded example includes an App class in the app package.
You can add or modify classes in any package you like.

Sync with IntelliJ

Run: jeka intellij: iml --force to sync the project with IntelliJ.
If changes don't appear in IntelliJ, go to the project's root directory, then run: jeka intellij: initProject.

Add Dependencies

The App.java class declares a @JkDep annotation to reference a library. You can add as many libraries as needed. A good practice is to declare all libraries in the same base class.

@JkDep("com.github.lalyos:jfiglet:0.0.9")
@JkDep("com.fasterxml.jackson:jackson-bom::pom:2.18.2")
@JkDep("com.fasterxml.jackson.core:jackson-core")
@JkDep("com.fasterxml.jackson.core:jackson-annotations")
public class App {
    ...
}

See details on dependency notations.

Additionally, you can copy-paste JAR files into the following directory to automatically include them as dependencies:

├── jeka-boot      <- Jars included in the production classpath.

Declare non-prod dependencies

Declare dependency on any class under _dev package, to add dependency with embedding them in production.

import sun.lwawt.macosx.CWarningWindow;

@JkDep("org.junit.jupiter:junit-jupiter:5.11.4")
@JkDep("org.mockito:mockito-junit-jupiter:5.15.2")
class Build extends KBean {
    ...
}

Reminder

Don't forget to run jeka intellij: iml once you have modified the dependencies.

Run your Application

The application can be run using:

jeka --program arg0 args1 ... # or `jeka -p` for short
To clean compilation before starting, use --clean option (-c for short).

If ths source code is hosted in a Git repo, the application can be directly executed by referencing the repo as:

jeka --remote [git repo url] --program arg0 arg1 ... # or jeka -r [git repo url] -p

Make it Native

To compile in native, execute:

jeka native: compile
When done, execution of jeka --program ... will run the native version instead of the Java one.

Notes

You may set this properties if your application needs some resources to run

@native.includeAllResources=true

If you want to force native build, set the following property:

jeka.properties
jeka.program.build=native: compile

Dockerize

If you want to create a Docker image of your application, execute:

jeka docker: build
This creates a Docker image based and registers it on your local Docker daemon. The console output explains how to execute it.

To create a Docker image based on native executable, execute:

jeka docker: buildNative

Pre-defined Build Commands

From *base* KBean
jeka base: test       # Runs tests
jeka base: pack       # Runs tests + creates jars
jeka base: runJar     # Runs the jar generated by the above command
jeka base: info       # Displays project configuration info
jeka base: depTree    # Displays dependency trees 

See here for extra commands.

Create a Library

If you want to write a library, instead of an application, you need to declare both moduleId and versioning.

The settings are similar for project building. Use 'base' instead of 'project', like this:

@base.moduleId=org.example:my-lib
@base.version=1.0.0-SNAPSHOT

Now, you can publish your library by executing:

jeka maven: publish