Depends on the main and test code for the same Gradle dependency project

I support at least 2 Java Gradle projects. Let's say I have a common project called A and another project Bed and , which depends on A . A is technically common for many other projects, but this is not a minor detail for my problem. In any case, the current situation is that B declares the dependency on A as a simple external dependency in the build.gradle file, for example:

compile group:'com.example', name: 'A', version: '0.1'

We have test specific code in A that we share with B by putting it in src / main / java. This code should remain A instead of B , because there are other projects depending on A that use this test code. I would like not to put the code in this directory, because it is not intended for deployment in production, because of its test nature. Also, simply moving code from A to B would not be possible, because other projects in my organization depend on the same code, and I want to avoid code duplication. I would prefer to move this test code to A in src / test / java, but then it will not be published in the fileA jar.

Thus, I am trying to find a solution in which this code is in src / test / java in A and is deployed in a JAR file for testing purposes only. There are posts discussing possible solutions, such as this blog post or SOF posts, such as Multi-project-dependent test dependencies with gradle and multi-project dependencies with gradle . HOWEVER , I am constantly confronted with a problem caused by the fact that I do not support the multi-design Gradle construct, but rather two completely separate single-design Gradle assemblies, and this cannot change.

Where am I now, I'm setting up build.gradle for A in the exact way suggested by the blog, for example:

configurations {
    testOutput.extendsFrom testCompile
}

task jarTest (type: Jar, dependsOn: testClasses) {
    from sourceSets.test.output
    classifier = 'test'
}

artifacts {
    testOutput jarTest
}

A, compile testCompile build.gradle B:

compile(group: 'com.example', name: 'A', version: '0.1')
testCompile(group: 'com.example', name: 'A', version: '0.1', configuration: 'testOutput')

. testCompile, B; testCompile gradle dependencies IDE (IntelliJ). " " configuration: 'testOutput' classifier: 'test', . Gradle , , , , , .

, Gradle, JAR A? .

+4
1

, , , , (, JUnit Mockito): .

A ( ):

  • --

A :

dependencies {
    testCompile project(':A-test-support')
}

B :

dependencies {
    compile(group: 'com.example', name: 'A', version: '0.1')
    testCompile(group: 'com.example', name: 'A-test-support', version: '0.1')
}
0

Source: https://habr.com/ru/post/1614351/


All Articles