3

I have been scratching my head for awhile trying to set up my library to use multi-release jars to use java 9+ features with backup java 8 implementations. However, it is only needed for a module of my project.

My current build.gradle for the module looks like this:

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
        )
    }
}

However refreshing my build.gradle in intellij is getting me this error: Could not find method java9Implementation() for arguments [file collection] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

I should also note this is on gradle 4.8.1.

austinv11
  • 33
  • 1
  • 3
  • 1
    [This link](https://github.com/melix/mrjar-gradle#building-a-mrjar-with-gradle) could be of help to you and [this as well](https://blog.gradle.org/mrjars#how-to-create-a-multi-release-jar-with-gradle)? – Naman Jul 14 '18 at 17:44
  • @nullpointer My build.gradle is based on those links actually. I've also found a couple other build.gradles, and as far as I can tell there shouldn't be anything wrong--but there is. – austinv11 Jul 14 '18 at 19:16

1 Answers1

1

You're problem is, that your sourceSet is defined after the dependencies block. Put it above dependencies and it should work.

The reference you used also explains it:

A source set represents a set of sources that are going to be compiled together. A jar is built from the result of the compilation of one or more source sets. For each source set, Gradle will automatically create a corresponding compile task that you can configure. This means that if we have sources for Java 8 and sources for Java 9, then they should live in separate source sets. That’s what we do by creating a Java 9 specific source set that will contain the specialized version of our class.

So before you can use 'java9'Implementation, gradle needs to know what 'java9' is supposed to mean.

Younes El Ouarti
  • 2,200
  • 2
  • 20
  • 31