【发布时间】:2011-12-25 06:26:13
【问题描述】:
如果可以的话,您能否告诉我,如何将 Scala 应用程序导出到可以直接在 JVM 中运行的普通可运行 JAR 中?
谢谢
【问题讨论】:
标签: scala jvm executable-jar
如果可以的话,您能否告诉我,如何将 Scala 应用程序导出到可以直接在 JVM 中运行的普通可运行 JAR 中?
谢谢
【问题讨论】:
标签: scala jvm executable-jar
完全有可能,例如:running a maven scala project。由于 Scala 编译为 Java 字节码,JVM 甚至不知道底层的实现语言。
本质上,在使用 scalac 编译 Scala 源代码后,您将获得一堆 .class 文件,您可以稍后将它们打包到 JAR 中。然后你可以简单地运行它们:
$ java -cp "your.jar:scala-library.jar" com.example.Main
请注意,您必须在 CLASSPATH 中包含 scala-library.jar(目前几乎是 9 MiB...)并指定包含 main 方法的类。
【讨论】:
如果你使用 sbt 来构建,你可以使用其中一个 jar 插件。他们会将所有依赖项放入一个大 jar 文件(包括所有 scala.jar 文件)。这意味着您只需要一个 jar 文件,而不必管理所有依赖项。
以 sbt-assembly 为例(主要是从 https://github.com/sbt/sbt-assembly 复制而来):
项目/plugins.sbt:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "X.X.X")
build.sbt:
import AssemblyKeys._ // put this at the top of the file
seq(assemblySettings: _*)
然后您可以使用以下命令生成 jar:
sbt assembly
【讨论】:
作为 Fabian 答案的替代方案,如果您使用的是 Maven,则可以使用程序集插件。比如:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>package-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifestEntries>
<SplashScreen-Image>splash.png</SplashScreen-Image>
</manifestEntries>
<manifest>
<mainClass>se.aptly.epm.main.PrognosisApp</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
这会将您的所有deps 打包,包括scala-library.jar(如果它在您的deps 中),但会在所有未打包的类的情况下进行扁平化。这是因为可运行的 jar 不能开箱即用地在 jar 中使用 jar 中的代码。
要使这项工作(更好),请使用http://code.google.com/p/onejar-maven-plugin/,我认为这是一个 Maven mojo 包装器到一个罐子:http://one-jar.sourceforge.net/
还有一个用于 one-jar 的 sbt-plugin: https://github.com/sbt/sbt-onejar
【讨论】:
为了将 Swing 应用程序打包到可运行的 jar 中,对我有用的解决方案是将我的项目导出为 普通 jar 文件(不可执行)并更新 jar 的 manifest 到:
您可以在以下路径找到 jar 内的 Manifest 文件(例如,您可以使用 7z 打开):
META-INF/MANIFEST.MF
在清单末尾添加以下行:
Main-Class: myPackage.myMainClass
Class-Path: scala-library.jar scala-swing.jar
现在您的 jar 在单击它时应该可以正常执行了。
注意:您可以在此处找到有关清单自定义的更多信息: http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html.
【讨论】:
她是我的解决方案,maven -->创建scala 可运行的jar。
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<id>scala-compile-first</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<includes>
<include>**/*.scala</include>
</includes>
</configuration>
</execution>
<execution>
<id>scala-test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>xxx.xxx.xxx.Main</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>reference.conf</resource>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
【讨论】: