2011-11-09 60 views

回答

14

这是完全可能的,请参阅例如:running a maven scala project。由于Scala编译为Java字节码,JVM甚至不知道底层的实现语言。

本质上,在使用scalac编译Scala资源之后,您将得到一堆.class文件,您可以稍后将其打包到JAR中。然后,你可以使用简单的运行它们:

$ java -cp "your.jar:scala-library.jar" com.example.Main 

请注意,您必须包括scala-library.jar的CLASSPATH(目前它几乎是9 MIB ...)并指定包含main方法类。

15

如果你使用sbt来构建,你可以使用其中的一个jar插件。他们将把所有的依赖关系放入一个大的jar文件(包括所有的scala.jar文件)。这意味着你只需要一个jar文件,不必管理所有的依赖项。

与SBT-组件(主要来自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: _*) 

然后就可以生成罐子用:

sbt assembly 
+1

该网址无效。尝试https://github.com/sbt/sbt-assembly –

5

作为Fabian答案的替代方案,如果您使用的是Maven,那么您可以使用assembly-plugin。喜欢的东西:

<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起来,包括斯卡拉 - library.jar(如果它在你的DEPS),但这样做有解开所有类夷为平地。这是因为可运行jar无法在jar中使用jar中的代码。

,使这项工作(这是更好),使用http://code.google.com/p/onejar-maven-plugin/,我认为这是一个Maven的魔力包装到一个罐子:http://one-jar.sourceforge.net/

还为一个广口瓶中的SBT-插件: https://github.com/sbt/sbt-onejar

1

为了打包运行的JAR Swing应用程序,为我工作的解决方案是我的项目导出为正常的jar文件(非执行),并更新JAR的清单到:

  1. add scala-library.jar scala-swing。jar包的路径
  2. 表明主类

您可以在以下路径的jar(你可以用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

0

她是我的解决方案,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> 
相关问题