我有一个具有以下结构的 Java 项目的多模块设置。
mainApp
|--> core-module
| |--> src
| |--> build.gradle
|--> implementation group: 'org.apache.axis2', name: 'axis2-kernel', version: '1.7.8'
|--> implementation group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.13'
|--> implementation files('libs/some_local.jar')
|--> compile project(":lib-module")
|--> compile project(":lib-another-module")
|--> ...
|
|--> lib-module
| |--> src
| |--> build.gradle
|--> implementation group: 'commons-lang', name: 'commons-lang', version: '2.6'
|--> implementation files('libs/another_local.jar')
|--> ...
|--> lib-another-module
| |--> src
| |--> build.gradle
|--> implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
|--> implementation files('libs/another_local.jar')
|--> ...
|
|--> settings.gradle
|--> build.gradle
|--> gradle.properties
在 mainApp/build.gradle
文件我有一个“提到”的子模块sourceSets {
main {
java {
srcDirs project('core-module').file("src")
srcDirs project('lib-module').file("src")
srcDirs project('lib-another-module').file("src")
}
}
}
如果我 构建单个子模块,编译成功但是当我运行 ./gradlew compileJava
在 root ,它失败了 classNotFoundExceptions
来自我已经在子模块中提到的 jar
> Task :core-module:compileJava
//successful
> Task :lib-module:compileJava
//successful
> Task :lib-another-module:compileJava
//successful
> Task :compileJava
/.../core-module/src/java/../OAuthUtils.java:12: error: package org.apache.http does not exist
import org.apache.http.Header;
...
//FAILED!
这里./gradlew core-module:compileJava
完美运行,但从 root 调用时编译失败。编辑
我发现root的编译
sourceSets{}
指向 child sub-module
失败和个人sub-module
编译成功。但是,我需要这个
sourceSets{}
用于创建一个整体 jar sub-module
注意:- 我不是在创建 fatJar
或 shadedJar
在这里,但我需要包含所有 sub-modules
中的所有包没有依赖 jar 。
最佳答案
这就是我成功地从所有 sub-module
中创建一个整体 jar 的方法
jar {
dependsOn compileJava
manifest {
def userName = System.properties['user.name']
def javaVersion = JavaVersion.current()
attributes 'Manifest-Version' : "1.0"
attributes 'Built-By' : userName
attributes 'Created-By' : "Java Version: " + javaVersion
}
destinationDir(file("$buildDir/image"))
baseName(jarName)
from project('core-module').sourceSets.main.output.classesDirs
from project('lib-module').sourceSets.main.output.classesDirs
from project('lib-another-module').sourceSets.main.output.classesDirs
}
关于java - Gradle 多模块项目编译失败,来自子模块中提到的依赖项的 "classNotFoundExceptions",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63933696/