How to make Multi-Release JAR Files with Gradle?

。_饼干妹妹 提交于 2019-12-03 05:16:30

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'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!