Hello. I am trying to create custom template inspired by hexagonal architecture for kotlin gradle pr...
f
Hello. I am trying to create custom template inspired by hexagonal architecture for kotlin gradle projects. I want to have 3 source sets:
main
,
adapter
and
port
, where
port
I want to take advantage of kotlin
internal
access modifier to hide
adapter
declarations from
port
. This is my current buildscript:
Copy code
plugins {
    kotlin("jvm") version "1.6.10"
}

group = "hex"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("stdlib"))
}

kotlin {
    sourceSets {
        val ports by creating {
        }

        val adapters by creating {
            dependsOn(ports)
        }

        main.get().apply {
            dependsOn(ports)
            dependsOn(adapters)
        }
    }

}
After importing gradle project in intellij I see that it recognizes new sourcesets but internal declarations in
adapter
are accessible from
port
sources so it’s not treated as a module described here https://kotlinlang.org/docs/visibility-modifiers.html#modules It looks like source sets are created in gradle model, but they are used by kotlin as one collective source set, at least that’s what i think is happening. So i want to achieve a gradle build with one project (just root, no subprojects), one compilation, but 3 source sets (depending on each other as shown by
dependsOn
s). What am I missing to get that working as I expect?
1
e
What's your reason not to use gradle sub-projects? Seems like the tool for the job, at least that's how I did it on my hexagonal/clean architecture. https://github.com/ESchouten/CleanArchitecture
j
internal hides the adapter to other modules, but not inside the same module
t
dependsOn
exists for multiplatform sourcesets
f
@ESchouten yes, I could do that, but keeping these sources in one gradle project seems more clean for me so I want to go that way. It seems to me that source sets are some kind of code organisation unit between gradle project (module) and packages. @Javier I guess you mean gradle project as a module. But the kotlin docs state that for gradle module = sourceset, not a gradle project/subproject
@tapchicoma I get it, I suspected that it won’t help me achieve project i described in original post. But added that to show what dependency structure I’m trying to create here
j
yeah, gradle project
f
I found the solution. I was declaring sourcesets in
kotlin { sourceSets { … } }
block. Turns out that source sets created that way aren’t recognized as modules by kotlin plugin. The correct way to do it is to create sourcesets in
sourceSets { … }
which I believe come from the java plugin. It would be helpful if there was documentation explaining how exactly the kotlin gradle project model is structured because right now it’s misleading when we have
SourceSet
and
KotlinSourceSet
types and we don’t know the difference betweeen them