我有以下scala编译问题
java source - >取决于scala来源
我的scala代码位于src / main / scala
我无法更改此代码,所以我需要使用gradle编译它,它目前正在编译JRuby就好了。
我已阅读以下有关如何解决此问题的帖子:
我将此添加到我的构建中:
ext {
baseName = 'd2'
description = 'Divisional IVR.'
combinedSources = "$buildDir/combined-sources"
}
apply plugin: 'scala'
compileScala.taskDependencies.values = compileScala.taskDependencies.values - 'compileJava'
compileJava.dependsOn compileScala
sourceSets.main.scala.srcDir "$combinedSources"
sourceSets.main.java.srcDirs = []
我尝试将所有scala和java文件复制到一个位置:
compileScala.dependsOn{
copyAllSourceFiles
}
task copyAllSourceFiles(type:Copy) {
description = 'Copy All Source Files.'
from('src/main/java') {}
from('/src/main/scala') {}
into combinedSources
includeEmptyDirs = false
}
但现在我收到了一个错误:
[ant:scalac] Compiling 18 source files to C:\usr\git_workspaces\xivr\d2\target\classes\main
[ant:scalac] Compiling 18 scala and 196 java source files to C:\usr\git_workspaces\xivr\d2\target\classes\main
[ant:scalac] C:\usr\git_workspaces\xivr\d2\target\combined-sources\com\comcast\ivr\d2\actors\AlternateAniWithAccountActor.scala:9: error: AlternateAniWithAccountActor is already defined as class AlternateAniWithAccountActor
它似乎是scalaCompile看到$ combinedSources和'src / main / scala'
答案 0 :(得分:2)
它似乎是scalaCompile看到$ combinedSources和' src / main / scala'
您的配置方式:src/main/scala
是默认设置,您添加了"$combinedSources"
。要覆盖默认值,请使用sourceSets.main.scala.srcDirs = [combinedSources]
。
在任何情况下,您都不必(并且不应该)复制来源。这是一个既不需要复制也不需要重新配置任务依赖关系的解决方案:
sourceSets.main.scala.srcDir "src/main/java"
sourceSets.main.java.srcDirs = []
现在,您的Java和Scala代码将被联合编译,并且可以任意依赖。
PS:使用"$combinedSources"
。
combinedSources
答案 1 :(得分:1)
theVersion=2.1
theSourceCompatibility=1.7
theScalaVersion=2.10.3
apply {
plugin 'scala'
plugin 'java'
plugin 'idea'
}
ext {
scalaVersion = theScalaVersion
}
sourceCompatibility = theSourceCompatibility
tasks.withType(ScalaCompile) {
scalaCompileOptions.useAnt = false
}
dependencies {
compile "org.scala-lang:scala-library:$theScalaVersion"
compile "org.scala-lang:scala-compiler:$theScalaVersion"
}
sourceSets {
main.scala.srcDirs = ["src/main/scala", "src/main/java"]
main.java.srcDirs = []
}