Building a Kotlin + Java 9 project with Gradle

前端 未结 3 1375
刺人心
刺人心 2021-02-02 13:22

I\'m fairly new to Gradle (and Java 9, to be honest), and I\'m trying to use Gradle to build a simple library project that is a mix of Java 9 and Kotlin. More in detail, there i

3条回答
  •  盖世英雄少女心
    2021-02-02 14:02

    I am using the following gradle script where I put the module-info.java under src/module. It gets automatically included in the jar (without duplicates):

    if (JavaVersion.current() >= JavaVersion.VERSION_1_9) {
        subprojects {
            def srcModule = "src/module"
            def moduleInfo = file("${project.projectDir}/$srcModule/module-info.java")
            if (moduleInfo.exists()) {
    
                sourceSets {
                    module {
                        java {
                            srcDirs = [srcModule]
                            compileClasspath = main.compileClasspath
                            sourceCompatibility = '9'
                            targetCompatibility = '9'
                        }
                    }
                    main {
                        kotlin { srcDirs += [srcModule] }
                    }
                }
    
                compileModuleJava.configure {
                    dependsOn compileKotlin
                    destinationDir = compileKotlin.destinationDir
                    doFirst {
                        options.compilerArgs = ['--module-path', classpath.asPath,]
                        classpath = files()
                    }
                }
                jar.dependsOn compileModuleJava
            }
        }
    }
    

    I won't update it any longer, have a look at https://github.com/robstoll/atrium/blob/master/build.gradle to see the current version in use.

提交回复
热议问题