0

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.

Swapnil Kotwal
  • 5,418
  • 6
  • 48
  • 92
  • I hope it helps -> https://stackoverflow.com/questions/50106128/how-to-declare-common-dependencies-in-multimodule-gradle-project-on-parent-folde – Gurkan İlleez Sep 17 '20 at 08:49
  • It's exactly reverse in my case. My `dependencies` are different for different module. Hence, I can't put it centrally in `parent` module. Second, my individual sub-module `> Task :core-module:compileJava` is compiling but as a whole project compilation is failing which is sort of weird here? – Swapnil Kotwal Sep 17 '20 at 12:37
  • Check out this one: https://stackoverflow.com/questions/63923161/java-lang-noclassdeffounderror-at-class-java-2?noredirect=1#comment113036969_63923161 – Onur Baştürk Sep 17 '20 at 12:54
  • Added `jar { enabled = true }` in `sub-module` but no luck. – Swapnil Kotwal Sep 17 '20 at 13:22
  • Sorry, my bad. I'm not creating `fatJar` or `shadedJar` here, But I need to include packages from all `sub-modules` – Swapnil Kotwal Sep 17 '20 at 13:58
  • this what I'm looking for https://stackoverflow.com/a/48027456/1665592 thnx everyone – Swapnil Kotwal Sep 17 '20 at 14:39

1 Answers1

0

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
  
}
Swapnil Kotwal
  • 5,418
  • 6
  • 48
  • 92