如何在多项目构建中使用 sbt 插件作为依赖项? [英] How can I use an sbt plugin as a dependency in a multi-project build?

查看:39
本文介绍了如何在多项目构建中使用 sbt 插件作为依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<小时>

我的测试插件

build.sbt

lazy val commonSettings = Seq(组织:=com.example",名称:=我的测试插件",版本:=0.1.0-快照",斯卡拉版本:=2.10.5")//root 的内容在这里基本上不重要lazy val root = (project in file(".")).settings(commonSettings: _*)懒惰的 val 插件 =(文件中的项目(插件")).settings(commonSettings: _*).设置(sbtPlugin := 真)

my-test-plugin/plugin/src/main/scala/PluginTest.scala

包 com.example.test//我想从另一个插件访问的示例代码对象插件测试{def foo: Unit = println("test")}

<小时>

我的子插件

build.sbt

lazy val commonSettings = Seq(组织:=com.sample",名称:=我的子插件",版本:=0.1.0-快照",斯卡拉版本:=2.10.5")lazy val root = (project in file(".")).settings(commonSettings: _*)懒惰的 val 插件 =(文件中的项目(插件")).settings(commonSettings: _*).设置(sbtPlugin := 真,libraryDependencies += Defaults.sbtPluginExtra("com.example" % "my-test-plugin" % "0.1.0-SNAPSHOT", "0.13", "2.10")).dependsOn(root)

my-sub-plugin/plugin/src/main/scala/SubPluginTest.scala

包 com.sample.test对象子插件测试{def bar = com.example.test.PluginTest.foo}

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

[error]/home/mike/code/sbt-tests/my-sub-plugin/plugin/src/main/scala/SubPluginTest.scala:4: 对象示例不是包 com 的成员[错误] def bar = com.example.test.PluginTest.foo[错误]^

当我 publish-localplugin/publish-local 两个项目(相反,只编译第二个)时,工件正确解析,但 SubPlugintest.scala 无法编译并出现上述错误,就好像依赖项不存在一样.但是,如果我删除 root 项目并将插件文件放在 root 中(没有惰性 vals 或任何东西,只是一个平面 build.sbt 结构),它就可以工作.>

我在这里遗漏了什么?

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

依赖 jars 出现在本地并正确解析:

$ jar tf ~/.ivy2/local/com.example/my-test-plugin/scala_2.10/sbt_0.13/0.1.0-SNAPSHOT/jars/my-test-plugin.jar元信息/清单.MFcom/com/例子/com/示例/测试/com/example/test/PluginTest$.classcom/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元信息/清单.MFcom/com/例子/com/示例/测试/com/example/test/DummyCode.classcom/example/test/DummyCode$.class

解决方案

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

我的测试插件

<代码>>;显示根/名称 ;显示插件/名称[信息] 我的测试插件[信息] 我的测试插件

我的子插件

<代码>>;显示根/名称 ;显示插件/名称[信息] 我的子插件[信息] 我的子插件

<小时>

如您所见,在 my-test-plugin 中发布是有效的:

<代码>>;root/publishLocal ;plugin/publishLocal[信息] 写/Users/dnw/Desktop/sbt-tests/my-test-plugin/target/scala-2.10/my-test-plugin_2.10-0.1.0-SNAPSHOT.pom[信息] :: 交付 :: com.example#my-test-plugin_2.10;0.1.0-SNAPSHOT :: 0.1.0-SNAPSHOT :: 集成 :: 2015 年 4 月 11 日星期六 09:16:15 BST[信息] 将 ivy 文件传送到/Users/dnw/Desktop/sbt-tests/my-test-plugin/target/scala-2.10/ivy-0.1.0-SNAPSHOT.xml[信息] 将 my-test-plugin_2.10 发布到/Users/dnw/.ivy2/local/com.example/my-test-plugin_2.10/0.1.0-SNAPSHOT/poms/my-test-plugin_2.10.绒球[信息] 将 my-test-plugin_2.10 发布到/Users/dnw/.ivy2/local/com.example/my-test-plugin_2.10/0.1.0-SNAPSHOT/jars/my-test-plugin_2.10.罐[信息] 将 my-test-plugin_2.10 发布到/Users/dnw/.ivy2/local/com.example/my-test-plugin_2.10/0.1.0-SNAPSHOT/srcs/my-test-plugin_2.10-源文件[信息] 将 my-test-plugin_2.10 发布到/Users/dnw/.ivy2/local/com.example/my-test-plugin_2.10/0.1.0-SNAPSHOT/docs/my-test-plugin_2.10-文档文件[信息] 将 ivy 发布到/Users/dnw/.ivy2/local/com.example/my-test-plugin_2.10/0.1.0-SNAPSHOT/ivys/ivy.xml[成功] 总时间:0 秒,完成 11-Apr-2015 09:16:15[信息] 写/Users/dnw/Desktop/sbt-tests/my-test-plugin/plugin/target/scala-2.10/sbt-0.13/my-test-plugin-0.1.0-SNAPSHOT.pom[信息] :: 交付 :: com.example#my-test-plugin;0.1.0-SNAPSHOT :: 0.1.0-SNAPSHOT :: 集成 :: Sat Apr 11 09:16:15 BST 2015[信息] 将 ivy 文件传送到/Users/dnw/Desktop/sbt-tests/my-test-plugin/plugin/target/scala-2.10/sbt-0.13/ivy-0.1.0-SNAPSHOT.xml[信息] 将 my-test-plugin 发布到/Users/dnw/.ivy2/local/com.example/my-test-plugin/scala_2.10/sbt_0.13/0.1.0-SNAPSHOT/poms/my-test-插件.pom[信息] 将 my-test-plugin 发布到/Users/dnw/.ivy2/local/com.example/my-test-plugin/scala_2.10/sbt_0.13/0.1.0-SNAPSHOT/jars/my-test-插件文件[信息] 将 my-test-plugin 发布到/Users/dnw/.ivy2/local/com.example/my-test-plugin/scala_2.10/sbt_0.13/0.1.0-SNAPSHOT/srcs/my-test-插件sources.jar[信息] 将 my-test-plugin 发布到/Users/dnw/.ivy2/local/com.example/my-test-plugin/scala_2.10/sbt_0.13/0.1.0-SNAPSHOT/docs/my-test-插件-javadoc.jar[信息] 发布常春藤到/Users/dnw/.ivy2/local/com.example/my-test-plugin/scala_2.10/sbt_0.13/0.1.0-SNAPSHOT/ivys/ivy.xml[成功] 总时间:0 秒,完成 11-Apr-2015 09:16:15

但请注意发布路径:

com.example/my-test-plugin_2.10/0.1.0-SNAPSHOT/jars/my-test-plugin_2.10.jarcom.example/my-test-plugin/scala_2.10/sbt_0.13/0.1.0-SNAPSHOT/jars/my-test-plugin.jar

  1. 为 Scala 2.10 交叉构建的名为 my-test-plugin 的非 sbt 插件.
  2. 一个名为 my-test-plugin 的 sbt 插件,为 Scala 2.10 和 sbt 0.13 交叉构建.

这在尝试解析 my-sub-plugin 中的 my-test-plugin 时会产生影响:

[error] 模块在 {file:/Users/dnw/Desktop/sbt-tests/my-sub-plugin/}root 中解决了冲突的跨版本后缀:[错误] com.example:my-test-plugin <none>, _2.10java.lang.RuntimeException:冲突的跨版本后缀:com.example:my-test-plugin在 scala.sys.package$.error(package.scala:27​​)在 sbt.ConflictWarning$.processCrossVersioned(ConflictWarning.scala:46)在 sbt.ConflictWarning$.apply(ConflictWarning.scala:32)

尝试为每个模块指定不同的名称,它应该可以工作.

I have two sbt plugin projects that both use multi-project builds. I would like to use one of the plugins as a dependency for the other. I was able to get this working with single project builds, but once I move to the multi-project build, I can't seem to get the dependencies to link up correctly.


my-test-plugin

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
    )

my-test-plugin/plugin/src/main/scala/PluginTest.scala

package com.example.test

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


my-sub-plugin

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)

