Gradle 在 运行 任务上设置了不正确的(意外的?)类路径
Gradle sets incorrect (unexpected?) classpath on running task
我的项目结构是这样的:
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
的任务,我可以看到类路径是使用我指定的参数设置的,但是它包含 build
目录而不是 out
目录。我希望类路径为:
<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
。如何设置类路径以便可以找到 类 和资源?
好的,原来问题不在于 Gradle,而在于 IntelliJ IDEA。 IDEA 正在覆盖标准 gradle 路径并将输出保存在与 gradle 预期的路径不同的路径中。然后,当下一个 gradle 任务 运行 时,输出不存在。直接使用gradle而不是IDEA编译解决了问题。
我的项目结构是这样的:
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
的任务,我可以看到类路径是使用我指定的参数设置的,但是它包含 build
目录而不是 out
目录。我希望类路径为:
<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
。如何设置类路径以便可以找到 类 和资源?
好的,原来问题不在于 Gradle,而在于 IntelliJ IDEA。 IDEA 正在覆盖标准 gradle 路径并将输出保存在与 gradle 预期的路径不同的路径中。然后,当下一个 gradle 任务 运行 时,输出不存在。直接使用gradle而不是IDEA编译解决了问题。