我创建了一个Web应用程序。我正在使用Gradle。我是Gradle的新手,我正在尝试将我的一个项目迁移到Java 9模块系统。我把所有的jar都移到了模块路径而不是classpath。这是我的build.gradle的片段
plugins {
id 'java-library'
id 'eclipse-wtp'
id 'war'
}
ext {
//logging
log4jGroupId = "org.apache.logging.log4j"
log4jVersion = "2.11.0"
.....
moduleName = "pk.training.basit.kanban"
}
dependencies {
// Unit Testing
testImplementation group: 'junit', name: 'junit', version: junitVersion
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junitJupiterEngineVersion
implementation group: log4jGroupId, name: 'log4j-api', version: log4jVersion
.....
['log4j-core', 'log4j-jcl', 'log4j-slf4j-impl', 'log4j-web'].each {
runtime "${log4jGroupId}:$it:${log4jVersion}"
}
implementation group: 'javax.transaction', name: 'javax.transaction-api', version: javaxTransactionApiVersion
implementation (group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: jaxbVersion) {
exclude group: 'org.jvnet.staxex', module: 'stax-ex'
}
...
}
eclipse.classpath.file {
whenMerged {
entries.findAll { isModule(it) }.each { it.entryAttributes['module'] = 'true' }
}
}
boolean isModule(entry) {
// filter java 9 modules
entry.kind == 'lib' // Only libraries can be modules
}
eclipse.wtp.facet.file.withXml { facetXml ->
def root = facetXml.asNode()
root.installed.find { it.@facet == 'jst.java' }.@version = '9'
root.installed.find { it.@facet == 'jst.web' }.@version = '3.1'
}
compileJava {
inputs.property("moduleName", moduleName)
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
]
classpath = files() // Clears the classpath property by creating an empty file collection
}
}
compileTestJava {
inputs.property("moduleName", moduleName)
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath, // This is the default value for the classpath property used as the --module-path.
'--add-modules', 'org.junit.jupiter.api', // junit5 automatic module specific
'--add-modules', 'java.xml.bind', // jaxb specific
'--add-reads', "$moduleName=org.junit.jupiter.api", // allow junit to read your module
'--patch-module', "$moduleName=" + files(sourceSets.test.java.srcDirs).asPath, // Adds the test source files to the org.gradle.actors module.
]
classpath = files()
}
}
test {
inputs.property("moduleName", moduleName)
doFirst {
jvmArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'ALL-MODULE-PATH',
'--add-reads', "$moduleName=org.junit.jupiter.api",
'--patch-module', "$moduleName=" + files(sourceSets.test.java.outputDir).asPath,
]
classpath = files()
}
}
现在,如果我执行gradle build
或buildDependents
或testClasses
任务,则会出现以下错误
module not found: junit --> requires junit;
module not found: log4j.api --> requires log4j.api;
module not found: org.apache.logging.log4j.web --> requires org.apache.logging.log4j.web;
error: the unnamed module reads package javax.transaction.xa from both java.sql and javax.transaction.api
error: the unnamed module reads package javax.activation from both java.activation and activation
error: the unnamed module reads package com.sun.xml.bind from both jaxb.runtime and jaxb.core
error: module spring.web reads package javax.transaction.xa from both javax.transaction.api and java.sql
error: module spring.security.core reads package javax.transaction.xa from both javax.transaction.api and java.sql
....
那么为什么我为jUnit
,log4j.api
和org.apache.logging.log4j.web
获取模块未找到例外。我检查了模块路径,并且罐子在那里。
其次,我读到了关于拆分包的内容。我的问题是,我从错误中知道,两个罐子是java.sql and javax.transaction.api
和jaxb.runtime and jaxb.core
,与其他罐子相似。
我可以创建一个Gradle任务,将这两个jar合并为一个,并在模块路径中包含那个jar,这样可以删除这些错误吗?或者还有另一种解决此类错误的方法吗?
是否可以将一些罐子移动到模块路径,将一些罐子移到类路径?以及如何找到移动模块路径的罐子和移动类路径的罐子?
谢谢