Search code examples
gradlebuildmulti-modulebuild-system

Creating a single source jar for a multi-module gradle project


I have a multi-module gradle project setup like this:

Module A (Parent)
-build.gradle
-settings.gradle
+Module B
--src
---main
----java
----kotlin
--build.gradle
+Module C
--src
---main
----java
----kotlin
--build.gradle

In this instance, Module B has a compile time dependency on Module C.

I want to create a sources jar including the sources from all sub-projects to publish to my repository.

In single module builds I have added the following to my project, but adding it to all subprojects does not result in the collection of source sets in the sources jar produced:

sourceSets {
    main.kotlin.srcDirs += 'src/main/kotlin'
    main.java.srcDirs += 'src/main/java'
}

task sourcesJar(type: Jar) {
    from sourceSets.main.kotlin
    from sourceSets.main.java
    archiveClassifier = 'sources'
}

How can I do this? ShadowJar works but unfortunately does not provide the option to create a sources jar as far as I know.


Solution

  • We have found a solution - deploy the sub-modules as independent sources jar and include them on an individual basis.

    For example, in each sub-module:

    sourceSets {
        main.kotlin.srcDirs += 'src/main/kotlin'
        main.java.srcDirs += 'src/main/java'
    }
    
    task sourcesJar(type: Jar) {
        from sourceSets.main.kotlin
        from sourceSets.main.java
        archiveClassifier = 'sources'
    }
    
    publishing {
        publications {
            mavenJava(MavenPublication) {
                groupId = rootProject.group
                artifactId = "$rootProject.name-$project.name"
                version = rootProject.project.version
                from components.java
                artifact sourcesJar
            }
        }