DEV Community

Cover image for JeKa: The Simplest Way to Start with Java for Real
Jerome Angibaud
Jerome Angibaud

Posted on

JeKa: The Simplest Way to Start with Java for Real

JeKa is a modern Java build tool focused on simplicity.

When starting with Java, developers usually just write some code, compile it, and run it. However, this is not enough to create useful Java applications or libraries that others can reuse. To build something practical in Java, we need to:

  • Use third-party libraries (e.g., Guava, Gson, Commons CLI, etc.)
  • Package and deploy the application/library so others can use it.

This is often done with traditional build tools like Maven or Gradle. These tools require significant learning and can involve tedious configuration.
A simpler alternative is JBang, which lets you write almost single-file Java apps and easily share them.

On the other hand, Jeka offers simple dependency management and deployment ala JBang while allowing you to build standard multi-class applications or libraries with proper tests included.

Scaffold a New Code Base

Prerequisite: Jeka must be installed.

Tip: To list available options, execute: jeka base: --doc.

To create a base structure, ready for starting coding right away, execute:

jeka base: scaffold scaffold.kind=APP
Enter fullscreen mode Exit fullscreen mode

You’ll get the following codebase layout:

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

All your Java code should be located in the jeka-src folder.

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

The scaffolded example includes an App class within the app package. You are free to add or modify classes in any package as you see fit.

Sync with IntelliJ

To synchronize with IntelliJ, execute:

jeka intellij: iml --force
Enter fullscreen mode Exit fullscreen mode

Add Dependencies

The App.java class uses the @JkDep annotation to reference a library. You can add as many libraries as needed. It’s a good practice to declare all dependencies 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 {

    public static void main(String[] args) {
        ...
    }
}
Enter fullscreen mode Exit fullscreen mode

See details on dependency notations.

Additionally, you can simply copy and paste JAR files into the following directory, and they will automatically be included as dependencies:

├── jeka-boot      <- Jars included in the production classpath.
Enter fullscreen mode Exit fullscreen mode

Declare non-prod dependencies

If you declare the @JkDev annotation within the _dev package, any dependencies it references will be excluded from the final production build.

package _dev;

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

Run your Application

The application can be run using:

jeka --program arg0 args1 ... # or `jeka -p` for short
Enter fullscreen mode Exit fullscreen mode

To clean the compilation before starting, use the --clean option.

If the source code is hosted in a Git repository, you can directly execute the application by referencing the repository as follows:

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

Create a Library

If you want to write a library, instead of an application, you need to declare both moduleId and versioning in order to publish it on a Maven repository:

@base.moduleId=org.example:my-lib
@base.version=1.0.0-SNAPSHOT
Enter fullscreen mode Exit fullscreen mode

Now, you can publish your library by executing:

jeka maven: publish
Enter fullscreen mode Exit fullscreen mode

Test your Code

The scaffolded code already contains a _dev.test.MyTest test class, ready to run.

. 
├── jeka-src           
│   ├── _dev             
│   │   └── test
│   │       └── MyTest.java

Enter fullscreen mode Exit fullscreen mode

You can add more tests in any package you like. However, keep in mind that test classes not located in the _dev package (or its sub-packages) will be included in the production JAR as dead code.

Change Java Version

Nowadays, Java evolves rapidly, with new releases every six months. JeKa provides a very convenient way to switch between Java versions. Simply specify the desired version in the jeka.properties file:

jeka.java.version=23
Enter fullscreen mode Exit fullscreen mode

This will automatically install JDK 23 during the next application compile or run.

Pre-defined Build Commands

Among others, Jeka provides the following commands:

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 
Enter fullscreen mode Exit fullscreen mode

Other commands:

jeka native: compile     # Compiles to native executable

jeka docker: build       # Builds a Docker image for the project
jeka docker: info        # Displays info about generated image.
jeka docker: buildNative # Builds a Docker image containing the native executable
jeka docker: infoNative  # Displays info about generated native image 

jeka maven: publishLocal # Publish artifacts in the local repository
jeka maven: publish      # Publish in the defined Maven repository
Enter fullscreen mode Exit fullscreen mode

Move to Full Project Structure

As the codebase is growing, you'll might find more confortable in using a full project structure.
Moving to project is easy, to figure out how to do, visit the Build Projects Tutorial.

Code with Kotlin

You can also write Kotlin code in your codebase. Just specify the Kotlin version you want to use in the jeka.properties file:

jeka.kotlin.version=2.1.0
Enter fullscreen mode Exit fullscreen mode

Now, you can edit Kotlin code as shown below, seamlessly integrate it with the existing Java code in your codebase, or even create a 100% Kotlin application:

class Greeting {

    fun sayHello() : String{
        return "hello"
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Jeka offers a practical mode to start writing real-life Java application without the burden of traditional build tools.

Its polymorphic structure allows handling increasing complexity and size gradually, without sacrificing simplicity.

Resoures:

Top comments (0)