使用SBT从另一个jar读取ManifestAttributes

时间:2019-03-08 13:36:37

标签: java scala sbt

我当前正在尝试在build.sbt中读取先前创建的jar的MANIFEST.MF,并将清单文件的属性用于packageOptions。原因是我正在使用sbt-osgi插件创建一个jar,然后想要使用sbt-assembly创建一个胖jar,但保留先前创建的MANIFEST.MF

这是我到目前为止的想法:

import java.io.InputStream
import java.nio.file.StandardCopyOption
import java.util.jar.JarFile
import java.util.zip.ZipEntry
import sbt.Package.ManifestAttributes
import scala.collection.JavaConverters._
import scala.reflect.io.Directory

lazy val readManifestFromOSGiBundle = taskKey[Seq[(String, String)]]("Reads the MANIFEST.MF of the jar built by the OSGi plugin")
readManifestFromOSGiBundle in Compile := {
  val uri = OsgiKeys.bundle.value
  val jarFile = new JarFile(uri)
  jarFile.getManifest.getMainAttributes.asScala.map(keyValue => (keyValue._1.toString, keyValue._2.toString)).toSeq
}

packageOptions := {
  val manifestAttributes = readManifestFromOSGiBundle.value: Seq[(String, String)]
  Seq(ManifestAttributes(manifestAttributes: _*))
}

清单属性可以在readManifestFromOSGiBundle中正确读取,但是很遗憾,我无法弄清楚如何在packageOptions中使用它们。

这是我遇到的错误:

[error] Reference to undefined setting: 
[error] 
[error]   gallium / readManifestFromOSGiBundle from myapplication / packageOptions (/home/fbaierl/Repos/kcc/scala/com.my.app/osgi.sbt:58)
[error]      Did you mean gallium / Compile / readManifestFromOSGiBundle ?
[error]  
[error]     at sbt.internal.util.Init.Uninitialized(Settings.scala:360)
[error]     at sbt.internal.util.Init.Uninitialized$(Settings.scala:347)
[error]     at sbt.Def$.Uninitialized(Def.scala:20)
[error]     at sbt.internal.util.Init.delegate(Settings.scala:260)
[error]     at sbt.internal.util.Init.delegate$(Settings.scala:236)
[error]     at sbt.Def$.delegate(Def.scala:20)
[error]     at sbt.internal.util.Init.compiled(Settings.scala:194)
[error]     at sbt.internal.util.Init.compiled$(Settings.scala:182)
[error]     at sbt.Def$.compiled(Def.scala:20)
[error]     at sbt.internal.util.Init.make(Settings.scala:204)
[error]     at sbt.internal.util.Init.make$(Settings.scala:199)
[error]     at sbt.Def$.make(Def.scala:20)
[error]     at sbt.internal.Load$.$anonfun$apply$5(Load.scala:266)
[error]     at sbt.internal.Load$.timed(Load.scala:1395)
[error]     at sbt.internal.Load$.apply(Load.scala:261)
[error]     at sbt.internal.Load$.defaultLoad(Load.scala:69)
[error]     at sbt.BuiltinCommands$.liftedTree1$1(Main.scala:829)
[error]     at sbt.BuiltinCommands$.doLoadProject(Main.scala:829)
[error]     at sbt.BuiltinCommands$.$anonfun$loadProjectImpl$2(Main.scala:800)
[error]     at sbt.Command$.$anonfun$applyEffect$4(Command.scala:142)
[error]     at sbt.Command$.$anonfun$applyEffect$2(Command.scala:137)
[error]     at sbt.Command$.process(Command.scala:181)
[error]     at sbt.MainLoop$.processCommand(MainLoop.scala:151)
[error]     at sbt.MainLoop$.$anonfun$next$2(MainLoop.scala:139)
[error]     at sbt.State$$anon$1.runCmd$1(State.scala:246)
[error]     at sbt.State$$anon$1.process(State.scala:250)
[error]     at sbt.MainLoop$.$anonfun$next$1(MainLoop.scala:139)
[error]     at sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:16)
[error]     at sbt.MainLoop$.next(MainLoop.scala:139)
[error]     at sbt.MainLoop$.run(MainLoop.scala:132)
[error]     at sbt.MainLoop$.$anonfun$runWithNewLog$1(MainLoop.scala:110)
[error]     at sbt.io.Using.apply(Using.scala:22)
[error]     at sbt.MainLoop$.runWithNewLog(MainLoop.scala:104)
[error]     at sbt.MainLoop$.runAndClearLast(MainLoop.scala:59)
[error]     at sbt.MainLoop$.runLoggedLoop(MainLoop.scala:44)
[error]     at sbt.MainLoop$.runLogged(MainLoop.scala:35)
[error]     at sbt.StandardMain$.runManaged(Main.scala:138)
[error]     at sbt.xMain.run(Main.scala:89)
[error]     at xsbt.boot.Launch$$anonfun$run$1.apply(Launch.scala:109)
[error]     at xsbt.boot.Launch$.withContextLoader(Launch.scala:128)
[error]     at xsbt.boot.Launch$.run(Launch.scala:109)
[error]     at xsbt.boot.Launch$$anonfun$apply$1.apply(Launch.scala:35)
[error]     at xsbt.boot.Launch$.launch(Launch.scala:117)
[error]     at xsbt.boot.Launch$.apply(Launch.scala:18)
[error]     at xsbt.boot.Boot$.runImpl(Boot.scala:56)
[error]     at xsbt.boot.Boot$.main(Boot.scala:18)
[error]     at xsbt.boot.Boot.main(Boot.scala)
[error] Reference to undefined setting: 
[error] 
[error]   gallium / readManifestFromOSGiBundle from myapplication / packageOptions (/home/fbaierl/Repos/kcc/scala/com.my.app/osgi.sbt:58)
[error]      Did you mean myapplication / Compile / readManifestFromOSGiBundle ?
[error]  
[error] Use 'last' for the full log.

