使用 Maven 编译 Scala [英] Compiling Scala Using Maven

查看:47
本文介绍了使用 Maven 编译 Scala的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a hello world application using maven.

这是我的 pom.xml:

here is my 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 代码:

and here is my scala code:

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

当我跑步时

mvn package

它会在目标目录中生成一个 jar 文件.然后我想要做的是使用

it generates a jar file in the target directory. Then what I want to be able to do is run that jar file using

scala target/ColossusPlay-0.0.1-SNAPSHOT.jar

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

However I get NullPointer Exception like this:

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 看到源文件.

The problem appears to be that the maven build does not see the source files. I tried to force it to have a build error writing nonesense to the source file but the mvn package still says build success. Additionaly when I examine the jar file, there isn't any class files inside. How can I make the maven see the source files.

推荐答案

你必须添加一个规模编译器插件比如 sbt-compiler 插件

You have to add a scale compiler plugin such sbt-compiler plugin

SBT 编译器插件

示例 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>

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

Using maven with scala in specific use case ( profiles, complex deployments, deep hierarchy ) is even better than pure sbt , but at the beginning is a bit tricky.

这篇关于使用 Maven 编译 Scala的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