scala+java+eclipse+maven开发环境构建

来源:互联网 发布:调度流程的优化的意义 编辑:程序博客网 时间:2024/05/21 01:52

    • 一eclipse配置
    • 二创建新project
    • 三maven配置
    • 四开发机配置

(一)eclipse配置

1、下载eclipse,或者直接下载scala IDE for eclipse
如果使用scala IDE for eclipse,则第2,3步可跳过。

2、在eclipse中安装scalaIDE插件

3、在eclipse中安装m2e插件

4、添加最新的archetype
curl -O http://repo1.maven.org/maven2/archetype-catalog.xml
可以使用remote或者local2种安装方式
(1)remote安装较为方便,直接填入url即可,但以后每次使用均要下载,国外地址下载很慢的
(2)local则需要将上面的xml下载到本地,然后导入:
image

5、eclipse中的maven版本配置
可以在偏好设置中的maven—>installations中指定使用哪个maven,不设置的话会使用eclipse自带的。
如果需要使用一个第三方源,如公司内部的,或者国内的,以提高速度,刚最好指定。见maven部分。

6、显示设置
调整背景,字体颜色等:general—>appearence—>color theme—>frontenddev
调整字体大小:general—>appearence—>color and fonts—>basic—>text font

7、签名设置
在java—>code—>styple—>code template—>comment—>types中添加以下内容:

/** * date: ${date} ${time} * @author LUJINHONG lu_jin_hong@163.com * Function: ${todo} ADD FUNCTION. * last modified: ${date} ${time} */

同时还要选上下面的automatically….
如果其它也要此签名,可以类似设置,包括scala类,getter/setter等均可设置

(二)创建新project

1、创建maven project
image

2、选择archetype
对于java项目,选择maven-archetype-quickstart,版本1.1
对于sala项目,选择scala-archetype-simple,版本1.6
image

3、填写groupid等信息
image

4、等待1分钟左右,eclipse会自动生成相应的目录
image

5、test中有一个类有错误,可以在pom.xml中添加以下依赖即可:

<dependency>    <groupId>org.specs2</groupId>    <artifactId>specs2-junit_${scala.compat.version}</artifactId>    <version>2.4.16</version>    <scope>test</scope></dependency>

或者干脆就把这个类删掉,反正只是一个示例。

6、根据需要在buildpath修改scala和java的版本
一般需要将java改为1.7:
image

同时修改pom.xml中的java与scala版本

7、修改pom.xml,指定源代码位置

<sourceDirectory>src/main/java</sourceDirectory><testSourceDirectory>src/test/java</testSourceDirectory>

默认是src/main/scala,改成java即可,scala的代码会自动包括

8、在pom.xml添加依赖

<dependency>     <groupId>org.apache.spark</groupId>     <artifactId>spark-core_2.10</artifactId>     <version>1.4.1</version></dependency>

9、创建src/main/java源文件目录,并添加java代码。在src/main/scala添加scala代码
image

10、在控制台尝试编译打包

 mvn clean package

注意是否scala代码和java代码都被编译了

(三)maven配置

1、修改$M2_HOME/conf/setting.xml中添加国内的源
(1)配置 mirror

<mirrors>...        <mirror>            <id>osc</id>            <mirrorOf>central</mirrorOf>            <url>http://maven.oschina.net/content/groups/public/</url>        </mirror>        <mirror>            <id>osc_thirdparty</id>            <mirrorOf>thirdparty</mirrorOf>            <url>http://maven.oschina.net/content/repositories/thirdparty/</url>        </mirror>...</mirrors>

(2)配置 profile

<profiles>...        <profile>            <id>osc</id>            <activation>                <activeByDefault>true</activeByDefault>            </activation>            <repositories>                <repository>                    <id>osc</id>                    <url>http://maven.oschina.net/content/groups/public/</url>                </repository>                <repository>                    <id>osc_thirdparty</id>                    <url>http://maven.oschina.net/content/repositories/thirdparty/</url>                </repository>            </repositories>            <pluginRepositories>                <pluginRepository>                    <id>osc</id>                    <url>http://maven.oschina.net/content/groups/public/</url>                </pluginRepository>            </pluginRepositories>        </profile>...</profiles>

详见http://maven.oschina.net/help.html

2、出现以下错误:
Description Resource Path Location Type Could not calculate build plan: Failure to transfer org.apache.maven.plugins:maven-compiler-plugin:pom:2.0.2

执行:
find ~/.m2 -name “*.lastUpdated” -exec grep -q “Could not transfer” {} \; -print -exec rm {} \;
原因是之前有一些下载失败了。
详见http://stackoverflow.com/questions/5074063/maven-error-failure-to-transfer

3、当发现eclipse解释pom.xml失败,或者下载依赖失败时,先到命令行执行mvn clean package,看原因是什么,很有可能是连接中央库失败。

(四)开发机配置

1、统一版本,如java,scala,maven,ant等

2、需要长时间编译打包的程序,无法在本机运行

kinit -kt /home/scheduler/keytab/g18_us.keytab g18_us/scheduler

/home/hadoop/spark/bin/spark-submit –master yarn-client –num-executors 10 –class com.chencai.spark.ml.TrainModel spark_proj-0.0.1.jar test ml_test ./ods_g18_model_data.txt /tmp/chencai/ods_g18_model_train 20151019

./sparkYarn.sh –class com.chencai.spark.ml.TrainModel spark_proj-0.0.1.jar test ml_test ./ods_g18_model_data.txt /tmp/chencai/ods_g18_model_train 20151019

0 0