在 SBT 中的多项目构建下使用包不为根项目生成工件 [英] Producing no artifact for root project with package under multi-project build in SBT

查看:10
本文介绍了在 SBT 中的多项目构建下使用包不为根项目生成工件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SBT 中有一个多项目构建配置,它由两个不同的模块组成, 彼此不依赖.它们只是(恰好)属于同一个产品.

I have a multi-project build configuration in SBT that consists of two distinct modules that do not depend on each other. They just (happen to) belong to the same product.

项目布局如下:

myLib
  + build.sbt
  + myProject_1
  |    + build.sbt
  |    + src
  |        + ...
  + myProject_2
  |    + build.sbt
  |    + src
  |        + ...
  + project
       + Build.scala

project/Build.scala 包含常用设置,如下所示:

project/Build.scala contains common settings and looks like this:

import sbt._
import Keys._

object ApplicationBuild extends Build {

  val appVersion = "1.0-SNAPSHOT"

  val defaultScalacOptions = Seq(
    "-unchecked", "-deprecation", "-feature", "-language:reflectiveCalls",
    "-language:implicitConversions", "-language:postfixOps",
    "-language:dynamics", "-language:higherKinds", "-language:existentials",
    "-language:experimental.macros", "-Xmax-classfile-name", "140")

  val defaultResolvers = Seq(
    "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
  )

  val defaultLibraryDependencies = Seq(
    "org.specs2" %% "specs2" % "1.14" % "test",
    "org.slf4j" % "slf4j-nop" % "1.7.5" % "test"
  )

  val defaultSettings = Defaults.defaultSettings ++ Seq(
    scalacOptions ++= defaultScalacOptions,
    resolvers ++= defaultResolvers,
    libraryDependencies ++= defaultLibraryDependencies
  )
}

只需要将根构建文件 build.sbt 放在一起[我也尝试将其删除.. 但随后子项目不再编译]:

The root build file build.sbt is just needed to put all together [I also tried to remove it.. but then the sub-projects don't get compiled anymore]:

lazy val myProject_1 = project.in(file("myProject_1"))

lazy val myProject_2 = project.in(file("myProject_2"))

最后是 myProject_1/build.sbt [我刚刚省略了 myProject_2/build.sbt,因为它非常相似并且没有为主题]:

And finally here is myProject_1/build.sbt [I have just omitted myProject_2/build.sbt because it is very similar and does not provide any added value for the topic]:

name := "myProject_1"

version := ApplicationBuild.appVersion

ApplicationBuild.defaultSettings

libraryDependencies ++= Seq(
  "commons-codec" % "commons-codec" % "1.8"
)

项目编译成功...但是当我发出命令sbt package时,在根目标目录中生成了一个空jar:

The project compiles successfully... but when I issue the command sbt package, then an empty jar is generated in the root target directory:

j3d@gonzo:~/myLib/$ ll target/scala-2.10
drwxrwxr-x 2 j3d j3d 4096 Dez 23 17:13 ./
drwxrwxr-x 5 j3d j3d 4096 Dez 23 17:13 ../
-rw-rw-r-- 1 j3d j3d  273 Dez 23 17:13 brix_2.10-0.1-SNAPSHOT.jar

我错过了什么吗?如何防止 SBT 生成这个空无用的 jar?

Am I missing something? How can I prevent SBT from generating this empty and useless jar?

推荐答案

我建议 一种解决方法,在 build.sbt:

Keys.`package` := {
    (Keys.`package` in (a, Compile)).value
    (Keys.`package` in (b, Compile)).value
}

其中 ab 是(子)模块.

where a and b are (sub)modules.

lazy val a = project

lazy val b = project

由于 package 是 Scala 中的关键字,因此需要引号才能解析.

Since package is a keyword in Scala it needs quotes to be resolvable.

它还需要完全限定,因为 packageKeys._ 导入,而 sbt._ 导入默认在.sbt 构建文件.

It also needs to be fully-qualified since package is imported by Keys._ and sbt._ imports that are by default in .sbt build files.

/Users/jacek/sandbox/so/multi-packageBin/build.sbt:5: error: reference to package is ambiguous;
it is imported twice in the same scope by
import Keys._
and import sbt._
`package` := {
^
[error] Type error in expression

还请注意,我使用的是 SBT 0.13.2-SNAPSHOT(由源代码构建),因此请谨慎使用(但我怀疑它会在任何版本的 SBT 0.13+).

Please also note that I'm using SBT 0.13.2-SNAPSHOT (built from the sources) so use with caution (yet I doubt it will make a difference in any version of SBT 0.13+).

[multi-packagebin]> */*:sbtVersion
[info] 0.13.2-SNAPSHOT
[multi-packagebin]> projects
[info] In file:/Users/jacek/sandbox/so/multi-packageBin/
[info]     a
[info]     b
[info]   * multi-packagebin
[multi-packagebin]> package
[info] Updating {file:/Users/jacek/sandbox/so/multi-packageBin/}a...
[info] Updating {file:/Users/jacek/sandbox/so/multi-packageBin/}b...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Resolving org.scala-lang#scala-reflect;2.10.3 ...
[info] Packaging /Users/jacek/sandbox/so/multi-packageBin/b/target/scala-2.10/b_2.10-0.1-SNAPSHOT.jar ...
[info] Done packaging.
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Packaging /Users/jacek/sandbox/so/multi-packageBin/a/target/scala-2.10/a_2.10-0.1-SNAPSHOT.jar ...
[info] Done packaging.
[success] Total time: 1 s, completed Feb 23, 2014 10:12:41 AM

这篇关于在 SBT 中的多项目构建下使用包不为根项目生成工件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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