如何在多项目构建中使用sbt插件作为依赖项?

时间:2015-04-07 02:12:29

标签: scala sbt

我有两个使用多项目构建的sbt插件项目。我想使用其中一个插件作为另一个的依赖。我能够使用单个项目构建来实现这一点,但是一旦我转向多项目构建,我似乎无法正确地链接到依赖项。


我的 - 测试 - 插件

build.sbt

lazy val commonSettings = Seq(
    organization := "com.example",
    name := "my-test-plugin",
    version := "0.1.0-SNAPSHOT",
    scalaVersion := "2.10.5"
)

// The contents of root are largely unimportant here
lazy val root = (project in file(".")).settings(commonSettings: _*)

lazy val plugin = (project in file("plugin"))
    .settings(commonSettings: _*)
    .settings(
        sbtPlugin := true
    )

我的 - 测试 - 插件/插件/ SRC /主/阶/ PluginTest.scala

package com.example.test

// Sample code I would like to access from another plugin
object PluginTest {
    def foo: Unit = println("test")
}

我的子插件

build.sbt

lazy val commonSettings = Seq(
    organization := "com.sample",
    name := "my-sub-plugin",
    version := "0.1.0-SNAPSHOT",
    scalaVersion := "2.10.5"
)

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

lazy val plugin = (project in file("plugin"))
    .settings(commonSettings: _*)
    .settings(
        sbtPlugin := true,
        libraryDependencies += Defaults.sbtPluginExtra("com.example" % "my-test-plugin" % "0.1.0-SNAPSHOT", "0.13", "2.10")
    ).dependsOn(root)

我的子插件/插件/ SRC /主/阶/ SubPluginTest.scala

package com.sample.test

object SubPluginTest {
    def bar = com.example.test.PluginTest.foo
}

但是最后一个文件没有编译:

[error] /home/mike/code/sbt-tests/my-sub-plugin/plugin/src/main/scala/SubPluginTest.scala:4: object example is not a member of package com
[error]     def bar = com.example.test.PluginTest.foo
[error]                   ^

当我publish-localplugin/publish-local两个项目(而不是仅编译第二个)时,工件会正确解析,但SubPlugintest.scala无法使用上述错误进行编译,就好像依赖项一样不在那里。但是,如果我删除root项目并将插件文件放在root中(没有lazy vals或任何东西,只是一个平坦的build.sbt结构),它就可以工作。

我在这里缺少什么?

我认为它并不相关,但我尝试了0.13.5和0.13.8。我也毫无结果地尝试在没有sbtPluginExtra的情况下添加依赖项,并将它们放在plugins.sbt中(我没想到会有效,但是嘿)。

编辑:

依赖关系jar出现在本地并正确解析:

$ jar tf ~/.ivy2/local/com.example/my-test-plugin/scala_2.10/sbt_0.13/0.1.0-SNAPSHOT/jars/my-test-plugin.jar
META-INF/MANIFEST.MF
com/
com/example/
com/example/test/
com/example/test/PluginTest$.class
com/example/test/PluginTest.class

$ jar tf ~/.ivy2/local/com.example/my-test-plugin_2.10/0.1.0-SNAPSHOT/jars/my-test-plugin_2.10.jar
META-INF/MANIFEST.MF
com/
com/example/
com/example/test/
com/example/test/DummyCode.class
com/example/test/DummyCode$.class

1 个答案:

答案 0 :(得分:2)

您没有为每个模块设置不同的name

我的 - 测试 - 插件

> ;show root/name ;show plugin/name
[info] my-test-plugin
[info] my-test-plugin

我的子插件

> ;show root/name ;show plugin/name
[info] my-sub-plugin
[info] my-sub-plugin

如您所见,在my-test-plugin发布工作:

