Eclipse Scala环境的配置

本文详细介绍了在Eclipse IDE上搭建Scala开发环境的过程,包括安装Scala-IDE与m2e-scala模板,配置Scala环境,以及解决常见问题如Aninternalerroroccurredduring:Computingadditionalinfo报错。同时,文章还分享了如何更新Maven依赖库版本,以及在Eclipse中关联Scala源码的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

要开发Scala程序, 一般使用的IDE主要有3种:

  • NetBean
  • Eclipse
  • IDEA

由于个人不太习惯IDEA的一些使用功能, 已经只能打开一个项目.(这可太蠢了.)
所以, 继续选择在Eclipse IDE上集成Scala IDE. 当然, 官方提供了Scala IDE, 可以直接使用. 但是, 如果你之前哟一个版本的Eclipse的话, 个人建议还是升级比较好.


基础知识

关于Eclipse 插件的安装和卸载建议看下我的这篇文章Eclipse 插件管理.


基本操作

主要安装2项东西: scala-IDEm2e-scala模版.

Scala IDE可以选择离线安装与在线安装.


Scala环境配置

  • 选择适合的Scala版本
    在这里插入图片描述
  • 配置Scalabuild path.
    在这里插入图片描述
  • 测试程序运行
    创建Hello.scala, 运行.
object Hello2 {
  def main(args: Array[String]): Unit = {
        println("123")
  }
}

在这里插入图片描述
注意, 如果没有Run as Scala Application. 原因有2个:

  • Scala-IDE插件配置错误;
  • 代码或包错误.

另: 官方的这个操作教学视频还可以, 大家有兴趣可以看看. 相关地址: http://scala-ide.org/download/current.html


Others

代码补齐报错.An internal error occurred during: "Computing additional info".
解决办法: 更换版本 / 不使用代码补全功能.(这个问题非常恶心)
[10]. eclipse安装Scala IDE插件及An internal error occurred during: "Computing additional info"报错解决


Maven

在使用模版创建完成后, 发现并不好用. Scala使用的依赖类库需要跟随自己的使用进行升级.

# 原版
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.yanxml</groupId>
  <artifactId>quick-scala</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>${project.artifactId}</name>
  <description>My wonderfull scala app</description>
  <inceptionYear>2010</inceptionYear>
  <licenses>
    <license>
      <name>My License</name>
      <url>http://....</url>
      <distribution>repo</distribution>
    </license>
  </licenses>

  <properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
    <encoding>UTF-8</encoding>
    <scala.tools.version>2.10</scala.tools.version>
    <scala.version>2.10.0</scala.version>
  </properties>

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

    <!-- Test -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.specs2</groupId>
      <artifactId>specs2_${scala.tools.version}</artifactId>
      <version>1.13</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.scalatest</groupId>
      <artifactId>scalatest_${scala.tools.version}</artifactId>
      <version>2.0.M6-SNAP8</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
    <plugins>
      <plugin>
        <!-- see http://davidb.github.com/scala-maven-plugin -->
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.1.3</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
            <configuration>
              <args>
                <arg>-make:transitive</arg>
                <arg>-dependencyfile</arg>
                <arg>${project.build.directory}/.scala_dependencies</arg>
              </args>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.13</version>
        <configuration>
          <useFile>false</useFile>
          <disableXmlReport>true</disableXmlReport>
          <!-- If you have classpath issue like NoDefClassError,... -->
          <!-- useManifestOnlyJar>false</useManifestOnlyJar -->
          <includes>
            <include>**/*Test.*</include>
            <include>**/*Suite.*</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

