如何使用Gradle制作多版本的JAR文件? [英] How to make Multi-Release JAR Files with Gradle?
问题描述
Java9引入了多版本JAR .
假设我有一个使用java8的多模块Gradle项目:
Let's say that I have multimodule Gradle project using java8:
project-root
settings.gradle
build.gradle
/module1
/src
... (common maven structure)
/module2
/module3
这是Gradle中常见的多模块项目.假设module1
我需要MR-Jar.
This is a common multi-module project in Gradle. Let's say I need MR-Jar for module1
.
我无法添加针对Java9的module1-java9
,因为它的基数是8-到目前为止,我的Gradle和IntelliJ IDEA都在抱怨. Gradle是使用java8编译的,但是我只需要为模块启用java9运行时(不知道如何);在IntelliJ IDEA中,我可以为模块设置java9运行时,但是每次重装gradle配置时,它都会被覆盖.
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.
此外,即使我以某种方式添加它,我也需要指示module1
到1)建立第二个,以及2)包含module1-java9
.这是这种情况的图形:
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
...
或者,这可以在module1
下完成,其来源不同:src
和src-java9
.但我怀疑这会不会被接受.这是一张图纸:
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
到目前为止,我只看到module1-java9
是一个单独的项目(不是模块),并且module1
只是在此处调用gradle并获取输出.是的,这是gradle调用gradle:)))
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?
推荐答案
如问题评论中所述,此博客文章和相关的示例项目描述了如何创建多版本带有Gradle的JAR.
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).
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'
这篇关于如何使用Gradle制作多版本的JAR文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!