Gradle multi-module project compilation fails with "classNotFoundExceptions" from dependencies mentioned in sub-modules

huangapple go评论62阅读模式
英文:

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 时,它会因为在子模块中已经提到过的 JAR 包引发 classNotFoundExceptions 错误。


> Task :core-module:compileJava
//编译成功
> Task :lib-module:compileJava
//编译成功
> Task :lib-another-module:compileJava
//编译成功
> Task :compileJava
/.../core-module/src/java/../OAuthUtils.java:12: error: package org.apache.http does not exist
import org.apache.http.Header;
...
//失败!

在这里,./gradlew core-module:compileJava 是完美工作的,但从根目录调用编译时会失败。

  1. 我该如何处理这个问题?
  2. 是否因为从文件系统引用了一些本地的 JAR 包?

编辑

我发现指向子 sub-module 的根 sourceSets{} 的编译失败,而单独的 sub-module 编译是成功的。

但是,我需要这个 sourceSets{} 来创建包含所有 sub-module 的一个单体 JAR 包。

注意: 我这里不是在创建 fatJarshadedJar,但我需要在不包含依赖 JAR 包的情况下包括所有 sub-module 的所有包。

英文:

I have a multi-module setup for a Java project with following structure.

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

In mainApp/build.gradle file I have a 'mentioned' sub-modules as

sourceSets {
    main {
        java {
            srcDirs project('core-module').file("src")
            srcDirs project('lib-module').file("src")
            srcDirs project('lib-another-module').file("src")
        }
    }
}

If I build individual sub-module it get compiled successfully but when I run ./gradlew compileJava at root , it fails with classNotFoundExceptions from the jars which I've already mentioned in sub-modules


> 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!

Here ./gradlew core-module:compileJava works perfectly but compilation fails when invoked from root.

  1. How shall I deal with it?
  2. Is it because of some local jars referred from file-system?

EDIT

I found that the compilation of root sourceSets{} which is pointing to child sub-module is failing and individual sub-module compilation is successful.

But, I need this sourceSets{} for creating one monolithic jar of all sub-module

Note:- I'm not creating fatJar or shadedJar here, But I need to include all packages from all sub-modules without dependency jars.

答案1

得分: 0

这是我成功创建单个大型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
}
英文:

This how I'm successfully able to create the one monolithic jar from all 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
  
}

huangapple
  • 本文由 发表于 2020年9月17日 15:58:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63933696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定