Balage1551
Balage1551

Reputation: 1067

Using testCompile output from other subproject (Gradle Kotlin DSL)

I have some utility files in the test sources in one of my gradle subproject and would like to use them in an other subproject. My "source" subproject is called core, while the one uses it is called tem.

I try to migrate and integrate the following example:

In your Server project:

configurations {
    testArtifacts.extendsFrom testCompile
}
task testJar(type: Jar) {
    classifier "test"
    from sourceSets.test.output
}
artifacts {
    testArtifacts testJar
}

In your ServerWeb project:

testCompile project(path: ":Server", configuration: 'testArtifacts')

As far as I get is making the conversation. I added the following to my core.gradle.kts:

val testConfig = configurations.create("testArtifacts") {
    extendsFrom(configurations["testCompile"])
}

tasks.register("testJar", Jar::class.java) {
    classifier += "test"
    from(sourceSets["test"].output)
}

artifacts {
    add("testArtifacts", tasks.named<Jar>("testJar") )
}

And tried to refer to it in tem.gradle.kts:

testImplementation(project(":core", "testArtifacts"))

It compiles, but I still can't access the classes from core.

Where did I miss something?

Upvotes: 4

Views: 1617

Answers (2)

user12096006
user12096006

Reputation: 21

The following configuration worked for me to include both the test classes and test resources:

core build.gradle.kts

val testConfig = configurations.create("testArtifacts") {
    extendsFrom(configurations["testCompile"])
}   

tasks.register("testJar", Jar::class.java) {
    dependsOn("testClasses")
    classifier += "test"
    from(sourceSets["test"].output)
}   

artifacts { 
    add("testArtifacts", tasks.named<Jar>("testJar") )
} 

tem build.gradle.kts

testImplementation(project(":core", "testArtifacts"))

Upvotes: 2

Raino Kolk
Raino Kolk

Reputation: 91

Most of your code should be OK

But you must define classesDirs for jar

tasks.register<Jar>("testJar") {
    dependsOn("testClasses")
    archiveBaseName.set("${project.name}-test")
    from(sourceSets["test"].output.classesDirs)
}

I also added depends on testClasses to be sure that classes are compiled.

You can test that jar is OK by executing testJar task. Then verify that generated jar contains your classes. If you make mistake with from method call then you get empty jar.

Upvotes: 3

Related Questions