How to create jar for both src/test and src/main code using gradle 7.6?

I want to create dependancy for base project with src/main and src/test in the azure devops artifacts so that all other project use structure within that project. How to create artifact using gradle 7.6 and use it in the other projects ?

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

Step 1: Create the Base Project

Set up your base project:
Create a new directory for your base project and set up the standard Gradle project structure with src/main/java and src/test/java directories.

Apply the Java plugin:

plugins {
    id 'java'
}

Create a custom task for building the combined JAR:

task customJar(type: Jar) {
    from sourceSets.main.output
    from sourceSets.test.output
    archiveClassifier.set('all-in-one')
    version = '1.0' // Replace with your desired version
}

Configure the publishing to Azure DevOps Artifacts:

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId = 'com.example.base' // Replace with your desired group ID for the base project
            artifactId = 'base-library' // Replace with your desired artifact ID for the base project
            version = customJar.version
            artifact customJar
        }
    }

    repositories {
        maven {
            name = "AzureDevOpsArtifacts"
            url = uri("https://pkgs.dev.azure.com/your-organization/_packaging/your-feed/maven/v1") // Replace with your Azure DevOps Artifacts feed URL
            credentials {
                username = project.findProperty("azureArtifactsUsername") ?: System.getenv("AZURE_ARTIFACTS_USERNAME")
                password = project.findProperty("azureArtifactsPassword") ?: System.getenv("AZURE_ARTIFACTS_PASSWORD")
            }
        }
    }
}

Publish the base project artifact:
./gradlew publish

Step 2: Use the Base Project as a Dependency in Other Projects

dependencies {
    implementation 'com.example.base:base-library:1.0' // Replace with the appropriate version of the base project
}

Build your other projects to resolve the dependency from Azure DevOps Artifacts:

./gradlew build

As said in your other post, you just publish a plain artifact, so of course no dependency information is published. You should probably just configure the normal jar task to include the test source set output to achieve what you showed.

Actually, you should maybe instead consider using the test fixtures plugin, that might be more appropriate for what you want to achieve.

Thanks for your reply. Can you please help with the code?

Probably, if you are stuck and ask concrete questions. :wink:
Start with reading the docs of the test fixtures plugin, as it most probably is exactly what you want I’d guess.