0

这在 Buildr 中似乎是一项简单的任务,所以我一定遗漏了一些明显的东西,因为我无法让它工作。假设我有一个包含两个文件的目录,如下所示:

test/lib/src/main/scala/MyLib.scala
test/app/src/main/scala/MyApp.scala

MyLib.scala是:

class MyLib {
  def hello() { println("Hello!") }
}

并且MyApp.scala是:

object MyApp extends App {
  val ml = new MyLib
  ml.hello()
}

构建这些scalac很简单:

$ cd test
$ scalac lib/src/main/scala/MyLib.scala -d target/main/classes
$ scalac app/src/main/scala/MyApp.scala -cp target/main/classes -d target/main/classes
$ cd target/main/classes/
$ scala MyApp
Hello!

然而,我天真地试图把它变成一个Buildfile(在test文件夹中):

require 'buildr/scala'

lib_layout = Layout.new
lib_layout[:source, :main, :scala] = 'lib/src/main/scala'
app_layout = Layout.new
app_layout[:source, :main, :scala] = 'app/src/main/scala'

define 'mylib', :layout => lib_layout do
end

define 'myapp', :layout => app_layout do
  compile.with project('mylib')
end

失败:

(in /test, development)
Building mylib
Compiling myapp into /test/target/main/classes
/test/app/src/main/scala/MyApp.scala:2: error: not found: type MyLib
  val ml = new MyLib
               ^
one error found
Buildr aborted!
RuntimeError : Failed to compile, see errors above

如果我运行buildr --trace,很明显scalac失败的原因是因为类路径不包含target/main/classes.

我该如何做到这一点?我知道将这两个项目分开可能看起来有些做作,但我有一些更复杂的想法,这个例子将问题归结为它的基本组成部分。

4

2 回答 2

1

The idiomatic way to describe your project with Buildr would be to use sub-projects,

Note: buildfile below goes into the test/ directory.

require 'buildr/scala'

define "my-project" do
  define "lib" do
  end

  define "app" do
    compile.with project("lib").compile.target
  end
end

The two sub-projects lib and app are automatically mapped to the lib/ and app/ sub-directories and Buildr will automatically look for sources under src/main/scala for each.

于 2012-09-24T16:55:32.640 回答
0

Buildr common convention for project layout looks for compiled classes in target/classes and target/test/classes. I see your build is placing classes into target/main/classes:

So you'll want to change your scala params to the expected location, or change the layout to include:

lib_layout[:target, :main, :scala] = 'target/main/classes'

If you also need to change the layout for test classes (I'm guessing you don't), use:

lib_layout[:target, :test, :scala] = ...
于 2012-09-22T17:24:27.290 回答