简体   繁体   中英

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

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.

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
  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM