Skip to content

Instantly share code, notes, and snippets.

@aalmiray
Created October 15, 2014 13:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aalmiray/ccf26a213cb70b671078 to your computer and use it in GitHub Desktop.
Save aalmiray/ccf26a213cb70b671078 to your computer and use it in GitHub Desktop.
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
Copy link

akhikhl commented Oct 15, 2014

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

@bmuschko
Copy link

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
Copy link

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