# 更新版本
<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.yanxml</groupId>
	<artifactId>quick-scala</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>${project.artifactId}</name>
	<description>My wonderfull scala app</description>
	<inceptionYear>2010</inceptionYear>
	<licenses>
		<license>
			<name>My License</name>
			<url>http://....</url>
			<distribution>repo</distribution>
		</license>
	</licenses>

	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<encoding>UTF-8</encoding>
		<scala.tools.version>2.11</scala.tools.version>
		<scala.version>2.11.8</scala.version>
	</properties>

	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.7</version>
		</dependency>
		<dependency>
			<groupId>org.scala-lang</groupId>
			<artifactId>scala-library</artifactId>
			<version>${scala.version}</version>
		</dependency>



		<dependency>
			<groupId>org.scala-lang</groupId>
			<artifactId>scala-library</artifactId>
			<version>2.11.8</version>
		</dependency>
		<dependency>
			<groupId>org.scala-lang</groupId>
			<artifactId>scala-compiler</artifactId>
			<version>2.11.8</version>
		</dependency>
		<dependency>
			<groupId>org.scala-lang</groupId>
			<artifactId>scala-reflect</artifactId>
			<version>2.11.8</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.scala-lang/scala-actors -->
		<dependency>
			<groupId>org.scala-lang</groupId>
			<artifactId>scala-actors</artifactId>
			<version>2.11.8</version>
		</dependency>
		<!-- Test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
		<!-- <dependency> <groupId>org.specs2</groupId> <artifactId>specs2_${scala.tools.version}</artifactId> 
			<version>3.0</version> <scope>test</scope> </dependency> -->
		<!-- https://mvnrepository.com/artifact/org.scalatest/scalatest -->
		<dependency>
			<groupId>org.scalatest</groupId>
			<artifactId>scalatest_2.11</artifactId>
			<version>3.0.0-M16-SNAP6</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.scalatest</groupId>
			<artifactId>scalatest_${scala.tools.version}</artifactId>
			<version>3.0.0-M16-SNAP6</version>
			<scope>test</scope>
		</dependency>

		<!-- spark -->
		<dependency>
			<groupId>org.apache.spark</groupId>
			<artifactId>spark-core_2.11</artifactId>
			<version>2.2.1</version>
		</dependency>

		<!-- akka -->
		<!-- https://mvnrepository.com/artifact/com.typesafe.akka/akka-actor -->
		<dependency>
			<groupId>com.typesafe.akka</groupId>
			<artifactId>akka-actor_2.11</artifactId>
			<version>2.3.14</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.typesafe.akka/akka-remote -->
		<dependency>
			<groupId>com.typesafe.akka</groupId>
			<artifactId>akka-remote_2.11</artifactId>
			<version>2.3.14</version>
		</dependency>

	</dependencies>

	<build>
		<sourceDirectory>src/main/scala</sourceDirectory>
		<testSourceDirectory>src/test/scala</testSourceDirectory>
		<plugins>
			<plugin>
				<!-- see http://davidb.github.com/scala-maven-plugin -->
				<groupId>net.alchim31.maven</groupId>
				<artifactId>scala-maven-plugin</artifactId>
				<version>3.1.3</version>
				<executions>
					<execution>
						<goals>
							<goal>compile</goal>
							<goal>testCompile</goal>
						</goals>
						<configuration>
							<args>
								<arg>-make:transitive</arg>
								<arg>-dependencyfile</arg>
								<arg>${project.build.directory}/.scala_dependencies</arg>
							</args>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.13</version>
				<configuration>
					<useFile>false</useFile>
					<disableXmlReport>true</disableXmlReport>
					<!-- If you have classpath issue like NoDefClassError,... -->
					<!-- useManifestOnlyJar>false</useManifestOnlyJar -->
					<includes>
						<include>**/*Test.*</include>
						<include>**/*Suite.*</include>
					</includes>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Q1:


Description	Resource	Path	Location	Type
missing or invalid dependency detected while loading class file 'JsonBaseMatchers.class'. Could not access term parsing in package scala.util, because it (or its dependencies) are missing. Check your build definition for missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to see the problematic classpath.) A full rebuild may help if 'JsonBaseMatchers.class' was compiled against an incompatible version of scala.util. (NOTE: It looks like the scala-parser-combinators module is missing; try adding a dependency on "org.scala-lang.modules" : "scala-parser-combinators".        See http://docs.scala-lang.org/overviews/ for more information.)	quick-scala		Unknown	Scala Problem

A1: 更新Maven依赖类库的版本.
scala-eclipse-maven环境搭建

Q2: Eclipse 内无法读取Scala源码. 无法解决! 这个问题是为什么那么多人无奈选择IDEA的原因. 至于其他的部分源码, 可以使用反编译工具. 个人感觉可以内嵌Scala反编译工具到Eclipse里面即可.(感觉个人有兴趣,可以开发一个.)

  1. 在eclipse中添加jdk源码
  2. eclipse 关联源码的两种方式
  3. spark最新源码下载并导入到开发环境下助推高质量代码(Scala IDEA for Eclipse和IntelliJ IDEA皆适用)(以spark2.2.0源码包为例)(图文详解)
  4. attaching-sources-in-intellij-idea-for-scala-project
  5. intellij idea查看scala sdk的源代码
  6. Eclipse安装反编译插件
  7. MyEclipse中android 项目如何解决第三方jar无法关联源码的问题( The JAR of this class file belongs to container ‘Android Private Libraries’ which does not allow modifications to source attachments on its entries.
  8. running-spark-on-eclipse-in-linux
  9. importing-spark-source-code-to-eclipse-ide
  10. 用Eclipse开发Spark应用,如何跟踪Spark源代码
  11. eclipse不识别scala代码
  12. Eclipse中如何关联源码?
  13. scala ide for eclipse搭建spark源码阅读环境报错
  14. Eclipse中Maven引入依赖后自动下载并关联源码(Source)

Reference

[1]. Offical - Scala IDE
[2]. Offical - m2e-scala
[3]. eclipse安装scala环境
[4]. 在eclipse中如何配置运行Scala?
[5]. eclipse安装scala
[6]. 分别用Eclipse和IDEA搭建Scala+Spark开发环境
[7]. maven插件(3) - scala插件scala-maven-plugin
[8]. Scala之——Eclipse离线手动安装Scala插件
[9]. There is “Run Configuration” but no “Run As Scala Application” in Eclipse
[10]. eclipse安装Scala IDE插件及An internal error occurred during: "Computing additional info"报错解决
[11]. 编写代码时,每打一个字符就会弹出“Computing additional info”.Could not initialize class…

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值