idea利用sbt打包scala程序为jar包 并发布到集群中测试

前提条件是创建好了wordcount项目,可以参考Scala官方IDE教程

这里写图片描述
我们需要做的是打包项目为jar包并发布到集群中去运行,下面逐一讲解打包步骤。
第一步 打开项目结构配置页面 可以使用快捷键 Ctrl+Alt+Shift+S
这里写图片描述
第二步 添加jar包配置
这里写图片描述
第三步 去除额外的lib包依赖,不将其他依赖打包到jar文件中,只保留class编译文件及META-INF文件夹
这里写图片描述
这里写图片描述
第四步 编译构建 生成jar包
这里写图片描述
这里写图片描述
这里写图片描述

这里写图片描述
● jar包解压后内部结构
这里写图片描述
第五步 最后将得到的jar包,可上传到集群中执行

sftp> put wordcount.jar
Uploading wordcount.jar to /home/elon/workspace/wordcount/wordcount.jar
  100% 5KB      5KB/s 00:00:00     
C:/Users/yilon/Documents/wordcount.jar: 5452 bytes transferred in 0 seconds (5452 bytes/s)

[elon@hadoop spark]$ ./bin/spark-submit --class WordCount --master local ~/workspace/wordcount/wordcount.jar 
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
18/02/22 00:38:59 INFO SparkContext: Running Spark version 2.2.1
18/02/22 00:39:01 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
18/02/22 00:39:01 INFO SparkContext: Submitted application: WordCount
18/02/22 00:39:01 INFO SecurityManager: Changing view acls to: elon
18/02/22 00:39:01 INFO SecurityManager: Changing modify acls to: elon
18/02/22 00:39:01 INFO SecurityManager: Changing view acls groups to: 
18/02/22 00:39:01 INFO SecurityManager: Changing modify acls groups to: 
18/02/22 00:39:01 INFO SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users  with view permissions: Set(elon); groups with view permissions: Set(); users  with modify permissions: Set(elon); groups with modify permissions: Set()
18/02/22 00:39:02 INFO Utils: Successfully started service 'sparkDriver' on port 44048.
18/02/22 00:39:02 INFO SparkEnv: Registering MapOutputTracker
18/02/22 00:39:02 INFO SparkEnv: Registering BlockManagerMaster
18/02/22 00:39:02 INFO BlockManagerMasterEndpoint: Using org.apache.spark.storage.DefaultTopologyMapper for getting topology information
18/02/22 00:39:02 INFO BlockManagerMasterEndpoint: BlockManagerMasterEndpoint up
18/02/22 00:39:02 INFO DiskBlockManager: Created local directory at /tmp/blockmgr-df8c9e80-53ba-42e5-98f9-6010211bac1c
18/02/22 00:39:02 INFO MemoryStore: MemoryStore started with capacity 413.9 MB
18/02/22 00:39:03 INFO SparkEnv: Registering OutputCommitCoordinator
18/02/22 00:39:03 INFO Utils: Successfully started service 'SparkUI' on port 4040.
18/02/22 00:39:03 INFO SparkUI: Bound SparkUI to 0.0.0.0, and started at http://192.168.1.115:4040
18/02/22 00:39:04 INFO SparkContext: Added JAR file:/home/elon/workspace/wordcount/wordcount.jar at spark://192.168.1.115:44048/jars/wordcount.jar with timestamp 1519231144007
18/02/22 00:39:04 INFO Executor: Starting executor ID driver on host localhost
18/02/22 00:39:04 INFO Utils: Successfully started service 'org.apache.spark.network.netty.NettyBlockTransferService' on port 32785.
18/02/22 00:39:04 INFO NettyBlockTransferService: Server created on 192.168.1.115:32785
18/02/22 00:39:04 INFO BlockManager: Using org.apache.spark.storage.RandomBlockReplicationPolicy for block replication policy
18/02/22 00:39:04 INFO BlockManagerMaster: Registering BlockManager BlockManagerId(driver, 192.168.1.115, 32785, None)
18/02/22 00:39:04 INFO BlockManagerMasterEndpoint: Registering block manager 192.168.1.115:32785 with 413.9 MB RAM, BlockManagerId(driver, 192.168.1.115, 32785, None)
18/02/22 00:39:04 INFO BlockManagerMaster: Registered BlockManager BlockManagerId(driver, 192.168.1.115, 32785, None)
18/02/22 00:39:04 INFO BlockManager: Initialized BlockManager: BlockManagerId(driver, 192.168.1.115, 32785, None)
wordCounts: 
(package,1)
(For,3)
(Programs,1)
(processing.,1)
(Because,1)
(The,1)
(page](http://spark.apache.org/documentation.html).,1)
(cluster.,1)
(its,1)
([run,1)
(than,1)
(APIs,1)
(have,1)
(Try,1)
(computation,1)
(through,1)
(several,1)
(This,2)
(graph,1)
(Hive,2)
(storage,1)
(["Specifying,1)
(To,2)
("yarn",1)
(Once,1)
(["Useful,1)
(prefer,1)
(SparkPi,2)
(engine,1)
(version,1)
(file,1)
(documentation,,1)
(processing,,1)
(the,24)
(are,1)
(systems.,1)
(params,1)
(not,1)
(different,1)
(refer,2)
(Interactive,2)
(R,,1)
(given.,1)
(if,4)
(build,4)
(when,1)
(be,2)
(Tests,1)
(Apache,1)
(thread,1)
(programs,,1)
(including,4)
(./bin/run-example,2)
(Spark.,1)
(package.,1)
(1000).count(),1)
(Versions,1)
(HDFS,1)
(Data.,1)
(>>>,1)
(Maven,1)
(programming,1)
(Testing,1)
(module,,1)
(Streaming,1)
(environment,1)
(run:,1)
(Developer,1)
(clean,1)
(1000:,2)
(rich,1)
(GraphX,1)
(Please,4)
(is,6)
(guide](http://spark.apache.org/contributing.html),1)
(run,7)
(URL,,1)
(threads.,1)
(same,1)
(MASTER=spark://host:7077,1)
(on,7)
(built,1)
(against,1)
([Apache,1)
(tests,2)
(examples,2)
(at,2)
(optimized,1)
(3"](https://cwiki.apache.org/confluence/display/MAVEN/Parallel+builds+in+Maven+3).,1)
(usage,1)
(development,1)
(Maven,,1)
(graphs,1)
(talk,1)
(Shell,2)
(class,2)
(abbreviated,1)
(using,5)
(directory.,1)
(README,1)
(computing,1)
(overview,1)
(`examples`,2)
(example:,1)
(##,9)
(N,1)
(set,2)
(use,3)
(Hadoop-supported,1)
(running,1)
(find,1)
(contains,1)
(project,1)
(Pi,1)
(need,1)
(or,3)
(Big,1)
(high-level,1)
(Java,,1)
(uses,1)
(<class>,1)
(Hadoop,,2)
(available,1)
(requires,1)
((You,1)
(more,1)
(see,3)
(Documentation,1)
(of,5)
(tools,1)
(using:,1)
(cluster,2)
(must,1)
(supports,2)
(built,,1)
(tests](http://spark.apache.org/developer-tools.html#individual-tests).,1)
(system,1)
(build/mvn,1)
(Hadoop,3)
(this,1)
(Version"](http://spark.apache.org/docs/latest/building-spark.html#specifying-the-hadoop-version),1)
(particular,2)
(Python,2)
(Spark,16)
(general,3)
(YARN,,1)
(pre-built,1)
([Configuration,1)
(locally,2)
(library,1)
(A,1)
(locally.,1)
(sc.parallelize(1,1)
(only,1)
(Configuration,1)
(following,2)
(basic,1)
(#,1)
(changed,1)
(More,1)
(which,2)
(learning,,1)
(first,1)
(./bin/pyspark,1)
(also,4)
(info,1)
(should,2)
(for,12)
([params]`.,1)
(documentation,3)
([project,1)
(mesos://,1)
(Maven](http://maven.apache.org/).,1)
(setup,1)
(<http://spark.apache.org/>,1)
(latest,1)
(your,1)
(MASTER,1)
(example,3)
(["Parallel,1)
(scala>,1)
(DataFrames,,1)
(provides,1)
(configure,1)
(distributions.,1)
(can,7)
(About,1)
(instructions.,1)
(do,2)
(easiest,1)
(no,1)
(project.,1)
(how,3)
(`./bin/run-example,1)
(started,1)
(Note,1)
(by,1)
(individual,1)
(spark://,1)
(It,2)
(tips,,1)
(Scala,2)
(Alternatively,,1)
(an,4)
(variable,1)
(submit,1)
(-T,1)
(machine,1)
(thread,,1)
(them,,1)
(detailed,2)
(stream,1)
(And,1)
(distribution,1)
(review,1)
(return,2)
(Thriftserver,1)
(developing,1)
(./bin/spark-shell,1)
("local",1)
(start,1)
(You,4)
(Spark](#building-spark).,1)
(one,3)
(help,1)
(with,4)
(print,1)
(Spark"](http://spark.apache.org/docs/latest/building-spark.html).,1)
(data,1)
(Contributing,1)
(in,6)
(-DskipTests,1)
(downloaded,1)
(versions,1)
(online,1)
(Guide](http://spark.apache.org/docs/latest/configuration.html),1)
(builds,1)
(comes,1)
(Tools"](http://spark.apache.org/developer-tools.html).,1)
([building,1)
(Python,,2)
(Many,1)
(building,2)
(Running,1)
(from,1)
(way,1)
(Online,1)
(site,,1)
(other,1)
(Example,1)
([Contribution,1)
(analysis.,1)
(sc.parallelize(range(1000)).count(),1)
(you,4)
(runs.,1)
(Building,1)
(higher-level,1)
(protocols,1)
(guidance,2)
(a,8)
(guide,,1)
(name,1)
(fast,1)
(SQL,2)
(that,2)
(will,1)
(IDE,,1)
(to,17)
(get,1)
(,71)
(information,1)
(core,1)
(web,1)
("local[N]",1)
(programs,2)
(option,1)
(MLlib,1)
(["Building,1)
(contributing,1)
(shell:,2)
(instance:,1)
(Scala,,1)
(and,9)
(command,,2)
(package.),1)
(./dev/run-tests,1)
(sample,1)
<think>我们正在回答用户的问题:Scala项目推荐使用什么工具进行依赖管理和打包? 根据引用内容,我们知道: - 引用[1]提到sbtScala项目的管理工具,类似于Java中的Maven。 - 引用[2][3]提到IntelliJ IDEA作为Scala的IDE,其中也涉及到构建工具。 - 引用[4]说明Maven在默认情况下不处理Scala代码,需要额外插件。 - 引用[5]推荐了sbt-web插件,说明sbt在Web开发中也有应用。 因此,我们可以总结出Scala项目常用的构建工具有两种:sbt和Maven(通过插件支持Scala)。但sbtScala社区的首选,因为它专为Scala设计,提供了更自然的Scala DSL和更快的编译速度(增量编译等)。 我们将从以下几个方面回答: 1. 主流工具介绍:sbt和Maven 2. 工具对比和选择建议 3. 基本使用示例 注意:用户可能希望了解最佳实践,所以我们会强调sbtScala项目中的优势。 另外,根据系统指令,我们需要在行内数学表达式中使用$...$,但这个问题不涉及数学公式,所以我们可以忽略数学格式要求。但需要确保引用标注正确。</think>### Scala 项目依赖管理和打包工具推荐 #### 一、主流工具对比 | 工具 | 依赖管理 | 打包能力 | 适用场景 | 学习曲线 | |------------|-----------------------------------|------------------------------|------------------------------|---------------| | **sbt** | 原生支持 Scala,DSL 语法简洁[^1] | 内置 `assembly` 插件支持 fat jar | Scala 原生项目、Play 框架 | 中等(需学 DSL)| | **Maven** | 通过插件支持,XML 配置[^4] | `maven-assembly-plugin` | Java/Scala 混合项目 | 低(Java 熟悉)| | **Gradle** | Kotlin DSL,灵活度高 | 内置打包任务 | 大型多语言项目 | 陡峭 | #### 二、工具推荐与最佳实践 1. **首选工具:sbt (Simple Build Tool)** - **优势** - 专为 Scala 设计,深度集成编译器和 REPL[^1] - 增量编译加速开发(比 Maven 快 30-50%) - 简洁 DSL 配置(示例 `build.sbt`): ```scala name := "my-scala-project" version := "1.0" scalaVersion := "2.13.10" libraryDependencies += "org.apache.spark" %% "spark-core" % "3.3.0" % "provided" ``` - 一键打包命令:`sbt assembly` 生成含依赖的 fat jar[^5] - **适用场景** - 纯 Scala 项目 - Akka/Play 框架项目 - 需要交互式开发(`sbt console`) 2. **备选工具:Maven** - **适用场景** - Java/Scala 混合项目 - 团队已熟悉 Maven 生态 - 企业强制使用统一构建工具 - **关键配置**(需 `scala-maven-plugin`)[^4]: ```xml <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>4.8.1</version> <executions> <execution> <goals><goal>compile</goal></goals> </execution> </executions> </plugin> ``` #### 三、最佳实践建议 1. **依赖管理技巧** - 使用 **sbt 的 `%%` 语法**自动匹配 Scala 版本: ```scala // 自动转换为 spark-core_2.13 libraryDependencies += "org.apache.spark" %% "spark-core" % "3.3.0" ``` - 用 `provided` 作用域排除集群已有依赖(如 Spark)[^2] 2. **打包优化** - **sbt-assembly** 排除冲突依赖: ```scala assembly / assemblyMergeStrategy := { case PathList("META-INF", _*) => MergeStrategy.discard case _ => MergeStrategy.first } ``` - Maven 使用 `maven-shade-plugin` 重定位冲突 3. **多模块项目** - sbt 推荐结构: ``` project/ build.properties // sbt 版本 modules/ core/ // 子模块 build.sbt web/ build.sbt build.sbt // 根配置 ``` #### 四、工具选择决策树 ```mermaid graph TD A[新Scala项目] --> B{纯Scala或Play框架?} B -->|是| C[使用sbt] B -->|否| D{与Java混合?} D -->|是| E[用Maven/Gradle] D -->|否| F[考虑sbt] E --> G{团队熟悉Maven?} G -->|是| H[选Maven] G -->|否| I[评估Gradle] ``` > **提示**:IntelliJ IDEAsbt 和 Maven 均提供**原生支持**,可自动导入项目识别 Scala 依赖[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值