如何在这里正确使用以前创建的任务readManifestFromOSGiBundle?任何帮助将不胜感激。

编辑:

当我这样定义readManifestFromOSGiBundle时:

readManifestFromOSGiBundle in Global := {...}

错误消息更改为:

error] Cyclic reference involving
[error]    ProjectRef(uri("file:/home/fbaierl/Repos/kcc/scala/"), "myapplication") / osgiBundle
[error]    ProjectRef(uri("file:/home/fbaierl/Repos/kcc/scala/"), "myapplication") / Compile / packageBin / packageOptions
[error]    ProjectRef(uri("file:/home/fbaierl/Repos/kcc/scala/"), "myapplication") / packageOptions
[error]     at sbt.internal.util.Dag$Cyclic.$colon$colon(Dag.scala:75)
[error]     at sbt.internal.util.Dag$.visit$1(Dag.scala:30)
[error]     at sbt.internal.util.Dag$.$anonfun$topologicalSort$2(Dag.scala:26)
[error]     at sbt.internal.util.Dag$.$anonfun$topologicalSort$2$adapted(Dag.scala:26)
[error]     at scala.collection.immutable.Set$Set4.foreach(Set.scala:206)
[error]     at sbt.internal.util.Dag$.visitAll$1(Dag.scala:26)
[error]     at sbt.internal.util.Dag$.visit$1(Dag.scala:30)
[error]     at sbt.internal.util.Dag$.$anonfun$topologicalSort$2(Dag.scala:26)
[error]     at sbt.internal.util.Dag$.$anonfun$topologicalSort$2$adapted(Dag.scala:26)
[error]     at scala.collection.immutable.Set$Set3.foreach(Set.scala:167)
[error]     at sbt.internal.util.Dag$.visitAll$1(Dag.scala:26)
[error]     at sbt.internal.util.Dag$.visit$1(Dag.scala:30)
[error]     at sbt.internal.util.Dag$.$anonfun$topologicalSort$2(Dag.scala:26)
[error]     at sbt.internal.util.Dag$.$anonfun$topologicalSort$2$adapted(Dag.scala:26)
[error]     at scala.collection.immutable.Set$Set2.foreach(Set.scala:130)
[error]     at sbt.internal.util.Dag$.visitAll$1(Dag.scala:26)
[error]     at sbt.internal.util.Dag$.visit$1(Dag.scala:30)
[error]     at sbt.internal.util.Dag$.$anonfun$topologicalSort$2(Dag.scala:26)
[error]     at sbt.internal.util.Dag$.$anonfun$topologicalSort$2$adapted(Dag.scala:26)
[error]     at scala.collection.immutable.Set$Set2.foreach(Set.scala:130)
[error]     at sbt.internal.util.Dag$.visitAll$1(Dag.scala:26)
[error]     at sbt.internal.util.Dag$.visit$1(Dag.scala:30)
[error]     at sbt.internal.util.Dag$.$anonfun$topologicalSort$2(Dag.scala:26)
[error]     at sbt.internal.util.Dag$.$anonfun$topologicalSort$2$adapted(Dag.scala:26)
[error]     at scala.collection.Iterator.foreach(Iterator.scala:937)
[error]     at scala.collection.Iterator.foreach$(Iterator.scala:937)
[error]     at scala.collection.AbstractIterator.foreach(Iterator.scala:1425)
[error]     at scala.collection.MapLike$DefaultValuesIterable.foreach(MapLike.scala:209)
[error]     at sbt.internal.util.Dag$.visitAll$1(Dag.scala:26)
[error]     at sbt.internal.util.Dag$.topologicalSort(Dag.scala:37)
[error]     at sbt.internal.util.Init.sort(Settings.scala:215)
[error]     at sbt.internal.util.Init.sort$(Settings.scala:214)
[error]     at sbt.Def$.sort(Def.scala:20)
[error]     at sbt.internal.util.Init.make(Settings.scala:206)
[error]     at sbt.internal.util.Init.make$(Settings.scala:199)
[error]     at sbt.Def$.make(Def.scala:20)
[error]     at sbt.internal.Load$.$anonfun$apply$5(Load.scala:266)
[error]     at sbt.internal.Load$.timed(Load.scala:1395)
[error]     at sbt.internal.Load$.apply(Load.scala:261)
[error]     at sbt.internal.Load$.defaultLoad(Load.scala:69)
[error]     at sbt.BuiltinCommands$.liftedTree1$1(Main.scala:829)
[error]     at sbt.BuiltinCommands$.doLoadProject(Main.scala:829)
[error]     at sbt.BuiltinCommands$.$anonfun$loadProjectImpl$2(Main.scala:800)
[error]     at sbt.Command$.$anonfun$applyEffect$4(Command.scala:142)
[error]     at sbt.Command$.$anonfun$applyEffect$2(Command.scala:137)
[error]     at sbt.Command$.process(Command.scala:181)
[error]     at sbt.MainLoop$.processCommand(MainLoop.scala:151)
[error]     at sbt.MainLoop$.$anonfun$next$2(MainLoop.scala:139)
[error]     at sbt.State$$anon$1.runCmd$1(State.scala:246)
[error]     at sbt.State$$anon$1.process(State.scala:250)
[error]     at sbt.MainLoop$.$anonfun$next$1(MainLoop.scala:139)
[error]     at sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:16)
[error]     at sbt.MainLoop$.next(MainLoop.scala:139)
[error]     at sbt.MainLoop$.run(MainLoop.scala:132)
[error]     at sbt.MainLoop$.$anonfun$runWithNewLog$1(MainLoop.scala:110)
[error]     at sbt.io.Using.apply(Using.scala:22)
[error]     at sbt.MainLoop$.runWithNewLog(MainLoop.scala:104)
[error]     at sbt.MainLoop$.runAndClearLast(MainLoop.scala:59)
[error]     at sbt.MainLoop$.runLoggedLoop(MainLoop.scala:44)
[error]     at sbt.MainLoop$.runLogged(MainLoop.scala:35)
[error]     at sbt.StandardMain$.runManaged(Main.scala:138)
[error]     at sbt.xMain.run(Main.scala:89)
[error]     at xsbt.boot.Launch$$anonfun$run$1.apply(Launch.scala:109)
[error]     at xsbt.boot.Launch$.withContextLoader(Launch.scala:128)
[error]     at xsbt.boot.Launch$.run(Launch.scala:109)
[error]     at xsbt.boot.Launch$$anonfun$apply$1.apply(Launch.scala:35)
[error]     at xsbt.boot.Launch$.launch(Launch.scala:117)
[error]     at xsbt.boot.Launch$.apply(Launch.scala:18)
[error]     at xsbt.boot.Boot$.runImpl(Boot.scala:56)
[error]     at xsbt.boot.Boot$.main(Boot.scala:18)
[error]     at xsbt.boot.Boot.main(Boot.scala)
[error] Cyclic reference involving
[error]    ProjectRef(uri("file:/home/fbaierl/Repos/kcc/scala/"), "myapplication") / osgiBundle
[error]    ProjectRef(uri("file:/home/fbaierl/Repos/kcc/scala/"), "myapplication") / Compile / packageBin / packageOptions
[error]    ProjectRef(uri("file:/home/fbaierl/Repos/kcc/scala/"), "myapplication") / packageOptions

1 个答案:

答案 0 :(得分:0)

循环依赖性是由于在OsgiKeys.bundle.value内部调用readManifestFromOSGiBundle引起的。我用artifactPath.in(packageBin).in(Compile).value替换了这一行,现在可以了。