Gradle Test Dependency

前端 未结 7 1628
一整个雨季
一整个雨季 2020-11-28 04:26

I have two projects, project A and Project B. Both are written in groovy and use gradle as their build system.

Project A requires project B. This holds for both th

相关标签:
7条回答
  • 2020-11-28 04:34

    For Android on the latest gradle version (I'm currently on 2.14.1) you just need to add the below in Project B to get all the test dependencies from Project A.

    dependencies {
      androidTestComplie project(path: ':ProjectA')
    }
    
    0 讨论(0)
  • 2020-11-28 04:38

    This is a simpler solution that doesn't require an intermediate jar file:

    dependencies {
      ...
      testCompile project(':aProject').sourceSets.test.output
    }
    

    There's more discussion in this question: Multi-project test dependencies with gradle

    0 讨论(0)
  • 2020-11-28 04:42

    The above solution works, but not for the latest version 1.0-rc3 of Gradle.

         task testJar(type: Jar, dependsOn: testClasses) {
           baseName = "test-${project.archivesBaseName}"
    
           // in the latest version of Gradle 1.0-rc3
           // sourceSets.test.classes no longer works
           // It has been replaced with 
           // sourceSets.test.output
    
           from sourceSets.test.output
         }
    
    0 讨论(0)
  • 2020-11-28 04:45

    This works for me (Java)

    // use test classes from spring-common as dependency to tests of current module
    testCompile files(this.project(':spring-common').sourceSets.test.output)
    testCompile files(this.project(':spring-common').sourceSets.test.runtimeClasspath)
    
    // filter dublicated dependency for IDEA export
    def isClassesDependency(module) {
         (module instanceof org.gradle.plugins.ide.idea.model.ModuleLibrary) && module.classes.iterator()[0].url.toString().contains(rootProject.name)
    }
    
    idea {
          module {
              iml.whenMerged { module ->
                  module.dependencies.removeAll(module.dependencies.grep{isClassesDependency(it)})
                  module.dependencies*.exported = true
              }
          }
      }
    .....  
    // and somewhere to include test classes 
    testRuntime project(":spring-common")
    
    0 讨论(0)
  • 2020-11-28 04:49

    You can expose the test classes via a 'tests' configuration and then define a testCompile dependency on that configuration.

    I have this block for all java projects, which jars all test code:

    task testJar(type: Jar, dependsOn: testClasses) {
        baseName = "test-${project.archivesBaseName}"
        from sourceSets.test.output
    }
    
    configurations {
        tests
    }
    
    artifacts {
        tests testJar
    }
    

    Then when I have test code I want to access between projects I use

    dependencies {
        testCompile project(path: ':aProject', configuration: 'tests')
    }
    

    This is for Java; I'm assuming it should work for groovy as well.

    0 讨论(0)
  • 2020-11-28 04:50

    If ProjectA contains the test code you wish to use in ProjectB and ProjectB wants to use artifacts to include the test code, then ProjectB's build.gradle would look like this:

    dependencies {
    
      testCompile("com.example:projecta:1.0.0-SNAPSHOT:tests")
    
    }
    

    Then you need to add an archives command to the artifacts section in ProjectA's build.gradle:

    task testsJar(type: Jar, dependsOn: testClasses) {
        classifier = 'tests'
        from sourceSets.test.output
    }
    
    configurations {
        tests
    }
    
    artifacts {
        tests testsJar
        archives testsJar
    }
    
    jar.finalizedBy(testsJar)
    

    Now when ProjectA's artifacts are published to your artifactory they will include a -tests jar. This -tests jar can then be added as a testCompile dependency for ProjectB (as shown above).

    0 讨论(0)
提交回复
热议问题