Gradle custom test sourceSet

混江龙づ霸主 提交于 2020-01-23 07:25:11

问题


I have a gradle project and have main and test as sourceSets.

I do want test-main not to be part of test source sets. Now the issue I encounter is that when projects are build, the test-main is marked as Sources Root instead of Test Sources Root.

This leads to compilation errors and I have to manually mark test-mains as Source Test Root for all subprojects.

I created a task in order to enforce intellij to mark them as Sources Test Root but seems like I am doing something wrong.

hints:

  • Intellij IDEA 2016.2
  • Gradle 2.14

Thanks,

subprojects {
  apply plugin: 'java' // all our projects are Java projects

  sourceCompatibility = '1.8'
  targetCompatibility = '1.8'

  tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
    options.warnings = false // There are too many :-(
  }

  configurations {
    testMainsCompile.extendsFrom testCompile
    testMainsRuntime.extendsFrom testMainsCompile
  }

  sourceSets {
    main {
      java {
        srcDirs = ['src']
      }
      resources {
        srcDirs = ['src']
      }
    }
    test {
      java {
        srcDirs = ['test']
      }
      resources {
        srcDirs = ['test']
      }
    }
    testMains {
      java {
        srcDirs = ['test-mains']
        compileClasspath = test.output + main.output + configurations.testMainsCompile
        runtimeClasspath = output + compileClasspath + configurations.testMainsRuntime
      }
      resources {
        srcDirs = ['test-mains']
      }
    }
  }

  // dummy task just to convince intellij idea that testMains is a test class folder
  task testMainsTest(type: Test) {
    testClassesDir = sourceSets.testMains.output.classesDir
    classpath += sourceSets.testMains.runtimeClasspath
  }

[...]

}

回答1:


I had to specify for idea plugin this:

subprojects {
  apply plugin: 'idea'

  idea {
    module {
      testSourceDirs += file('test-mains')
    }
  }
}


来源:https://stackoverflow.com/questions/38369060/gradle-custom-test-sourceset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!