【问题标题】:Cannot include external classes in build files in sbt+intellij不能在 sbt+intellij 的构建文件中包含外部类
【发布时间】:2015-09-09 14:17:12
【问题描述】:

我想在我的 Build.scala 文件中使用 org.apache.commons 中的一些类,但它总是抱怨找不到我想要的类。当它们包含在源文件的顶部时,就可以了。所以同一个库,如果包含在源文件中,可以;如果包含在构建文件中,则会出错。我需要做什么才能让 include 在 Build.scala 中工作?

为了澄清,对于源使用的库,您可以添加到

libraryDependencies ++= ...

build.sbt 文件的行

但是如何处理构建文件本身所需的库呢?看来

那些添加到 libraryDependencies 中的项目 ++= ...

构建文件(.scala 之一)不能使用。

【问题讨论】:

    标签: scala sbt


    【解决方案1】:

    您正在为元构建添加库。您应该将设置放在 project/build.sbt 中,而不是 build.sbt 中。请查看sbt documentation 以获得更好的理解。

    一个简单的演示:

    $ mkdir project
    
    $ cat > project/build.sbt
    libraryDependencies += "org.json4s" %% "json4s-jackson" % "3.3.0.RC3"
    
    $ cat > build.sbt
    import org.json4s.jackson.JsonMethods
    
    {
      println(JsonMethods)
      libraryDependencies ++= Nil
    }
    
    $ sbt
    [info] Loading global plugins from /Users/thirstycrow/.sbt/0.13/plugins/project
    [info] Loading global plugins from /Users/thirstycrow/.sbt/0.13/plugins
    [info] Loading project definition from /Users/thirstycrow/tmp/project
    [info] Updating {file:/Users/thirstycrow/tmp/project/}tmp-build...
    [info] Resolving org.fusesource.jansi#jansi;1.4 ...
    [info] Done updating.
    org.json4s.jackson.JsonMethods$@7addc5e4
    [info] Set current project to tmp (in build file:/Users/thirstycrow/tmp/)
    

    【讨论】:

    • 你用过这个吗?
    • @marios 不,实际上不是。我倾向于编写插件来做一些复杂的事情。但它肯定是这样工作的。我添加了一个简单的演示。希望对您有所帮助。
    • 这真是太好了!现在我更好地理解了构建的递归性质。
    猜你喜欢
    • 2012-11-19
    • 2015-11-06
    • 2015-10-15
    • 1970-01-01
    • 2017-01-15
    • 2021-12-28
    • 2015-09-17
    • 2018-02-27
    • 1970-01-01
    相关资源
    最近更新 更多