Gradle 多模块项目编译失败,"classNotFoundExceptions" 来自子模块中提到的依赖项
Gradle multi-module project compilation fails with "classNotFoundExceptions" from dependencies mentioned in sub-modules
我有一个 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
文件中我有一个 'mentioned' 子模块作为
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
来自我已经在子模块中提到的罐子
> 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 调用时编译失败。
- 我该如何处理?
- 是否因为从文件系统引用了一些本地 jar?
编辑
我发现指向子sub-module
的根sourceSets{}
编译失败,个别sub-module
编译成功。
但是,我需要这个 sourceSets{}
来创建一个包含所有 sub-module
的整体罐子
注意:- 我不会在这里创建 fatJar
或 shadedJar
,但我需要包含所有 sub-modules
的所有包没有依赖罐。
这就是我如何成功地从所有 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 项目的多模块设置,结构如下。
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
文件中我有一个 'mentioned' 子模块作为
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
来自我已经在子模块中提到的罐子
> 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 调用时编译失败。
- 我该如何处理?
- 是否因为从文件系统引用了一些本地 jar?
编辑
我发现指向子sub-module
的根sourceSets{}
编译失败,个别sub-module
编译成功。
但是,我需要这个 sourceSets{}
来创建一个包含所有 sub-module
注意:- 我不会在这里创建 fatJar
或 shadedJar
,但我需要包含所有 sub-modules
的所有包没有依赖罐。
这就是我如何成功地从所有 sub-module
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
}