Gradle从"test"依赖jar开始运行测试

sum*_*n j 5 gradle

任何人都可以在gradle构建中从"测试"依赖项jar运行测试吗?我有一个gradle构建脚本,其中包含一些test-jar以及testRuntime依赖项.我想使用"gradle test"在这些依赖项中运行测试.

我看到gradle没有开箱即用的解决方案来运行这个链接中提到的jar测试.我想按照这篇文章中提出的"解包"选项进行操作.我不确定如何将解包任务与测试任务联系起来迭代所有测试jar依赖项并运行测试?PS:我知道我们不必在使用项目时运行依赖项测试.但出于我的原因,我必须这样做.

任何gradle专家如何实现这一目标?

[编辑]
我使用下面的代码从jar运行测试.但我想要的是一个通用任务,例如"runTestsFromDependencyJars",它会遍历所有测试配置依赖项并运行测试.不知道如何让它运行所有这些依赖项?

    task unzip(type: Copy )  {
        from zipTree(file('jar file with absolute path'))
        into file("$temporaryDir/")
    }

    task testFromJar(type: Test , dependsOn: unzip) {
        doFirst {
            testClassesDir=file("$temporaryDir/../unzip/")
            classpath=files(testClassesDir)+sourceSets.main.compileClasspath+sourceSets.test.compileClasspath
        }
    }
Run Code Online (Sandbox Code Playgroud)

sum*_*n j 2

使用 ant junit 方法找到了解决方案。

 configurations {
        testsFromJar {
        transitive = false
        }
        junitAnt
 }

 dependencies {
        junitAnt('org.apache.ant:ant-junit:1.9.3') {
            transitive = false
        }
        junitAnt('org.apache.ant:ant-junit4:1.9.3') {
            transitive = false
        }

    compile "groupid:artifact1name:version"
    compile "groupid:artifact2name:version"
    testsFromJar ( group:'groupid', name:'artifact1 name', version:'version',classifier:'tests')
    testsFromJar ( group:'groupid', name:'artifact2 name', version:'version',classifier:'tests')

 }
 ant.taskdef(name: 'junit', classname: 'org.apache.tools.ant.taskdefs.optional.junit.JUnitTask',
            classpath: configurations.junitAnt.asPath)


task runTestsFromJar(  ) << {
        configurations.testsFromJar.each {
            file ->
                ant.junit(printsummary:'on', fork:'yes', showoutput:'yes', haltonfailure:'yes') { //configure junit task as per your need
                    formatter (type:'xml')
                    batchtest(todir:"$temporaryDir", skipNonTests:'true' ) {
                        zipfileset(src:file,
                                includes:"**/*Test.class",
                        )
                    }
                    classpath {
                        fileset(file:file)
                        pathelement(path:sourceSets.main.compileClasspath.asPath+sourceSets.test.compileClasspath.asPath)
                    }
                }
        }
    }
Run Code Online (Sandbox Code Playgroud)