栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Scala 发布可执行Jar包

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Scala 发布可执行Jar包

对于有些场景,希望scala 编写得程序能够封装成黑盒直接运行。

java -jar mypack.jar 

有两种打包方式

1. 命令行打包

对于一个Scala 项目有两种方式打包

  • sbt pacakge 打包但是不按照依赖包,瘦包,往往比较小
  • sbt assmbly 将所有得依赖都打入jar包,俗称fat jar, 往往比较大几十M
resolvers += DefaultMavenRepository

val spinalVersion = "1.7.2"
val spinalIdslPlugin = compilerPlugin("com.github.spinalhdl" %% "spinalhdl-idsl-plugin" % spinalVersion)

lazy val app = (project in file("."))
  .settings(
    name := "reggen",
    version := "0.1",
    scalaVersion := "2.11.12",
    organization := "xx.com",
    Compile / mainClass := Some("xlab.json.regifApp"),
    libraryDependencies ++= Seq(spinalIdslPlugin,
      "com.github.spinalhdl" %% "spinalhdl-core" % spinalVersion,
      "com.github.spinalhdl" %% "spinalhdl-lib" % spinalVersion,
      "org.scala-lang.modules" %% "scala-swing" % "3.0.0",
      "com.google.code.gson" % "gson" % "2.9.1",
      "org.apache.poi" % "poi" % "3.17",
      "org.apache.poi" % "poi-ooxml" % "3.17"
    )
  )

fork := true
sbt package

打包后在./target/scala-2.11/reggen_2.11-0.1jar, 只有900k

java -jar .reggen_2.11-0.1.jar 
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: scala/Product
        at java.lang.ClassLoader.defineClass1(Native Method)
        ....
sbt assembly

sbt assembly 直接运行不生效,

[info] set current project to regGen (in build file:/D:/xmLab/lab/misoc/)
[error] Not a valid command: assembly
[error] Not a valid project ID: assembly
[error] Expected ':'
[error] Not a valid key: assembly
[error] assembly
[error]         ^

需要添加依赖插件:https://github.com/sbt/sbt-assembly
在build.sbt 添加 assembly / mainClass := Some("xlab.json.regifApp"),

    Compile / mainClass := Some("xlab.json.regifApp"),
    assembly / mainClass := Some("xlab.json.regifApp"),
    libraryDependencies ++= Seq(spinalIdslPlugin,
      "com.github.spinalhdl" %% "spinalhdl-core" % spinalVersion,

重新运行 sbt assembly

$ sbt assembly
[info] welcome to sbt 1.5.5 (Oracle Corporation Java 1.8.0_301)
[info] loading global plugins from C:UsersXXX.sbt1.0plugins
[info] loading settings for project XXXX-build from plugins.sbt ...
[info] loading project definition from D:XXXXXXXXXXXproject
[info] loading settings for project app from build.sbt ...
[info] set current project to regGen (in build file:/D:/XXXX/lab/XXXX/)
[info] Strategy 'concat' was applied to a file (Run the task at debug level to see details)
[info] Strategy 'deduplicate' was applied to 2 files (Run the task at debug level to see details)
[info] Strategy 'discard' was applied to 22 files (Run the task at debug level to see details)
[success] Total time: 21 s, completed 2022-8-8 23:57:45

打包成功

运行 jar 包 成功

2. IDEA打包

step1:

step2:

step3:

step4:


step5:
查看out/artifacts 路径

运行成功

Build.sbt

重新整理后的Build.sbt

val appname = "xxx"
val appversion = "0.1.1"

val spinalVersion = "1.7.2"

lazy val app = (project in file("."))
  .settings(
    name         := appname,
    version      := appversion,
    scalaVersion := "2.11.12",
    organization := "xxx.com",
    assembly / mainClass       := Some("app.migen"),
    assembly / assemblyJarName := s"${appname}-${appversion}.jar",
    assembly / target          := file("out/artifacts/migen/"),
    libraryDependencies ++= Seq(
      "com.github.spinalhdl" %% "spinalhdl-core" % spinalVersion,
      "com.github.spinalhdl" %% "spinalhdl-lib" % spinalVersion,
      compilerPlugin("com.github.spinalhdl" %% "spinalhdl-idsl-plugin" % spinalVersion),
      "org.scala-lang.modules" %% "scala-swing" % "3.0.0",
      "com.google.code.gson" % "gson" % "2.9.1",
      "org.apache.poi" % "poi" % "3.17",
      "org.apache.poi" % "poi-ooxml" % "3.17"
    )
  )

fork := true

只需要 sbt assembly 就会发布到out/artifacts/xxx/xxx-0.1.1.jar

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1039349.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号