运行maven scala项目 [英] running a maven scala project

查看:166
本文介绍了运行maven scala项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

mvn:archetype-generate

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

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

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

然后我使用

mvn编译

它说

build success

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

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.

推荐答案

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)
    }
}

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

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

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

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

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

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

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

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

如果如果您要发送应用程序,请使用 maven-jar-plugin 添加 Main-Class Class-Path 清单的条目:

If you want to ship the application, use maven-jar-plugin to add Main-Class and Class-Path entries to the manifest:

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

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

The following configuration does that and also copies all the dependencies (including Scala runtime library) to 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>

现在你只需运行你的应用程序(注意 target / lib 目录是必需的):

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

$ java -jar target/your_app-VERSION.jar

您只需将JAR文件与 / lib 子目录。

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

另见 Exec Maven插件玩Scala和Maven

这篇关于运行maven scala项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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