执行scala运行任务时,Gradle失败 [英] Gradle fails when executes run task for scala
问题描述
我创建了一个 HelloWorld
应用程序,我能够执行 build 任务,但无法执行 run 任务。
我得到以下错误:
无法找到或加载主类Demo
blockquote>
以下是我的build.gradle文件:
apply plugin:' scala'
repositories {
mavenCentral()
mavenLocal()
}
依赖关系{
compile'org.slf4j:slf4j-api:1.7.5 '
compile'org.scala-lang:scala-library:2.11.2
testCompilejunit:junit:4.11
}
任务运行(类型:JavaExec,dependsOn :classes){
main ='Demo'
}
我的源代码 Demo.scala :
对象演示{
def main(args:Array [String ]){
println(Helo World)
}
}
我的文件结构:
build
src\main\scala\Demo.scala
build.gradle
我正在使用gradle 3.0和Java 1.8.0_91。
在我看来,我缺少一些与 classpath 相关的设置。
任何帮助将不胜感激。解决方案您需要提供
classpath
以及。将run
任务声明更改为:
任务运行(类型:JavaExec, dependsOn:classes){
main ='Demo'
classpath = sourceSets.main.runtimeClasspath
}
它会正常工作。 演示。
打印类路径:
pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'
classpath = sourceSets.main.runtimeClasspath
doFirst {
classpath.each {println it}
}
}
I created a
HelloWorld
app and I was able to execute build task but fail to execute run task. I got the following error:Could not find or load main class Demo
The following is my build.gradle file:
apply plugin: 'scala' repositories{ mavenCentral() mavenLocal() } dependencies{ compile 'org.slf4j:slf4j-api:1.7.5' compile "org.scala-lang:scala-library:2.11.2" testCompile "junit:junit:4.11" } task run(type: JavaExec, dependsOn: classes) { main = 'Demo' }
My source code Demo.scala:
object Demo { def main(args: Array[String]) { println("Helo World") } }
My file structure:
build src\main\scala\Demo.scala build.gradle
I am using gradle 3.0 and Java 1.8.0_91. It looks to me that I am missing some settings related to classpath. Any help will be appreciated.
解决方案You need to provide the
classpath
as well. Changerun
task declaration to:task run(type: JavaExec, dependsOn: classes) { main = 'Demo' classpath = sourceSets.main.runtimeClasspath }
And it will work fine. Demo.
To print the classpath:
task run(type: JavaExec, dependsOn: classes) { main = 'Demo' classpath = sourceSets.main.runtimeClasspath doFirst { classpath.each { println it } } }
这篇关于执行scala运行任务时,Gradle失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!