my-sub-plugin/plugin/src/main/scala/SubPluginTest.scala

package com.sample.test

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

But the last file doesn't compile:

[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]                   ^

When I publish-local and plugin/publish-local both projects (rather, just compile the second), the artifacts resolve correctly, but SubPlugintest.scala fails to compile with the above error, as if the dependency isn't there. However, if I remove the root projects and put the plugin files in root (without lazy vals or anything, just a flat build.sbt structure), it works.

What am I missing here?

I don't think it's relevant, but I tried 0.13.5 and 0.13.8. I've also fruitlessly tried adding the dependency without sbtPluginExtra, and putting them in plugins.sbt as well (I didn't think that would work, but hey).

Edit:

The dependency jars appear exist locally and resolve correctly:

$ 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

解决方案

You're not setting a distinct name for each module:

my-test-plugin

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

my-sub-plugin

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


As you can see, publishing in my-test-plugin works:

> ;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

But note the publish paths:

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. A non-sbt-plugin called my-test-plugin cross-built for Scala 2.10.
  2. An sbt-plugin called my-test-plugin, cross-built for Scala 2.10 and sbt 0.13.

This has an impact when trying to resolve my-test-plugin in 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)

Try and specify different names for each module and it should work.

这篇关于如何在多项目构建中使用 sbt 插件作为依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