Java Gradle多模块项目编译失败,返回“0”;classNotFoundExceptions“;来自子模块中提到的依赖项

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

我有一个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
时,它会失败,因为我在子模块中已经提到过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
工作正常,但从根目录调用时编译失败

  • 我该怎么处理呢
  • 是因为文件系统引用了一些本地JAR吗
  • 编辑

    我发现指向子
    子模块
    的根
    源集{}
    的编译失败,单个
    子模块
    编译成功

    但是,我需要这个
    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
    ,但我需要包含所有
    子模块的包