【问题标题】:Gradle equivalent of Surefire classpathDependencyExcludeGradle 等效于 Surefire 类路径DependencyExclude
【发布时间】:2015-11-19 10:06:09
【问题描述】:

我正在尝试将 java 项目从 maven 迁移到 gradle。问题是现在测试的类路径依赖配置非常棘手。

我们的 maven-surefire-plugin 配置:

 <includes>
     <include>**/SomeTest1.java</include>
 </includes>
 <classpathDependencyExcludes>
     <classpathDependencyExclude>com.sun.jersey:jersey-core</classpathDependencyExclude>
 </classpathDependencyExcludes>

不同的测试类有不同的类路径。如何使用 Gradle 实现它?

【问题讨论】:

    标签: java maven gradle build maven-surefire-plugin


    【解决方案1】:

    使用下一个解决方法:

    1. 为所需测试创建源集
    2. 为创建的源集添加配置
    3. 使用自定义配置添加运行测试任务
    4. 配置测试任务dependOn自定义测试任务
    5. 配置报告插件以生成漂亮的 html 报告:)

    点赞getting started

    【讨论】:

      【解决方案2】:

      首先,您需要将测试源分成不同的源集。比如说,我们需要从包 org.foo.test.rest 运行测试,运行时类路径与其他测试略有不同。因此,它的执行将转到 otherTest,剩下的测试在 test 中:

      sourceSets {
          otherTest {
              java {
                  srcDir 'src/test/java'
                  include 'org/foo/test/rest/**'
              }
              resources {
                  srcDir 'src/test/java'
              }
          }
          test {
              java {
                  srcDir 'src/test/java'
                  exclude 'org/foo/rest/test/**'
              }
              resources {
                  srcDir 'src/test/java'
              }
          }
      }
      

      之后,您应该确保 otherTest 已正确设置所有必需的编译和运行时类路径:

      otherTestCompile sourceSets.main.output
      otherTestCompile configurations.testCompile
      otherTestCompile sourceSets.test.output
      otherTestRuntime configurations.testRuntime + configurations.testCompile
      

      最后一件事是从 test 中排除(或包含)不需要的运行时包:

      configurations {
          testRuntime {
              exclude group: 'org.conflicting.library'
          }
      }
      

      并为 otherTest 创建 Test Gradle 任务:

      task otherTest(type: Test) {
          testClassesDir = sourceSets.otherTest.output.classesDir
          classpath += sourceSets.otherTest.runtimeClasspath
      }
      
      check.dependsOn otherTest
      

      【讨论】:

      • 我这样做并得到了&gt; Could not find method otherTestCompile() for arguments [main classes] on project。有什么想法可能是错的吗?
      猜你喜欢
      • 1970-01-01
      • 2013-12-13
      • 2021-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多