How to make Multi-Release JAR Files with Gradle?

后端 未结 1 2096
礼貌的吻别
礼貌的吻别 2021-02-07 10:52

Java9 introduces with Multi-Release JARs.

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

project-root
      settings.gradle
      build.g         


        
相关标签:
1条回答
  • 2021-02-07 11:13

    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'
    
    0 讨论(0)
提交回复
热议问题