所以 Java 9 已经存在,Java 10 很快就会紧随其后。是时候让我们的库准备好在 Java 9 项目中使用了。我是通过以下方式做到的:
- 提供一个module-info.java
- 添加了(实验性的)jigsaw plugin在
build.gradle
中
- 根据 guide 手动更改在 gradle 网站上,而不是使用拼图插件。
到目前为止,这两种方法都可以正常工作,我可以在 Java 9 项目中使用生成的 Jar。
问题是,生成的 Jar 与 Java 8 不兼容,尽管我除了 module-info.java
外没有使用任何 Java 9 功能。当我设置 targetCompatibility = 8
时,一条错误消息告诉我还相应地设置 sourceCompatibility = 8
。然后拒绝我应该为其设置 sourceCompatibility = 9
的 module-info.java
。
如何解决?
我再次删除了拼图插件,并尝试了这个,但卡住了:
- 设置
sourceCompatibility = 8
和targetCompatibility = 8
- 创建一个包含单个文件
module-info.java
的新源集moduleInfo
- 为新的源集设置
sourceCompatibility = 9
和targetCompatibility = 9
现在编译工作正常,Gradle 在尝试编译 module-info.java
时使用 Java 9。但是,缺少模块(在本例中为 log4j),我收到此错误:
:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
:jar UP-TO-DATE
:sourcesJar UP-TO-DATE
:assemble UP-TO-DATE
:spotbugsMain UP-TO-DATE
:compileModuleInfoJava
classpath:
compilerArgs: [--module-path, , --add-modules, ALL-SYSTEM]
D:\git\utility\src\module-info\java\module-info.java:14: error: module not found: org.apache.logging.log4j
requires org.apache.logging.log4j;
^
warning: using incubating module(s): jdk.incubator.httpclient
1 error
1 warning
:compileModuleInfoJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileModuleInfoJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
5 actionable tasks: 1 executed, 4 up-to-date
这是使用的build.gradle
(Gradle版本是4.5.1):
plugins {
id "com.github.spotbugs" version "1.6.0"
}
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'java-library'
apply plugin: 'com.github.spotbugs'
sourceCompatibility = 8
targetCompatibility = 8
group = 'com.dua3.utility'
repositories {
mavenLocal()
jcenter()
}
dependencies {
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.10.0'
testRuntime group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.10.0'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
ext.moduleName = 'com.dua3.utility'
sourceSets {
moduleInfo {
java {
srcDir 'src/module-info/java'
}
}
}
compileModuleInfoJava {
sourceCompatibility = 9
targetCompatibility = 9
inputs.property("moduleName", moduleName)
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'ALL-SYSTEM'
]
classpath = files()
System.out.println("classpath: "+classpath.asPath)
System.out.println("compilerArgs: "+options.compilerArgs)
}
}
tasks.withType(com.github.spotbugs.SpotBugsTask) {
reports {
xml.enabled false
html.enabled true
}
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
// fails with jigsaw: archives javadocJar
}
defaultTasks 'build', 'publishToMavenLocal', 'install'
这是module-info.java
:
module com.dua3.utility {
exports com.dua3.utility;
exports com.dua3.utility.io;
exports com.dua3.utility.jfx;
exports com.dua3.utility.swing;
exports com.dua3.utility.lang;
exports com.dua3.utility.math;
exports com.dua3.utility.text;
requires javafx.controls;
requires javafx.web;
requires java.xml;
requires java.desktop;
requires org.apache.logging.log4j;
}
最佳答案
好的,我终于让它工作了。如果其他人想知道怎么做,这就是我所做的:
将 Java 版本设置为 8,以便 Java 8 应用程序可以使用该库:
来源兼容性= 8
targetCompatibility = 8配置模块名称
ext.moduleName
=com.dua3.utility
添加一个仅由
module-info.java
组成的新源集:sourceSets { moduleInfo { java { srcDir 'src/module-info/java' } } }
为 moduleInfo、sourceSet 设置与 Java 9 的兼容性,配置模块,并设置输出目录:
compileModuleInfoJava { sourceCompatibility = 9 targetCompatibility = 9 inputs.property("moduleName", moduleName) doFirst { classpath += sourceSets.main.compileClasspath options.compilerArgs = [ '--module-path', classpath.asPath, '--add-modules', 'ALL-SYSTEM,org.apache.logging.log4j', '-d', sourceSets.main.output.classesDirs.asPath ] } }
配置
jar
任务以包含moduleInfo
:jar { from sourceSets.main.output from sourceSets.moduleInfo.output }
如果您使用的是 SpotBugs
插件,您还必须显式配置 sourceSet,否则它会在尝试处理 ModuleInfo sourceSet 时失败。
我最终得到了这个版本的build.gradle
:
plugins {
id "com.github.spotbugs" version "1.6.0"
}
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'java-library'
apply plugin: 'com.github.spotbugs'
sourceCompatibility = 8
targetCompatibility = 8
group = 'com.dua3.utility'
repositories {
mavenLocal()
jcenter()
}
dependencies {
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.10.0'
testRuntime group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.10.0'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
ext.moduleName = 'com.dua3.utility'
sourceSets {
moduleInfo {
java {
srcDir 'src/module-info/java'
}
}
}
compileModuleInfoJava {
sourceCompatibility = 9
targetCompatibility = 9
inputs.property("moduleName", moduleName)
doFirst {
classpath += sourceSets.main.compileClasspath
options.compilerArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'ALL-SYSTEM',
'-d', sourceSets.main.output.classesDirs.asPath
]
}
}
jar
{
from sourceSets.main.output
from sourceSets.moduleInfo.output
}
spotbugs {
sourceSets = [sourceSets.main]
}
tasks.withType(com.github.spotbugs.SpotBugsTask) {
reports {
xml.enabled false
html.enabled true
}
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
defaultTasks 'build', 'publishToMavenLocal', 'install'
关于java - Gradle:构建与 Java 8 兼容的模块化库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48928723/