Scala:Java 项目中混入scala代码

Spark 是用Scala代码写的。为了调试Spark,做了如下尝试。

 

1、Eclipse下:Java 项目 ,Using Maven,编写了一个java 版Spark应用。 Spark的代码(scala代码可以调试,但是查看变量极不方便,不忍直视,毕竟调试器时Java的,但代码是scala生成的字节码,还是有出入的)。

2、Scala IDE下:Scala 项目,Using Maven,编写一个Scala版的Spark应用。

报找不到类的异常。解决该问题的方法是:http://scala-ide.org/docs/tutorials/m2eclipse/index.html

简单点说:

第一步:升级Maven插件

   因为Scala IDE本事就是一个有Scala插件、Maven插件的Eclipse。但是内置的Maven插件只能创建Java项目,不能创建Scala项目。所以要更新Maven插件。

第二步:创建Maven项目时,选择scala的archetypes。

 

 

但是因为Scala IDE下载相关插件,速度实在是不敢恭维,就此作罢。

 

3、Scala IDE下:Java 项目,Using Maven,编写Scala 版的Spark应用。

在pom.xml中配置 scala编译器,以使使用Maven构建时,会使用Scala相关的编译器对Scala代码进行编译,生成字节码。

这样一来,就可以使用Scala调试器进行调试了。

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.fjn.helper</groupId>
  <version>0.0.1-SNAPSHOT</version>
  <artifactId>frameworkex-apache-spark</artifactId>
  <name>frameworkex-apache-spark</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.10</artifactId>
        <version>2.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-streaming_2.10</artifactId>
        <version>2.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.10.6</version>
    </dependency>

  </dependencies>
    <build>
        <plugins>
            <!-- This plugin compiles Scala files -->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>scala-compile-first</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>scala-test-compile</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            
            <!-- This plugin compiles Java files -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            
            <!-- This plugin adds all dependencies to JAR file during 'package' command.
            Pay EXTRA attention to the 'mainClass' tag.
            You have to set name of class with entry point to program ('main' method) -->

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.3</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>sparkTest1</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

 

posted @ 2017-01-02 23:38  乐享程序员  阅读(6100)  评论(0编辑  收藏  举报