15

Java9 introduces with Multi-Release JARs.

Let's say that I have multimodule Gradle project using java8:

project-root
      settings.gradle
      build.gradle 
      /module1
          /src
          ... (common maven structure)
      /module2
      /module3

This is a common multi-module project in Gradle. Let's say I need MR-Jar for module1.

I can't add module1-java9 targeting Java9 because the base is on 8 - so far, both my Gradle and IntelliJ IDEA complains. Gradle is compiled with the java8, but I need to enable java9 runtime just for the module (don't know how); in IntelliJ IDEA I can set the java9 runtime for the module, but it gets overwritten every time when gradle config is reloaded.

Moreover, even if I add it somehow, I need to instruct module1 to 1) build second and 2) to include module1-java9. Here is a drawing of this situation:

project-root
      settings.gradle
      build.gradle 
      /module1
      /module1-java9  (added java9 module)
      /module2
      ...

Alternatively, this could be done under module1, having different sources: src and src-java9. but I doubt this would ever be accepted. Here is a drawing:

project-root
      settings.gradle
      build.gradle 
      /module1
          /src
          /src-java9 (added java9 source folder)
      /module2
      /module3

So far I only see that module1-java9 is a separate project (not a module) and that module1 simply calls the gradle there and takes the output. Yeah, it's gradle calling gradle :)))

Is there any developer-friendly way to do this?

igr
  • 10,199
  • 13
  • 65
  • 111
  • Whats there with `mod` and `mod1` in the question? Also, though not gradle, I was able to create a similar java-module [sample here](https://github.com/namannigam/jdk9-mrjar). Over the point "*So far I only see that module1-java9 is separate project (not a module)*". In the project shared there is just one project with multiple modules. – Naman Dec 05 '17 at 17:33
  • 2
    @nullpointer fixed 'mod' names. Anyway, your example is IDE dependent, and I am aware of it. The question asks for gradle solution. – igr Dec 05 '17 at 17:59
  • 5
    There is a long article in the Gradle Blog about multi-release JARs: https://blog.gradle.org/mrjars – Stephen C Aug 04 '18 at 12:06
  • 2
    And in the gradle article above, there is a github [repo](https://github.com/melix/mrjar-gradle) that contains an example implementation... – m4gic Jan 04 '19 at 16:24

1 Answers1

2

As mentioned in the comments on the question, this blog post and the associated example project describe how to create a multi-release JAR with Gradle.

In case the blog post or example project should go away, you may also refer to the following setup which was derived from the example project and tailored a bit to the setup that is given in the question (as far as details are provided).

Overview

project-root/
├── build.gradle
├── module1
│   └── src
│       └── main
│           ├── java
│           │   └── com
│           │       └── acme
│           │           ├── JdkSpecific.java
│           │           └── Shared.java
│           └── java9
│               └── com
│                   └── acme
│                       └── JdkSpecific.java
├── module2
│   └── whatever
├── module3
│   └── whatever
└── settings.gradle

build.gradle

allprojects {
    apply plugin: 'java'

    compileJava {
        sourceCompatibility = 8
        targetCompatibility = 8
    }
}

dependencies {
    implementation project(':module1')
}

project(':module1') {
    sourceSets {
        java9 {
            java {
                srcDirs = ['src/main/java9']
            }
        }
    }

    compileJava9Java {
        sourceCompatibility = 9
        targetCompatibility = 9
    }

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

    jar {
        into('META-INF/versions/9') {
            from sourceSets.java9.output
        }
        manifest.attributes('Multi-Release': 'true')
    }
}

settings.gradle

include 'module1', 'module2', 'module3'
Chriki
  • 15,638
  • 3
  • 51
  • 66