使用Maven编译Scala

时间:2016-09-19 06:31:26

标签: scala maven colossus

我想使用maven创建一个hello world应用程序。

这是我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>ColossusPlay</groupId>
  <artifactId>ColossusPlay</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
      <dependency>
        <groupId>com.tumblr</groupId>
        <artifactId>colossus-metrics_2.10</artifactId>
        <version>0.8.1-RC1</version>
    </dependency>
  </dependencies>
</project>

这是我的scala代码:

object Main extends App{
  println( "Helo World" )
}

当我跑

mvn package

它在目标目录中生成一个jar文件。那么我想要做的就是使用

运行该jar文件
scala target/ColossusPlay-0.0.1-SNAPSHOT.jar

但是我得到像这样的NullPointer异常:

java.lang.NullPointerException
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at scala.reflect.internal.util.ScalaClassLoader$$anonfun$tryClass$1.apply(ScalaClassLoader.scala:43)
    at scala.reflect.internal.util.ScalaClassLoader$$anonfun$tryClass$1.apply(ScalaClassLoader.scala:43)
    at scala.util.control.Exception$Catch$$anonfun$opt$1.apply(Exception.scala:119)
    at scala.util.control.Exception$Catch$$anonfun$opt$1.apply(Exception.scala:119)
    at scala.util.control.Exception$Catch.apply(Exception.scala:103)
    at scala.util.control.Exception$Catch.opt(Exception.scala:119)
    at scala.reflect.internal.util.ScalaClassLoader$class.tryClass(ScalaClassLoader.scala:42)
    at scala.reflect.internal.util.ScalaClassLoader$class.tryToInitializeClass(ScalaClassLoader.scala:39)
    at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.tryToInitializeClass(ScalaClassLoader.scala:101)
    at scala.reflect.internal.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:63)
    at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
    at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:22)
    at scala.tools.nsc.JarRunner$.run(MainGenericRunner.scala:13)
    at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:29)
    at scala.tools.nsc.JarRunner$.runJar(MainGenericRunner.scala:25)
    at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:69)
    at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:87)
    at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:98)
    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:103)
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

我错过了什么?

更新

问题似乎是maven构建没有看到源文件。我试图强制它有一个构建错误写无意义的源文件,但mvn包仍然说建立成功。另外,当我检查jar文件时,里面没有任何类文件。如何让maven看到源文件。

1 个答案:

答案 0 :(得分:1)

您必须添加一个缩放编译器插件,例如sbt-compiler插件

SBT compiler plugin

Example pom

 <plugin>
            <groupId>com.google.code.sbt-compiler-maven-plugin</groupId>
            <artifactId>sbt-compiler-maven-plugin</artifactId>
            <version>1.0.0-beta9</version>
            <executions>
                <execution>
                    <id>default-sbt-compile</id>
                    <goals>
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

在特定用例(配置文件,复杂部署,深层次结构)中使用maven和scala甚至比纯sbt更好,但在开始时有点棘手。