How to create Jar file for both src/test and src/test code using gradle 8.2?

I want to create jar for my base project with src/main and src/test in the azure devops artifacts so that all other project use code and existing dependacnioes from my base project and we can pramote reusability through base project and avoid code duplication,

I have tried below steps but its not working . I cant see any depedancy added in the based project to another project. Any help highly appreciated. Thanks


task createJar(type: Jar) {
    project.logger.lifecycle("Starting Task: createJar")
    from sourceSets.test.output
    from sourceSets.main.output
}

configurations {
    sourcesJar
    sourcesTestJar
}

task sourcesJar(type: Jar) {
    from sourceSets.main.allSource
    archiveClassifier.set('sources')
}

task sourcesTestJar(type: Jar) {
    from sourceSets.test.allSource
    archiveClassifier.set('test-sources')

}

artifacts {
    sourcesJar sourcesJar
    sourcesTestJar sourcesTestJar
}


tasks.withType(Test) {

    jvmArgs '-Xmx4096m', '-Xms4096m'
    testLogging {
        events "FAILED",
                "SKIPPED",
                "STANDARD_ERROR",
                "STANDARD_OUT"
        exceptionFormat "FULL"
        showCauses true
        showExceptions true
        showStackTraces true
        showStandardStreams true
    }
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
            artifact sourceSets.test.java
            groupId = 'com.example'
            artifactId = 'your-artifact-id'
            version = '1.0.2'
            artifact sourcesJar
            artifact sourcesTestJar
            // Include test sources

        }
    }
    repositories {
        maven {
            url 'https://pkgs.dev.azure.com/maven/v1'
            credentials {
                username = ""
                password = ""
            }
        }
    }
}

I don’t understand why you should want to mix production and test classes in one jar?
Or why you should publish test classes at all, they are tests to test your production code.

If what you want is to provide some code that downstream project tests can reuse like base classes or fixtures, you could simply use the java test fixtures plugin which is exactly for this use-case.