How do you create new modules? I always copy-paste...
# gradle
p
How do you create new modules? I always copy-paste a skeleton and then manually change the packagename and add it to the settings.gradle.kts - that's lot of boilerplate - has someone a solution to this already?
1
g
Well, I think if you include any folder in the settings.gradle it will become a module, if this folder exists. But maybe you’re asking if we have a tool to do something like that?
t
for the
settings.gradle
I have following:
Copy code
// Define dirs pattern to skip traversing
def skipDirs = ~/^(build|\..*|src|out|config)/

def preDir = {
    if (skipDirs.matcher(it.name).matches()) return FileVisitResult.SKIP_SUBTREE
}

def getProjectName(String dir) {
    return (dir - (rootDir.toString() + '/')).replaceAll('/', ':')
}

def getProjectBuildFile(String dir) {
    return ((dir - (rootDir.toString() + '/')).replaceAll('/', '-')) + '.gradle'
}

rootDir.traverse(type: FileType.DIRECTORIES, preDir: preDir) { dir->
    String dstr = dir.toString()
    if (new File(dstr + '/' + getProjectBuildFile(dstr)).exists()) {
        def projectName = getProjectName(dstr)
        <http://logger.info|logger.info>("Including module from ${dstr} with project name: ${projectName}")
        include projectName
        project(':' + projectName).buildFileName = getProjectBuildFile(dstr)
    }
}
that searches starting from the root folder and automatically adds modules.
and for creating a module you may write a Gradle task for it
for the modules
build.gradle
I am using bootstrap file:
Copy code
apply plugin: 'java-library'
apply plugin: 'kotlin'
apply from: "${project.rootDir}/config/quality.gradle"

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

test {
    useJUnitPlatform()
}

dependencies {
    api Libs.kotlinStandardLibrary

    testImplementation Libs.junit
    testRuntimeOnly Libs.junitVintage
}
Then in main `build.gradle`:
Copy code
buildscript {
        ext.bootstrap = [
            kotlinLibrary  : "${project.rootDir}/config/kotlin-library-bootstrap.gradle",
    ]
}
and then module
build.gradle
looks following:
Copy code
apply from: bootstrap.kotlinLibrary
really simplifies module creation