【问题标题】:Is this build.sbt ok for packaging?这个 build.sbt 可以打包吗?
【发布时间】:2022-02-02 00:40:17
【问题描述】:

我正在尝试将非托管 jar 与一些 Scala 代码一起打包。我使用 IntelliJ 进行编程,但不确定是否正确构建了包。

我的build.sbt如下:

name := "InvokeCompiler"

version := "1.0"

scalaVersion := "2.11.8"

exportJars := true

val parserLocation = "lib/parser-0.0.1.jar"

mappings in (Compile, packageBin) ~= {

    _.filter(!_._1.getName.startsWith("Main"))
}

//unmanagedJars in Compile += file(parserLocation)

mappings in (Compile, packageBin) <+= baseDirectory map { base =>

    (base / parserLocation) -> "parser-0.0.1.jar"
}

我想创建一个新的 jar 文件,其中包含非托管 jar 和我编写的代码。这个 jar 将被转换为 .dll 以便在 C# 中使用。但是,当这样做时,IKVMC 会抛出各种警告。当我添加 .dll 时,它会生成 .dll 只包含我自己编写的类。

编辑: 阅读福特先生的评论后,以下是我在生成的 jar 上运行 ikvmc 得到的警告和错误:

PROMPT:> ikvmc -target:library compiled.jar
IKVM.NET Compiler version 7.2.4630.5
Copyright (C) 2002-2012 Jeroen Frijters
http://www.ikvm.net/

note IKVMC0002: Output file is "compiled.dll"
warning IKVMC0100: Class "org.nlogo.core.FrontEndInterface" not found
warning IKVMC0100: Class "scala.Tuple2" not found
warning IKVMC0100: Class "scala.reflect.ScalaSignature" not found
warning IKVMC0100: Class "scala.Option" not found
warning IKVMC0100: Class "org.nlogo.core.Program" not found
warning IKVMC0100: Class "scala.collection.immutable.ListMap" not found
warning IKVMC0100: Class "org.nlogo.core.ExtensionManager" not found
warning IKVMC0100: Class "org.nlogo.core.CompilationEnvironment" not found
warning IKVMC0100: Class "org.nlogo.core.Femto$" not found
warning IKVMC0111: Emitted java.lang.NoClassDefFoundError in "Interface.ASTSingleton$.getFrontEndCompiledAsJSON(Ljava.lang.String;)Lscala.Tuple2;"
    ("org.nlogo.core.FrontEndInterface")
warning IKVMC0111: Emitted java.lang.NoClassDefFoundError in "Interface.ASTSingleton$.getFrontEndSingletion()Lorg.nlogo.core.FrontEndInterface;"
    ("org.nlogo.core.Femto$")

【问题讨论】:

    标签: scala jar sbt


    【解决方案1】:

    你没有说你是如何构建它的,所以这可能是第一个问题。具体来说,您应该使用sbt publish-local 命令。要验证依赖项是否包含,只需解压缩 JAR 文件并查看。

    如果您需要生成的 JAR 文件是可执行的,那么您应该将其添加到您的 build.sbt:

    mainClass in Compile := Some("name.of.your.main.Class")
    

    name.of.your.main.Class 替换为您的班级名称。您正在做类似但可能存在问题的事情:

    mappings in (Compile, packageBin) ~= {
        _.filter(!_._1.getName.startsWith("Main"))
    }
    

    这意味着任何类名不以Main 开头的东西都将被过滤掉。除非您对此有充分的理由,否则我会摆脱它并明确指出 package main 方法。 mappings 所做的是described here

    【讨论】:

    • 我不希望 jar 有一个 main 方法,我希望它是一个 lib,仅此而已。通过提取生成的 jar 的内容,似乎添加了所有依赖项。那么问题仍然是为什么ikvmc不能将其转换为dll。谢谢,至少我的 sbt 似乎还可以。 :)
    • @Marin 乐于助人!请记住支持和/或接受有助于您找到解决方案或阐明这种情况的答案。通过这样做,人们将来更有可能帮助您!我还会用您收到的警告更新您的原始帖子,因为它可能会提供更多线索,让我们为您提供帮助。
    猜你喜欢
    • 2018-10-17
    • 2011-03-14
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    相关资源
    最近更新 更多