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

标签 eclipse scala maven

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

重现步骤如下:

1) 新建scala项目

  • 项目名称:测试
  • 完成

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

结果:工作正常,打印出 hello world

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

  • 组号:测试
  • Artifact 编号:测试
  • 版本: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 用于 Eclipse 的 Scala IDE 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>

最佳答案

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>

运行应用程序:

运行 -> scala 应用程序

  • 项目:测试
  • 主类:test.hello_world

关于eclipse - 在 maven 项目中运行 scala 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26929100/

相关文章:

java - Java项目需要UML逆向工程工具

scala - 惯用的 scala 代码的好例子

tomcat - mvn包tomcat6 :deploy and mvn tomcat6:deploy之间的区别

java - 几种情况因为maven导致tomcat打不开

maven - 如何使用 SonarCloud 分析 Git 子模块?

eclipse - 在 BlackBerry 上安装测试应用程序

java - 无法点击 Android 应用程序中的 ListView 内容

scala - List[OptionT[Future, Int]] 到 OptionT[Future, List[A]]

android - OpenNLP 的模型文件路径是什么?

scala - 如何与ZIO并行优雅地执行多种效果