> ;root/publishLocal ;plugin/publishLocal
[info] Wrote /Users/dnw/Desktop/sbt-tests/my-test-plugin/target/scala-2.10/my-test-plugin_2.10-0.1.0-SNAPSHOT.pom
[info] :: delivering :: com.example#my-test-plugin_2.10;0.1.0-SNAPSHOT :: 0.1.0-SNAPSHOT :: integration :: Sat Apr 11 09:16:15 BST 2015
[info]  delivering ivy file to /Users/dnw/Desktop/sbt-tests/my-test-plugin/target/scala-2.10/ivy-0.1.0-SNAPSHOT.xml
[info]  published my-test-plugin_2.10 to /Users/dnw/.ivy2/local/com.example/my-test-plugin_2.10/0.1.0-SNAPSHOT/poms/my-test-plugin_2.10.pom
[info]  published my-test-plugin_2.10 to /Users/dnw/.ivy2/local/com.example/my-test-plugin_2.10/0.1.0-SNAPSHOT/jars/my-test-plugin_2.10.jar
[info]  published my-test-plugin_2.10 to /Users/dnw/.ivy2/local/com.example/my-test-plugin_2.10/0.1.0-SNAPSHOT/srcs/my-test-plugin_2.10-sources.jar
[info]  published my-test-plugin_2.10 to /Users/dnw/.ivy2/local/com.example/my-test-plugin_2.10/0.1.0-SNAPSHOT/docs/my-test-plugin_2.10-javadoc.jar
[info]  published ivy to /Users/dnw/.ivy2/local/com.example/my-test-plugin_2.10/0.1.0-SNAPSHOT/ivys/ivy.xml
[success] Total time: 0 s, completed 11-Apr-2015 09:16:15
[info] Wrote /Users/dnw/Desktop/sbt-tests/my-test-plugin/plugin/target/scala-2.10/sbt-0.13/my-test-plugin-0.1.0-SNAPSHOT.pom
[info] :: delivering :: com.example#my-test-plugin;0.1.0-SNAPSHOT :: 0.1.0-SNAPSHOT :: integration :: Sat Apr 11 09:16:15 BST 2015
[info]  delivering ivy file to /Users/dnw/Desktop/sbt-tests/my-test-plugin/plugin/target/scala-2.10/sbt-0.13/ivy-0.1.0-SNAPSHOT.xml
[info]  published my-test-plugin to /Users/dnw/.ivy2/local/com.example/my-test-plugin/scala_2.10/sbt_0.13/0.1.0-SNAPSHOT/poms/my-test-plugin.pom
[info]  published my-test-plugin to /Users/dnw/.ivy2/local/com.example/my-test-plugin/scala_2.10/sbt_0.13/0.1.0-SNAPSHOT/jars/my-test-plugin.jar
[info]  published my-test-plugin to /Users/dnw/.ivy2/local/com.example/my-test-plugin/scala_2.10/sbt_0.13/0.1.0-SNAPSHOT/srcs/my-test-plugin-sources.jar
[info]  published my-test-plugin to /Users/dnw/.ivy2/local/com.example/my-test-plugin/scala_2.10/sbt_0.13/0.1.0-SNAPSHOT/docs/my-test-plugin-javadoc.jar
[info]  published ivy to /Users/dnw/.ivy2/local/com.example/my-test-plugin/scala_2.10/sbt_0.13/0.1.0-SNAPSHOT/ivys/ivy.xml
[success] Total time: 0 s, completed 11-Apr-2015 09:16:15

但请注意发布路径:

com.example/my-test-plugin_2.10/0.1.0-SNAPSHOT/jars/my-test-plugin_2.10.jar
com.example/my-test-plugin/scala_2.10/sbt_0.13/0.1.0-SNAPSHOT/jars/my-test-plugin.jar
  1. 为Scala my-test-plugin交叉构建的名为2.10的非sbt插件。
  2. 一个名为my-test-plugin的sbt-plugin,为Scala 2.10和sbt 0.13交叉构建。
  3. 尝试解决my-test-plugin中的my-sub-plugin时会产生影响:

    [error] Modules were resolved with conflicting cross-version suffixes in {file:/Users/dnw/Desktop/sbt-tests/my-sub-plugin/}root:
    [error]    com.example:my-test-plugin <none>, _2.10
    java.lang.RuntimeException: Conflicting cross-version suffixes in: com.example:my-test-plugin
        at scala.sys.package$.error(package.scala:27)
        at sbt.ConflictWarning$.processCrossVersioned(ConflictWarning.scala:46)
        at sbt.ConflictWarning$.apply(ConflictWarning.scala:32)
    

    尝试为每个模块指定不同的名称,它应该有效。