Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gradle multi-project build where project2's test classes depend on project1's test classes
.
├── build.gradle
├── project1
│ ├── build.gradle
│ └── src
│ └── test
│ └── java
│ └── sample
│ └── Foo.java
├── project2
│ ├── build.gradle
│ └── src
│ └── test
│ └── java
│ └── sample
│ └── BarTest.java
└── settings.gradle
== BarTest.java
package sample;
import org.junit.Test;
import static org.junit.Assert.*;
public class BarTest {
@Test
public void doit() {
assertNotNull(new Foo());
}
}
== build.gradle (project1)
task testJar(type: Jar) {
classifier 'test'
from sourceSets.test.output
}
== build.gradle (project2)
dependencies {
compile project(':project1')
testCompile files(project(':project1').testJar.archivePath)
}
@akhikhl

This comment has been minimized.

Copy link

@akhikhl akhikhl commented Oct 15, 2014

What about: testCompile project(':project1').sourceSets.test.output

@bmuschko

This comment has been minimized.

Copy link

@bmuschko bmuschko commented Oct 15, 2014

I'd actually add a configuration to project1 (e.g. tests) and then register the JAR task as outgoing artifact.

configurations {
    tests
}

artifacts {
    tests testJar
}

Then you reference it in project2 like this:

dependencies {
    testCompile project(path: ':project1', configuration: 'tests')
}
@ido-ran

This comment has been minimized.

Copy link

@ido-ran ido-ran commented Apr 11, 2018

First I think it's a very bad idea to design a project which depends on test code of another project.
Now that I've said it, I've migrated a large project from Maven to Gradle in which this bad idea was implemented and I've found that @bmuschko solution was the right way because it accounts for transitive dependencies while @akhikhl solution doesn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment