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

时间:2014-11-14 11:35:56

标签: eclipse scala maven

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

以下是重现的步骤:

1)新的scala项目

  • 项目名称:test
  • 光洁度

2)新的scala对象

  • 姓名:hello_world
  • 光洁度

3)hello_world.scala

package test

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

4)运行scala应用程序

  • 项目:测试
  • 主要课程:test.hello_world

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

5)对项目进行正确的陈述 - > configure - >转换为mavem项目

  • group id:test
  • artifact id:test
  • 版本:0.0.1-SNAPSHOT
  • 包装:jar
  • 光洁度

6)运行scala应用程序

  • 项目:测试
  • 主要课程:test.hello_world

结果:

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

版本详情:

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

生成的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>

4 个答案:

答案 0 :(得分:2)

不推荐使用

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>

运行应用程序:

运行 - &gt; scala应用程序

  • 项目:测试
  • 主要课程:test.hello_world

答案 1 :(得分:1)

您的pom.xml应包含这些部分(更改课程版本):

存储库:

    <repositories>
        <repository>
            <id>scala-tools.org</id>
            <name>Scala-Tools Maven2 Repository</name>
            <url>http://scala-tools.org/repo-releases</url>
        </repository>
    </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>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>${scala.version}</version>
    </dependency>
</dependencies>

构建

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

答案 2 :(得分:1)

转到

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

运行

     maven clean

运行Scala application

然后你无法在控制台

中看到输出

答案 3 :(得分:0)

可能是我的迟到回复,但它可能有所帮助,下面的过程解决了我的问题。

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

将源添加到构建路径 1)右键单击项目属性 2)在左侧窗格中选择Java Build Path,在source选项卡中单击add folder并选择源文件夹并单击ok