Java Gradle多模块项目编译失败,返回“0”;classNotFoundExceptions“;来自子模块中提到的依赖项
我有一个Java项目的多模块设置,结构如下Java Gradle多模块项目编译失败,返回“0”;classNotFoundExceptions“;来自子模块中提到的依赖项,java,spring,hibernate,gradle,build.gradle,Java,Spring,Hibernate,Gradle,Build.gradle,我有一个Java项目的多模块设置,结构如下 mainApp |--> core-module | |--> src | |--> build.gradle |--> implementation group: 'org.apache.axis2', name: 'axis2-kernel', version: '1.7.8' |--> implementation group: 'org.apach
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
时,它会失败,因为我在子模块中已经提到过JAR中出现了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核心模块:compileJava
工作正常,但从根目录调用时编译失败
子模块
的根源集{}
的编译失败,单个子模块
编译成功
但是,我需要这个sourceset{}
来创建一个包含所有子模块的单片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
}
注意:-我不是在这里创建
fatJar
或shadedJar
,但我需要包含所有子模块的所有包,而不包含依赖项jar。这就是我如何成功地从所有子模块创建一个单片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
}
我希望这有帮助->我的情况正好相反。我的依赖项
对于不同的模块是不同的。因此,我不能将它集中放在父模块中。第二,我的单个子模块任务:核心模块:compileJava
正在编译,但作为一个整体,项目编译失败了,这有点奇怪吗?看看这个:在子模块
中添加了jar{enabled=true}
,但运气不好。对不起,我的坏消息。这里我没有创建fatJar
或shadedJar
,但我需要包含所有子模块的包