首页 > 解决方案 > Gradle 在运行任务上设置了不正确(意外?)的类路径

问题描述

我的项目结构是这样的:

projectRoot
  +-src
     +-main
     |   +-java
     |   |   +-package/java files go here
     |   +-resources
     |       +-config
     |           +-files go here
     +-test
         +-java
         |   +-package/java files go here
         +-resources
             +-config
             |   +-files go here
             +-features
                 +-files go here

构建此项目时,它会生成以下输出结构:

projectRoot
  +-out
      +-production
      |     +-classes
      |     |   +-package/classes in here
      |     +-resources
      |         +-config
      |             +-files in here
      +-test
            +-classes
            |   +-package/classes in here
            +-resources
                +-config
                |   +-files in here
                +-features
                    +-files in here

这一切都在预料之中。我定义了一个任务来运行黄瓜测试,如下所示:

task runCucumber() {
    doLast {
        javaexec {
            main = "cucumber.api.cli.Main"
            args += ['--plugin', 'pretty', '--plugin', 'html:out/reports/cucumber/', '--plugin', 'json:out/reports/cucumber/report.json',
                     '--glue', 'com.example.mypackage.cucumber', 'classpath:features'
                     , '--tags', 'not @ignore']
            systemProperties['http.keepAlive'] = 'false'
            systemProperties['http.maxRedirects'] = '20'
            systemProperties['env'] = System.getProperty("env")
            systemProperties['envType'] = System.getProperty("envType")
            classpath = configurations.cucumber + configurations.compile + configurations.testCompile + sourceSets.main.runtimeClasspath + sourceSets.test.runtimeClasspath
        }
    }
}

当我执行此任务时,找不到我的类,也找不到功能。显然,这是由于类路径设置不正确。如果我使用 运行任务--info,我可以看到类路径是使用我指示的参数设置的,但是out它包含目录而不是build目录。我希望类路径是:

<all dependencies>:/projectRoot/out/production/classses:/projectRoot/out/production/resources:/projectRoot/out/test/classes:/projectRoot/out/test/resources

然而,类路径包含以下内容:

<all dependencies>:/projectRoot/build/classes/java/main:/projectRoot/build/classes/java/test:

有趣的是,build在项目根目录下根本没有生成目录。如何设置类路径以便可以找到类和资源?

标签: javagradleclasspath

解决方案


好的,原来问题不在于 Gradle,而在于 IntelliJ IDEA。IDEA 覆盖了标准 gradle 路径,并将输出保存在与 gradle 预期不同的路径中。然后,当下一个 gradle 任务运行时,输出不存在。直接使用gradle而不是IDEA编译解决了这个问题。


推荐阅读