In gradle multi-project buil, how to build single jar and publish it to maven?

I asked the same question on stackoverflow, but did not receive a fully available answer. :pensive:

In simple terms, I want the jar file I generated to include class files from other projects, while keeping third-party dependencies imported through the pom file as normal. Then publish it to the maven repository.

Looking forward to your help. :smile:

I have tried this:

configurations {
    api.extendsFrom(internal)
}

dependencies {
    internal project(':server-public')

    implementation 'org.springframework:spring-web'
}

jar {
    from { configurations.internal.collect { it.isDirectory() ? it : zipTree(it) } }
}

But there is still some problem, such as:

  • My internal project is still placed in the pom file.
  • Content from other projects does not appear in sources.jar and javadoc.jar.

After constantly trying, I wrote this configuration.

buildscript {
    ext {
        internalDependencies = [
                project(':public'),
                project(':server-public')
        ]
    }
}

apply plugin: 'maven-publish'
apply plugin: 'signing'

dependencies {
    internalDependencies.each {
        internal it
    }

    implementation 'org.springframework:spring-web'
}

jar {
    from {
        configurations.internal.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
}

task sourcesJar(type: Jar) {
    from sourceSets.main.allJava
    from {
        internalDependencies.collect {
            it.sourceSets.main.allJava
        }
    }
    archiveClassifier = 'sources'
}

javadoc {
    if (JavaVersion.current().isJava9Compatible()) {
        options.addBooleanOption('html5', true)
    }
    source internalDependencies.collect { it.sourceSets.main.allJava }
    classpath += files(internalDependencies.collect { it.sourceSets.main.compileClasspath })
}

task javadocJar(type: Jar) {
    from javadoc
    archiveClassifier = 'javadoc'
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
            artifact sourcesJar
            artifact javadocJar
            pom {
                name = 'Server Client Library'
            }
            pom.withXml {
                Node pomNode = asNode()
                pomNode.dependencies.'*'.each {
                    Node temp = it
                    internalDependencies.each {
                        if (temp.groupId.text() == it.getGroup() && temp.artifactId.text() == it.getName()) {
                            temp.parent().remove(temp)
                            return false
                        }
                    }
                }
            }
        }
    }
}

signing {
    sign publishing.publications.mavenJava
}

This has basically met my needs. But it’s not elegant enough.

Is there a way to get projects through my custom configuration?

This will eliminates the need to list dependencies or even transitive dependencies through a list.