运行 maven scala 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6758258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:13:57  来源:igfitidea点击:

running a maven scala project

scalamavenintellij-idea

提问by swordfish

Im starting to learn scala and mongo , my IDE is intellij IDEA. I created a scala project using

我开始学习 scala 和 mongo ,我的 IDE 是 intellij IDEA。我使用创建了一个 Scala 项目

mvn:archetype-generate

and typed a simple hello world program in the IDEA with some arithmetic options such as

并在 IDEA 中键入一个简单的 hello world 程序,其中包含一些算术选项,例如

println(5)
val i = 1+2
println(i)

Then i compiled it using

然后我编译它使用

mvn compile

编译

It said

它说

build success

But now how should i execute my application and verify the output. There isn't a single article which explains how to start off with scala,maven,idea and i am entirely new to all of this. any help would be useful for me.

但是现在我应该如何执行我的应用程序并验证输出。没有一篇文章解释了如何从 scala、maven、idea 开始,我对所有这些都是全新的。任何帮助都会对我有用。

回答by Tomasz Nurkiewicz

maven-exec-plugin

maven-exec-plugin

Try with this code:

试试这个代码:

package com.example

object Main {
    def main(args: Array[String]) {
        println(5)
        val i = 1 + 2
        println(i)
    }
}

Place it under /src/main/scala/com/example/Main.scalaand run it using:

将它放在下面/src/main/scala/com/example/Main.scala并使用以下命令运行它:

$ mvn package exec:java -Dexec.mainClass=com.example.Main

If you don't want to pass mainClassmanually, you can do this in plugin configuration:

如果您不想mainClass手动传递,可以在插件配置中执行此操作:

<plugins>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1</version>
    <configuration>
      <mainClass>com.example.Main</mainClass>
    </configuration>
  </plugin>
</plugins>

There are other possibilities, this is the easiest one. Of course in IntelliJ you should be able to run the program directly.

还有其他可能性,这是最简单的一种。当然在 IntelliJ 中你应该能够直接运行程序。

maven-jar-plugin

Maven jar 插件

If you want to ship the application, use maven-jar-pluginto add Main-Classand Class-Pathentries to the manifest:

如果你想出货的应用程序,使用maven-jar-plugin添加Main-ClassClass-Path条目清单:

Main-Class: com.example.Main
Class-Path: lib/scala-library-2.9.0-1.jar lib/slf4j-api-1.6.1.jar ...

The following configuration does that and also copies all the dependencies (including Scala runtime library) to target/lib.

以下配置执行此操作并将所有依赖项(包括 Scala 运行时库)复制到target/lib.

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.example.Main</mainClass>
                <addClasspath>true</addClasspath>
                <classpathLayoutType>custom</classpathLayoutType>
                <customClasspathLayout>lib/$${artifact.artifactId}-$${artifact.version}$${dashClassifier?}.$${artifact.extension}
                </customClasspathLayout>
            </manifest>
        </archive>
    </configuration>
</plugin>
<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <outputDirectory>${project.build.directory}/lib</outputDirectory>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Now you can simply run your application by (note the target/libdirectory is required):

现在您可以简单地通过以下方式运行您的应用程序(注意target/lib目录是必需的):

$ java -jar target/your_app-VERSION.jar

You can ship your application simply by copying your JAR file along with /libsubdirectory.

您只需复制 JAR 文件和/lib子目录即可发布您的应用程序。

Also see Exec Maven Pluginand Playing with Scala and Maven.

另请参阅Exec Maven 插件Playing with Scala 和 Maven