适用于Android的Gradle:javaexec为什么不选择我的类路径? [英] Gradle for Android: Why doesn't javaexec pick up my classpath?

查看:94
本文介绍了适用于Android的Gradle:javaexec为什么不选择我的类路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android项目(在Windows上),试图在其中运行 cucumber-jvm 作为非仪表的单元测试。即当我运行 gradlew测试时,执行Cucumber功能。

I have an Android project (on Windows) where I am trying to run cucumber-jvm as a non-instrumented "unit test". I.e. execute Cucumber features when I run gradlew test.

以下是我的应用程序 build.gradle :

android {
    ...
    testOptions {
        unitTests.all {
            javaexec {
                main = "cucumber.api.cli.Main"
                classpath = getClasspath()
                args = ['--plugin', 'pretty', '--glue', 'gradle.cucumber', 'src/test/java/cucumber/assets']
            }
        }
    }
}
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    testImplementation 'io.cucumber:cucumber-java:3.0.2'
    testImplementation 'io.cucumber:cucumber-junit:3.0.2'
}

当我在命令行中运行 gradlew test --info ,我得到以下错误:

When I run gradlew test --info at the command-line I get the following error:

Starting process 'command 'C:\Program Files\Java\jdk1.8.0_162\bin\java.exe''. Working directory: C:\dev\urig\android-cucumber\app Command: C:\Program Files\Java\jdk1.8.0_162\bin\java.exe -Dfile.encoding=windows-1252 -Duser.country=US -Duser.language=en -Duser.variant cucumber.api.cli.Main --plugin pretty --glue gradle.cucumber src/test/java/cucumber/assets
Successfully started process 'command 'C:\Program Files\Java\jdk1.8.0_162\bin\java.exe''
Error: Could not find or load main class cucumber.api.cli.Main

在我看来,该命令不包含类路径,我的问题是-为什么?

It looks to me like the command contains no classpath and my question is - Why?

PS -我已经验证了在调用 javaexec 时进行的调用c $ c> getClasspath()实际上包含所有与Groovy相关的依赖: println getClasspath()。any {println it}

PS - I've verified that at the time of the call to javaexec the call to getClasspath() indeed contains all the dependencies with this little bit of Groovy: println getClasspath().any { println it }

PPS -我知道cumulum-jvm的预期用途是用于仪器测试,使用黄瓜android 。我有一个将Cucumber作为本地单元测试运行的特定用例(Android术语,不是我的),因此上述内容对我没有帮助。

PPS - I know the intended use of cucumber-jvm is for instrumented tests using cucumber-android. I have a specific use case for running Cucumber as a "local unit test" (Android terms, not mine) so the above doesn't quite help me.

推荐答案

我相信我已经找到解决问题的方法。以下是对我有用的代码:

I believe I've found a solution for my issue. Here's the code that works for me:

testOptions {
    unitTests.all {
        def classpath2 = getClasspath()
        javaexec {
            main = "cucumber.api.cli.Main"
            classpath = classpath2
            args = ['--plugin', 'pretty', '--glue', 'gradle.cucumber', 'src/test/java/cucumber/assets']
        }
    }
}

在我看来,我最初在 javaexec <中调用 getClassPath() / code>关闭返回一个空文件集合。同时,在 unitTests.all 的闭包中, getClassPath()包含正确的类路径。

It seems to me that my original call to getClassPath() inside the javaexec closure was returning an empty file collection. At the same time, in the closure for unitTests.all, getClassPath() contains the correct classpath.

通过变量 cucumber.api.cli.Main 现在成功运行,并且我的Cucumber功能作为Gradle test 任务的一部分运行。

By passing the classpath from the external closure into the internal closure via a variable, cucumber.api.cli.Main now runs successfully and my Cucumber features run as part of the Gradle test task.

这篇关于适用于Android的Gradle:javaexec为什么不选择我的类路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