sbt 创建 多个关联项目

张映 发表于 2019-08-09

分类目录: hadoop/spark/scala

标签:, ,

将多个相关的项目定义在一个构建中是很有用的,尤其是如果它们依赖另一个,而且你倾向于一起修改它们。
每个子项目在构建中都有它们自己的源文件夹,当打包时生成各自的 jar 文件,而且通常和其他的项目一样运转。

1,先通过idea创建一个普通的sbt项目

2,改造项目目录,以下是改造过的

MacBook-Pro:sbt_multi zhangying$ tree -L 3
.
├── build.sbt
├── order       //子项目
│   ├── src
│   │   ├── main
│   │   └── test
│   └── target
│       ├── scala-2.11
│       └── streams
├── project
│   ├── Dependencies.scala
│   ├── build.properties
│   ├── plugins.sbt
│   ├── project
│   │   └── target
│   └── target
│       ├── config-classes
│       ├── scala-2.12
│       └── streams
├── target
│   ├── scala-2.11
│   │   └── resolution-cache
│   └── streams
│       ├── $global
│       ├── compile
│       ├── runtime
│       └── test
└── user   //子项目
    ├── src
    │   ├── main
    │   └── test
    └── target
        ├── scala-2.11
        └── streams

29 directories, 4 files

3,sbt版本

# cat project/build.properties
sbt.version = 1.2.8

4,安装assembly插件

# cat project/plugins.sbt
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.9")

5,创建project/Dependencies.scala

# cat project/Dependencies.scala
import sbt._

object Dependencies {

    // 统一管理依赖版本
    val akkaVersion = "2.5.4"
    val sparkVersion = "2.3.0"

    // Akka Libraries
    val akkaActor = "com.typesafe.akka" %% "akka-actor" % akkaVersion
    val akkaTestkit = "com.typesafe.akka" %% "akka-testkit" % akkaVersion

    //json
    val json = "com.alibaba" % "fastjson" % "1.2.49"

    // user子项目的依赖,在build.sbt中使用
    val spark =
        Seq(
            "org.apache.spark" % "spark-core_2.11" % sparkVersion,
            "org.apache.spark" % "spark-sql_2.11" % sparkVersion
        )

    // order子项目的依赖,在build.sbt中使用
    val orderPro = Seq(akkaActor,akkaTestkit,json)
}

6,创建build.sbt

import Dependencies._

lazy val root = (project in file("."))
        .aggregate(user,order)   //对应下面的子项目,文件夹名
        .settings(
            inThisBuild(List(
                organization := "com.mytest",
                scalaVersion := "2.11.8",
                version := "0.0.1-SNAPSHOT",
                externalResolvers := List("Aliyun Mirror" at "http://maven.aliyun.com/nexus/content/groups/public")
            )),
            name := "mytest"
        )

//user 子项目
lazy val user = project
        .settings(
            name := "mytest-user",
            libraryDependencies ++= spark   // 引入Dependencies.scala中的依赖设置
        )

//order 子项目
lazy val order = project
        .dependsOn(user)   // order子项目依赖于user子项目
        .settings(
            name := "mytest-order",
            mainClass in assembly := Some("com.mytest.WordCount"),
            libraryDependencies ++= orderPro  // 引入Dependencies.scala中的依赖设置
        )

assemblyMergeStrategy in assembly := {
    case PathList("javax", "inject", xs @ _*)          => MergeStrategy.first
    case PathList("org", "apache", xs @ _*) 		   => MergeStrategy.first
    case "application.conf"                            => MergeStrategy.concat
    case "git.properties"                              => MergeStrategy.first
    case PathList("org", "aopalliance", xs @ _*)       => MergeStrategy.first
    case "unwanted.txt"                                => MergeStrategy.discard
    case m if m.toLowerCase.endsWith("manifest.mf")    => MergeStrategy.discard
    case PathList("META-INF", xs @ _*) => MergeStrategy.discard
    case x =>
        val oldStrategy = (assemblyMergeStrategy in assembly).value
        oldStrategy(x)
}

7,加载包后,看一下结果

sbt创建多个关联项目

sbt创建多个关联项目



转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/hadoop/2169.html