Axe*_*xel 6 java gradle java-platform-module-system build.gradle java-9
所以 Java 9 就在那里,很快就会有 Java 10。是时候让我们的库准备好在 Java 9 项目中使用了。我是通过以下方式做到的:
到目前为止,两种方法都运行良好,我可以在 Java 9 项目中使用生成的 Jar。
问题是,生成的 Jar 与 Java 8 不兼容,尽管除了module-info.java. 当我设置时targetCompatibility = 8,一条错误消息告诉我也进行相应的设置sourceCompatibility = 8。然后拒绝module-info.java我应该设置的sourceCompatibility = 9。
如何解决这个问题?
我再次删除了拼图插件,并尝试了这个,但卡住了:
sourceCompatibility = 8和targetCompatibility = 8 moduleInfo包含单个文件的新源集module-info.javasourceCompatibility = 9并targetCompatibility = 9为新的源集现在编译工作了,Gradle 在尝试编译module-info.java. 但是,缺少模块(在本例中为 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
Run Code Online (Sandbox Code Playgroud)
这是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'
Run Code Online (Sandbox Code Playgroud)
这是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;
}
Run Code Online (Sandbox Code Playgroud)
这个问题已经有一年多了,但以防万一有人在这里绊倒,自版本1.5.0以来, Gradle Modules Plugin现在支持此功能。
使用此插件,您不必创建自定义源集,而只需要调用modularity.mixedJavaRelease方法。
以下是如何将插件应用到 main 的示例build.gradle:
plugins {
// your remaining plugins here
id 'org.javamodularity.moduleplugin' version '1.5.0' apply false
}
subprojects {
// your remaining subproject configuration here
apply plugin: 'org.javamodularity.moduleplugin'
modularity.mixedJavaRelease 8 // sets "--release 8" for main code, and "--release 9" for "module-info.java"
// test.moduleOptions.runOnClasspath = true // optional (if you want your tests to still run on classpath)
}
Run Code Online (Sandbox Code Playgroud)
好吧,我终于成功了。如果其他人想知道如何做到这一点,这就是我所做的:
将 Java 版本设置为 8,以便 Java 8 应用程序可以使用该库:
源兼容性 = 8
目标兼容性 = 8
配置模块名称
ext.moduleName=com.dua3.utility
添加一个仅包含以下内容的新源集module-info.java:
sourceSets {
moduleInfo {
java {
srcDir 'src/module-info/java'
}
}
}
Run Code Online (Sandbox Code Playgroud)设置 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
]
}
}
Run Code Online (Sandbox Code Playgroud)配置jar任务以包括moduleInfo:
jar
{
from sourceSets.main.output
from sourceSets.moduleInfo.output
}
Run Code Online (Sandbox Code Playgroud)如果您使用该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'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1310 次 |
| 最近记录: |