Reuse test dependeny jars in parent project


(Kevin Peter) #1

Hi,

I have a multi project setup. In one of the child projects I have test dependency as below,

testCompile 'org.springframework:spring-test:3.2.16.RELEASE’
testCompile ‘junit:junit:4.12’

How I can reuse these two jars in parent project without redefining these two jars in build.gradle? Basically I don’t want to repeat the above two lines in all of my projects.


Application plugin generates wrong classpath for bare .class files
Pull project artifacts from maven repo
(Lance Java) #2

There’s a couple of options.

Option 1: Declare a variable in the parent project and use it in one or more projects

ext {
   libraries = [
      testCommon: ['org.springframework:spring-test:3.2.16.RELEASE', 'junit:junit:4.12'] 
   ] 
} 

project(':a') {
   dependencies {
      testCompile libraries.testCommon
   } 
}

project(':b') {
   dependencies {
      testCompile libraries.testCommon
   } 
}

Option 2: Declare in one project and re-use the configuration in another

project(':a') {
   configurations {
      testCommon
      testCompile.extendsFrom testCommon
   } 
   dependencies {
      testCommon 'org.springframework:spring-test:3.2.16.RELEASE'
      testCommon 'junit:junit:4.12'
   } 
}

project(':b') {
   dependencies {
      testCompile project(path: ':a', configuration: 'testCommon') 
   } 
}