【问题标题】:sbt assembly works, but still "no main manifest attribute"sbt 程序集有效,但仍然“没有主要清单属性”
【发布时间】:2018-04-01 08:36:36
【问题描述】:

我正在使用 sbt 1.0.2。我的 build.sbt 是

name := "myprogram"

version := "0.1"

scalaVersion := "2.12.3"

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test"

// https://mvnrepository.com/artifact/net.sf.py4j/py4j
libraryDependencies += "net.sf.py4j" % "py4j" % "0.10.6"

scalacOptions ++= Seq(
  "-unchecked",
  "-deprecation",
  "-feature",
  "-encoding", "UTF-8",
  "-Xfuture",
  "-Xlint:unsound-match",      // Pattern match may not be typesafe.
  "-Yno-adapted-args",         // Do not adapt an argument list (either by inserting () or creating a tuple) to match the receiver.
  "-Ypartial-unification",     // Enable partial unification in type constructor inference
  "-Ywarn-dead-code",          // Warn when dead code is identified.
  "-Ywarn-inaccessible",       // Warn about inaccessible types in method signatures.
  "-Ywarn-infer-any",          // Warn when a type argument is inferred to be `Any`.
  "-Ywarn-nullary-override",   // Warn when non-nullary `def f()' overrides nullary `def f'.
  "-Ywarn-nullary-unit",       // Warn when nullary methods return Unit.
  "-Ywarn-unused",             // Warn if a method or value is unused.
  "-Ywarn-unused:implicits",   // Warn if an implicit parameter is unused.
  "-Ywarn-unused:imports",     // Warn if an import selector is not referenced.
  "-Ywarn-unused:locals",      // Warn if a local definition is unused.
  "-Ywarn-unused:params",      // Warn if a value parameter is unused.
  "-Ywarn-unused:patvars",     // Warn if a variable bound in a pattern is unused.
  "-Ywarn-unused:privates",    // Warn if a private member is unused.
  "-Ywarn-value-discard"       // Warn when non-Unit expression results are unused.
)

javacOptions ++= Seq("-source", "1.8", "-target", "1.8")

test in assembly := {}
assemblyJarName in assembly := "myprogram.jar"

exportJars := true

mainClass in Compile := Some("com.company.MyClass")
mainClass in assembly := Some("com.company.MyClass")
mainClass in(Compile, run) := Some("com.company.MyClass")
mainClass in(Compile, packageBin) := Some("com.company.MyClass")

它似乎可以用sbt assembly 编译,其中一些输出是

[warn] Multiple main classes detected.  Run 'show discoveredMainClasses' to see the list
[info] Including: py4j-0.10.6.jar
[info] Including: scala-library.jar
[info] Checking every *.class/*.jar file's SHA-1.
[info] Merging files...
[warn] Merging 'META-INF/MANIFEST.MF' with strategy 'discard'
[warn] Merging 'META-INF/maven/net.sf.py4j/py4j/pom.properties' with strategy 'discard'
[warn] Merging 'META-INF/maven/net.sf.py4j/py4j/pom.xml' with strategy 'discard'
[warn] Strategy 'discard' was applied to 3 files

一旦我进入命令行,我就会得到

$ java -jar myprogram.jar
no main manifest attribute, in myprogram.jar

我打开我的 jar 并查看 MANIFEST.MF 并找不到那里的主要内容:

Manifest-Version: 1.0
Implementation-Title: myprogram
Implementation-Version: 0.1
Specification-Vendor: default
Specification-Title: myprogram
Implementation-Vendor-Id: default
Specification-Version: 0.1
Implementation-Vendor: default

值得一提的是,我的程序主要是用 Scala 编写的,除了我的主类是用 Java 编写的。

我需要改变什么?

【问题讨论】:

    标签: java scala sbt sbt-assembly


    【解决方案1】:

    根据您显示的日志,我的猜测是 - 在其中一个包含的 jar 的内容中 - 已经有一个 META-INF/MANIFEST.MF,而你的/由 sbt 程序集创建的那个 - 声明了你的主类 - 被丢弃(但我不是 100% 确定,因为您显示的清单内容似乎包含来自您的代码的内容)。

    我认为这是您的清单被丢弃的迹象:

    [警告] 将 'META-INF/MANIFEST.MF' 与策略 'discard' 合并

    我敢打赌,py4j-0.10.6.jar 中已经有一个“META-INF/MANIFEST.MF”。

    如何解决?

    1. 避免使用胖罐子,例如使用http://www.scala-sbt.org/sbt-native-packager,但我想这不是重点
    2. 调整合并策略,详见 https://github.com/sbt/sbt-assembly#merge-strategy。更具体地说,您可以尝试设置MergeStrategy.concat 策略以合并META-INF/MANIFEST.MF

    大概:

    assemblyMergeStrategy in assembly := {
      case PathList("META-INF", "MANIFEST.MF")         => MergeStrategy.concat
      case x =>
        val oldStrategy = (assemblyMergeStrategy in assembly).value
        oldStrategy(x)
    }
    

    【讨论】:

    • 当检测到第二个清单文件时,这会导致我出现重复错误。它不会尝试将多个文件的条目连接到一个文件中
    猜你喜欢
    • 2015-03-07
    • 1970-01-01
    • 2019-07-19
    • 2015-12-10
    • 2015-03-14
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    相关资源
    最近更新 更多