繁体   English   中英

何时执行sbt-assembly?

[英]When sbt-assembly executes?

堆栈溢出:

使用sbt-assembly(sbt插件)和sbt-multiproject,我想生成命令行参数中指定的子项目的胖子。

该命令可能如下所示:

sbt "assemblyWith hello world"

我定义了四个子项目,分别命名为hello,world,Alien和root。 我希望该命令生成hello-assembly.jar和world-assembly.jar。 但是Alien-assembly.jar也是如此。

组装过程何时解雇?

这是产生问题的示例build.sbt。 (我希望看到日志

'managedProject: ProjectRef(module/hello,hello)'

之前

'Packaging /path/to/dir/module/world/target/scala-2.11/world-assembly-0.1-SNAPSHOT.jar ...'

[build.sbt]

import sbt._
import Keys._
import complete.DefaultParsers.spaceDelimited

name := "sbt-query"

scalaVersion in ThisBuild := "2.11.7"

scalacOptions in ThisBuild ++= Seq("-unchecked", "-deprecation", "-feature")

test in assembly in ThisBuild := {}

lazy val hello = (project in file("./module/hello"))

lazy val world = (project in file("./module/world"))

lazy val alien = (project in file("./module/alien"))

lazy val root = (project in file(".")).aggregate(hello, world, alien)

// sbt "assemblyWith hello world"
// aims to produce hello-assembly.jar and world-assembly.jar (not alien-assembly.jar)

lazy val assemblyWith = inputKey[Unit]("assembly sub-projects specified in args")

assemblyWith := {
    val args = spaceDelimited("<arg>").parsed
    val stateee = state.value
    val log = stateee.globalLogging.full
    val extractedRoot = sbt.Project.extract(stateee)
    val destDirectory = (crossTarget in extractedRoot.currentRef get extractedRoot.structure.data).get
    args.collect {
        case projectName if (file("module") / projectName).exists =>
            ProjectRef(file("module") / projectName, projectName)
    }.map { proj =>
        log.info(s"managedProject: $proj")
        // improve: https://github.com/sbt/sbt/issues/1095
        // -> outside the task's definition (i.e. as a top level statement in build.sbt), then it works.
        val assemblyJarFile = proj.project match {
            case "hello" => assembly.all(ScopeFilter(inProjects(hello))).value.head
            case "world" => assembly.all(ScopeFilter(inProjects(world))).value.head
            case "alien" => assembly.all(ScopeFilter(inProjects(alien))).value.head
        }
        log.info(s"out: ${assemblyJarFile.getCanonicalPath}")
        // IO.copyFile(assemblyJarFile, destDirectory)
    }
}

[项目/ build.properties]

sbt.version=0.13.9

[项目/ plugins.sbt]

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.13.0")

[目录树]

├─alien
│  └─src
│      └─main
│          └─scala
│              └─com.example.alien -> SayAlien.scala
├─hello
│  └─src
│      └─main
│          └─scala
│              └─com.example.hello -> SayHello.scala
└─world
   └─src
       └─main
           └─scala
               └─com.example.world -> SayWorld.scala

[版本]

java version "1.8.0_51"

[日志]

[info] Including from cache: scala-library-2.11.7.jar
[info] Including from cache: scala-library-2.11.7.jar
[info] Including from cache: scala-library-2.11.7.jar
[info] Checking every *.class/*.jar file's SHA-1.
[info] Merging files...
[warn] Merging 'META-INF\MANIFEST.MF' with strategy 'discard'
[warn] Strategy 'discard' was applied to a file
[info] Checking every *.class/*.jar file's SHA-1.
[info] Merging files...
[warn] Merging 'META-INF\MANIFEST.MF' with strategy 'discard'
[warn] Strategy 'discard' was applied to a file
[info] Checking every *.class/*.jar file's SHA-1.
[info] Merging files...
[warn] Merging 'META-INF\MANIFEST.MF' with strategy 'discard'
[warn] Strategy 'discard' was applied to a file
[info] Packaging ...../world-assembly-0.1-SNAPSHOT.jar ...
[info] Packaging ...../alien-assembly-0.1-SNAPSHOT.jar ...
[info] Packaging ...../hello-assembly-0.1-SNAPSHOT.jar ...
[info] Done packaging.
[info] Done packaging.
[info] Done packaging.
[info] managedProject: ProjectRef(module/hello,hello)
[info] out: ...../hello-assembly-0.1-SNAPSHOT.jar
[info] managedProject: ProjectRef(module/world,world)
[info] out: ...../world-assembly-0.1-SNAPSHOT.jar

代码没有错。 构建任务很清楚,您应该将要构建的模块的名称传递给assemblyWith任务。 我刚刚在本地构建并复制了您的代码。 您可以在github上找到它。

我与sbt> assemblyWith hello alien world跑了sbt> assemblyWith hello alien world ,看到没有问题,并且生成了所有三个项目。

这是输出:

> assemblyWith hello alien world
[info] Including from cache: scala-library-2.11.7.jar
[info] Including from cache: scala-library-2.11.7.jar
[info] Including from cache: scala-library-2.11.7.jar
[info] Checking every *.class/*.jar file's SHA-1.
[info] Merging files...
[warn] Merging 'META-INF/MANIFEST.MF' with strategy 'discard'
[warn] Strategy 'discard' was applied to a file
[info] Checking every *.class/*.jar file's SHA-1.
[info] Merging files...
[warn] Merging 'META-INF/MANIFEST.MF' with strategy 'discard'
[warn] Strategy 'discard' was applied to a file
[info] Checking every *.class/*.jar file's SHA-1.
[info] Merging files...
[warn] Merging 'META-INF/MANIFEST.MF' with strategy 'discard'
[warn] Strategy 'discard' was applied to a file
[info] Assembly up to date: /code/module/hello/target/scala-2.11/hello-assembly-0.1-SNAPSHOT.jar
[info] Assembly up to date: /code/module/world/target/scala-2.11/world-assembly-0.1-SNAPSHOT.jar
[info] Assembly up to date: /code/module/alien/target/scala-2.11/alien-assembly-0.1-SNAPSHOT.jar
[info] managedProject: ProjectRef(module/hello,hello)
[info] out: /code/module/hello/target/scala-2.11/hello-assembly-0.1-SNAPSHOT.jar
[info] managedProject: ProjectRef(module/alien,alien)
[info] out: /code/module/alien/target/scala-2.11/alien-assembly-0.1-SNAPSHOT.jar
[info] managedProject: ProjectRef(module/world,world)
[info] out: /code/module/world/target/scala-2.11/world-assembly-0.1-SNAPSHOT.jar
[success] Total time: 4 s, completed Oct 9, 2015 10:05:46 AM

我不确定您的问题是什么,如果您可以改写更清楚的问题,这将有所帮助。

组装过程何时解雇?

当需要它产生的值时,将触发组装过程,即:

    val assemblyJarFile = proj.project match {
        case "hello" => assembly.all(ScopeFilter(inProjects(hello))).value.head

会告诉sbt,这部分任务取决于hello项目值的组合。 这将导致组装过程被解雇。 SBT任务实质上是通过指定它们依赖于另一个任务来工作的,这导致该任务在需要其值时被调用。 至少这就是我如何处理sbt任务的方法,这很像make。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM