更新时间:2023-01-15 15:14:19
好,我终于可以正常使用了.万一其他人想知道怎么做,这就是我所做的:
OK, I finally got it working. In case anyone else wants to know how to do it, this is what I have done:
将Java版本设置为8,以便该库可被Java 8应用程序使用:
set the Java version to 8, so that the library will be usable by Java 8 applications:
sourceCompatibility = 8
targetCompatibility = 8
sourceCompatibility = 8
targetCompatibility = 8
配置模块名称
ext.moduleName
= com.dua3.utility
添加仅包含module-info.java
的新源集:
sourceSets {
moduleInfo {
java {
srcDir 'src/module-info/java'
}
}
}
为moduleInfo,sourceSet,配置模块和设置输出目录设置与Java 9的兼容性:
set compatibility to Java 9 for the moduleInfo, sourceSet, configure modules, and set the output directory:
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
:
configure the jar
task to include moduleInfo
:
jar
{
from sourceSets.main.output
from sourceSets.moduleInfo.output
}
如果使用的是SpotBugs
插件,则还必须显式配置sourceSet,因为否则它将在尝试处理ModuleInfo sourceSet时失败.
In case you are using the SpotBugs
plugin, you also have to configure the sourceSet explicitly because it will otherwise fail when it tries to process the ModuleInfo sourceSet.
我最终得到了这个版本的build.gradle
:
I finally ended up with this version of 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'