3

マルチリリースjarを使用してJava 9+機能をバックアップJava 8実装で使用するようにライブラリをセットアップしようとして、しばらく頭を悩ませていました。ただし、それは私のプロジェクトのモジュールにのみ必要です。

モジュールの現在の build.gradle は次のようになります。

apply plugin: 'java'
apply plugin: 'java-library'

dependencies {
    compile project(":common")

    compile 'org.ow2.asm:asm:6.2'
    compile 'org.ow2.asm:asm-util:6.2'

    testCompile "junit:junit:$junit_version"
    testCompileOnly "com.google.auto.service:auto-service:$autoservice_version"

    java9Implementation files(sourceSets.main.output.classesDirs) { builtBy compileJava }
}

compileJava {
    sourceCompatibility = 8
    targetCompatibility = 8
    options.compilerArgs.addAll(['--release', '8'])
}

compileJava9Java {
    sourceCompatibility = 9
    targetCompatibility = 9
    options.compilerArgs.addAll(['--release', '9'])
}

sourceSets {
    java9 {
        java {
            srcDirs = ['src/main/java9']
        }
    }

}

jar {
    into('META-INF/versions/9') {
        from sourceSets.java9.output
    }

    manifest {
        attributes(
                "Manifest-Version": "1.0",
                "Multi-Release": true
        )
    }
}

ただし、intellij で build.gradle を更新すると、次のエラーが発生します。 Could not find method java9Implementation() for arguments [file collection] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

これはgradle 4.8.1上にあることにも注意してください。

4

1 に答える 1