【问题标题】:Gradle multi-module project compilation fails with "classNotFoundExceptions" from dependencies mentioned in sub-modulesGradle 多模块项目编译因子模块中提到的依赖项中的“classNotFoundExceptions”而失败
【发布时间】:2020-09-18 17:20:19
【问题描述】:

我有一个具有以下结构的 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")
        }
    }
}

如果我构建单个子模块,它会成功编译,但是当我在 root 运行 ./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 core-module:compileJava 可以完美运行,但是从 root 调用时编译失败。

  1. 我该如何处理?
  2. 是因为文件系统引用了一些本地 jars 吗?

编辑

我发现根sourceSets{}指向子sub-module的编译失败,个别sub-module编译成功。

但是,我需要这个 sourceSets{} 来创建所有 sub-module 的一个整体罐子

注意:-我不是在这里创建fatJar 或shadedJar,但我需要包含来自所有sub-modules 的所有包,而不需要依赖jar。

【问题讨论】:

  • 希望对你有帮助 -> stackoverflow.com/questions/50106128/…
  • 在我的情况下完全相反。我的dependencies 对于不同的模块是不同的。因此,我不能把它集中在parent 模块中。其次,我的个人子模块 > Task :core-module:compileJava 正在编译,但作为一个整体项目编译失败,这有点奇怪?
  • 在sub-module 中添加了jar { enabled = true },但没有运气。
  • 对不起,我的错。我不是在这里创建fatJar 或shadedJar,但我需要包含来自所有sub-modules 的包

标签: java spring hibernate gradle build.gradle


【解决方案1】:

这就是我成功地从所有 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
  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 2016-02-15
    • 1970-01-01
    • 2018-10-15
    相关资源
    最近更新 更多