eclipse 在 maven 项目中运行 scala 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26929100/
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-09-19 22:12:06  来源:igfitidea点击:

Running a scala application in maven project

eclipsescalamaven

提问by Fran?ois

I am trying to use maven to handle dependencies in a scala project in Eclipse. But once the project is converted to maven, the application wont run anymore.

我正在尝试使用 maven 来处理 Eclipse 中 scala 项目中的依赖项。但是一旦项目转换为maven,应用程序就不会再运行了。

Here is the steps to reproduce :

以下是重现的步骤:

1) new scala project

1)新的scala项目

  • project name : test
  • finish
  • 项目名称:测试
  • 结束

2) new scala object

2)新的Scala对象

  • name : hello_world
  • finish
  • 名称:hello_world
  • 结束

3) hello_world.scala

3) hello_world.scala

package test

object hello_world {
  def main(args: Array[String]) {
    println("Hello world !!!")
  }
}

4) run scala application

4)运行scala应用程序

  • project : test
  • main class : test.hello_world
  • 项目:测试
  • 主类:test.hello_world

result : working, hello world is printed

结果:工作,你好世界被打印

5) right clic on project -> configure -> convert to mavem project

5) 右键单击​​项目 -> 配置 -> 转换为 mavem 项目

  • group id : test
  • artifact id : test
  • version : 0.0.1-SNAPSHOT
  • packaging : jar
  • finish
  • 组ID:测试
  • 工件 ID:测试
  • 版本:0.0.1-快照
  • 包装:罐
  • 结束

6) run scala application

6)运行scala应用程序

  • project : test
  • main class : test.hello_world
  • 项目:测试
  • 主类:test.hello_world

result :

结果 :

Error: Could not find or load main class test.hello_world

Versions details :

版本详情:

Eclipse 4.4.1 M2E 1.5.0 Scala IDE for Eclipse 4.0.0.nightly-2_11

Eclipse 4.4.1 M2E 1.5.0 用于 Eclipse 4.0.0.nightly-2_11 的 Scala IDE

Generated pom-xml :

生成的 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>test</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

回答by Fran?ois

http://scala-tools.org/repo-releasesis deprecated, here is a working pom.xml :

http://scala-tools.org/repo-releases已弃用,这是一个工作 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>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>

<repositories>
    <repository>
        <id>scala-tools.org</id>
        <name>Scala-tools Maven2 Repository</name>
        <url>https://oss.sonatype.org/content/groups/scala-tools/</url>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>scala-tools.org</id>
        <name>Scala-tools Maven2 Repository</name>
        <url>https://oss.sonatype.org/content/groups/scala-tools/</url>
    </pluginRepository>
</pluginRepositories>

<dependencies>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.11.4</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <sourceDirectory>src</sourceDirectory>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <version>3.2.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.scala-tools</groupId>
            <artifactId>maven-scala-plugin</artifactId>
            <executions>
                <execution>
                    <id>scala-compile-first</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>add-source</goal>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

</project>

To run the application :

运行应用程序:

run -> scala application

运行 -> Scala 应用程序

  • project : test
  • main class : test.hello_world
  • 项目:测试
  • 主类:test.hello_world

回答by user2922316

Go to

project properties->Select scala compiler->Select scala installation and change to scala-2.10->apply->close

Run

     maven clean

Run Scala application

Scala application

then you cant see the output in Console

那么你就无法在控制台中看到输出

回答by Quizzie

Your pom.xml should contain these parts (change the versions of course):

您的 pom.xml 应包含以下部分(当然要更改版本):

Repositories:

存储库:

    <repositories>
        <repository>
            <id>scala-tools.org</id>
            <name>Scala-Tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </repository>
    </repositories>

Plugin repositories:

插件库:

<pluginRepositories>
    <pluginRepository>
        <id>scala-tools.org</id>
        <name>Scala-Tools Maven2 Repository</name>
        <url>http://scala-tools.org/repo-releases</url>
    </pluginRepository>
</pluginRepositories>

Dependencies:

依赖项:

<dependencies>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>${scala.version}</version>
    </dependency>
</dependencies>

Build:

建造:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <version>2.15.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.scala-tools</groupId>
            <artifactId>maven-scala-plugin</artifactId>
            <executions>
                <execution>
                    <id>scala-compile-first</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>add-source</goal>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

回答by Shabarinath Volam

May be mine is late reply but it may help some one, Below process fixed my issue.

可能是我的回复晚了,但它可能对某些人有所帮助,下面的过程解决了我的问题。

Please do check whether your source folders is on build path or not. If your source folders is not in build path you can't see run as scala application in runas

请检查您的源文件夹是否在构建路径上。如果您的源文件夹不在构建路径中,您将无法在 runas 中看到作为 scala 应用程序运行

To add your source to build path 1) Right click on project properties 2) In left side pane select Java Build Path and in source tab click on add folder and select your source folder and click ok

将源添加到构建路径 1) 右键单击​​项目属性 2) 在左侧窗格中选择 Java Build Path,在源选项卡中单击添加文件夹并选择您的源文件夹,然后单击确定