1. Introduction

In our Kotlin projects, we often encounter scenarios where we need to introduce new source code directories or source sets to organize our codebase effectively. This is where Gradle’s Kotlin DSL (Domain Specific Language) comes into play.

In this tutorial, we’ll explore how to add new source sets to our Gradle-based Kotlin projects using the Kotlin DSL syntax. By utilizing this approach, we can structure our code and dependencies efficiently, improving modularity and maintainability.

2. Creating a New SourceSet

In Gradle, the SourceSet is an essential component of the Java plugin that allows us to define and organize different sets of source code within our project. By creating new source sets, we can group related code and resources together, facilitating better organization and separation of concerns. Let’s see how to add a new source set using the Kotlin DSL syntax:

plugins {
    java
}

sourceSets {
    create("analytics") {
        java.srcDir("src/analytics/java")
        java.srcDir("src/analytics/kotlin")
        resources.srcDir("src/analytics/resources")
    }
}

In this code snippet, we utilize the sourceSets block to define a new source set named analytics. We specify the Java source directory using java.srcDir(“src/analytics/java”) and the resources directory using resources.srcDir(“src/analytics/resources”). This allows us to differentiate the code and resources specific to the analytics source set for the rest of our project.

By leveraging the flexibility provided by SourceSets, we can create a modular project structure that enhances maintainability and scalability. This enables us to manage different aspects of our codebase independently and apply specific configurations or dependencies to individual source sets.

3. Configuring Dependencies for the New SourceSet

Once we’ve created the new source set, we may need to add dependencies specific to that source set. We can achieve this by utilizing Kotlin DSL’s configuration block. Let’s see an example of how we can add dependencies to our analytics source set:

dependencies {
    sourceSets.named("analytics") {
        implementation("org.apache.commons:commons-lang3:3.12.0")
        // Additional dependencies for the analytics source set
    }
}

In this snippet, we access the analytics source set using the named() function and then specify the dependencies for that source set. This allows us to manage dependencies independently for each source set, ensuring modularity and isolation.

3.1. Automatically Generated Dependency Configuration

When it comes to adding dependencies to a specific source set using the Kotlin DSL, the Gradle Kotlin DSL provides a convenient way to access the source set and configure its dependencies without explicitly using named() to find the source set:

dependencies {
    "analyticsImplementation"("org.apache.commons:commons-math3:3.6.1")
}

In this code snippet, we use the Kotlin DSL’s dependency notation with the SourceSet name followed by Implementation as a suffix. For example, analyticsImplementation() refers to the analytics SourceSet. These dynamic dependency configuration functions are generated just in time by Gradle as it builds our project.

The auto-generated function follows the convention of <sourcesetName>Implementation, where <sourcesetName> is the name of the desired source set. It simplifies the process of adding dependencies to the source set by automatically associating the dependency with the correct source set without our having to explicitly reference it by name. We can use this technique to target any of the dependency configurations in our project.

By leveraging this functionality provided by the Kotlin DSL, we can easily manage dependencies for specific source sets, promoting modularity and allowing us to configure dependencies tailored to the needs of each source set within our Kotlin project.

4. Utilizing the New SourceSet

Apart from configuring tasks, we can also use the newly created source set as a dependency within our application. Let’s look at an example of how we can achieve this using the Kotlin DSL:

plugins {
    java
    application
    kotlin("jvm") version "1.8.22"
}

sourceSets {
    create("analytics") {
        java.srcDir("src/analytics/java")
        java.srcDir("src/analytics/kotlin")
        resources.srcDir("src/analytics/resources")
    }
}

dependencies {
    implementation(sourceSets.named("analytics").get().output)
}

application {
    mainClass.set("com.baeldung.SampleKt")
    applicationDefaultJvmArgs = listOf("-Dconfig.file=analytics.conf")
}

In this code snippet, we first apply the java and application plugins to our project. Next, we define the analytics SourceSet with its corresponding code and resources directories.

We then add the source set output as a dependency using implementation(sourceSets.named(“analytics”).get().output). This ensures that the compiled code from analytics is included as a dependency.

Additionally, we configure the application plugin by specifying the main class name and setting default JVM arguments.

By incorporating the new source set into the application build, we can utilize the specific code and resources within analytics directly in our application. This promotes modularity and allows us to structure our project in a way that aligns with our requirements and development needs.

5. Conclusion

In this article, we explored how to add new source sets to our Gradle-based Kotlin projects using the Kotlin DSL syntax. By creating new source sets, configuring dependencies, and utilizing them within the application, we can effectively organize and manage our codebase. This promotes modularity, improves code maintainability, and allows us to handle specific aspects of our project separately.

Leveraging Gradle Kotlin DSL’s flexibility and expressiveness, we can adapt our build configuration to meet the unique requirements of our Kotlin projects. As always, the code used in this article is available over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.