sbt 0.13.1 multi-project module not found when I change the sbt default scala library to scala 2.11.2

本小妞迷上赌 提交于 2020-01-03 21:01:49

问题


I use the sbt 0.13.1 create the two modules, and I create project/MyBuild.scala to compile this two modules. MyBuild.scala:

import sbt._
import Keys._
object MyBuild extends Build {
    lazy val task = project.in(file("task"))
    lazy val root = project.in(file(".")) aggregate(task) dependsOn task
}

When I change the scala library to 2.11.2 by set scalaHome. It will go to maven download the task.jar and failed, that's very strange. Is it a sbt bug?

There is the github test project address: test-sbt-0.13.1


回答1:


I'd recommend you to move the sources of root project from root to subfolder (task2), and add them as aggregated. It will remove aggregating and depending on same project:

object MyBuild extends Build {
          lazy val task = project.in(file("task"))
          lazy val task2 = project.in(file("task2")) dependsOn task
          lazy val root = project.in(file(".")) aggregate(task, task2) 
}

This works for me, but it's strange that such non-standard project structure works fine with default scalaHome. Seems to be a problem in the way in which sbt resolves such dependency as external.

P.S. This answer doesn't cover the whole story. See @jjst's answer to clarify more.




回答2:


This is because you have defined a custom scalaVersion in your main build.sbt which means it is defined for the root project only. The task project will use the default value:

jjst@ws11:test-sbt-0.13.1$ sbt                                                                                                                
[...]
> projects
[info] In file:/home/users/jjost/dev/test-sbt-0.13.1/
[info]   * root
[info]     task
> show scalaVersion
[info] task/*:scalaVersion
[info]  2.10.4
[info] root/*:scalaVersion
[info]  2.11.2-local

As a consequence, artifacts generated by the task subproject won't be available for the root project to use.

You can solve this by making sure that your projects use the same scalaVersion. The easiest way to do so while keeping the same project structure would be to share common settings like the scala version across projects like so:

object MyBuild extends Build {
    val commonSettings = Seq(
      scalaVersion := "2.11.2-local",
      scalaHome := Some(file("/usr/local/scala-2.11.2/"))
    )

    lazy val task = (project.in(file("task"))).
    settings(commonSettings: _*)

    lazy val root = (project.in(file("."))).
    settings(commonSettings: _*).
    aggregate(task).
    dependsOn(task)
}

In practice, you might want to put common settings shared between projects in a dedicated file under project/common.scala, as recommended in Effective sbt.



来源:https://stackoverflow.com/questions/25972466/sbt-0-13-1-multi-project-module-not-found-when-i-change-the-sbt-default-scala-li

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!