Siva
Siva

Reputation: 25

How to add dependent project in build.sbt for running sbt compile

I am new to sbt build. I would like to add java files of a dependent project (say Proj A) to my compiling project (Proj B). Running sbt compile in Proj B throws error that dependent project's java package/classes are not found. I went through the link: https://www.scala-sbt.org/0.13/docs/Multi-Project.html but its not clear to me add this dependency to make it work.

I tried adding a below line in build.sbt, but it didnt work. lazy val projB = project.dependsOn(/projA)

Updated

build.sbt of projB:

organization := "com.org"

name := "projB"

version := "1"

resolvers ++= Seq(
  "Typesafe" at "http://repo.typesafe.com/typesafe/releases/",
  "Java.net Maven2 Repository" at "http://download.java.net/maven/2/",
 )

lazy val projB = project.dependsOn(projA)

// the library dependencies of springframework here

build.sbt of Proj A:

organization := "com.org"

name := "proj A"

version := "1"

resolvers ++= Seq(
  "Typesafe" at "http://repo.typesafe.com/typesafe/releases/",
 "Java.net Maven2 Repository" at "http://download.java.net/maven/2/",
 )

// the library dependencies of springframework here

When i do sbt compile on proj B, it throws error the dependent classes are not found. Class Hbase is in Proj A.

[error] import com.org.config.Hbase;
[error] **\hbase\HbaseDAO.java:38:1:
cannot find symbol
[error]   symbol:   class Hbase
[error]   location: class com.org.hbase.HbaseDAO
[error]         private Hbase hbase;
[error] (Compile / compileIncremental) javac returned non-zero exit code
[error] Total time: 6 s, completed 29/08/2019 9:58:39 AM

Updated build.sbt after the suggestion:

inThisBuild(
 Seq(
   organization := "com.org",
   version := "1",
   resolvers ++= Seq(
   "Typesafe" at "http://repo.typesafe.com/typesafe/releases/",
   "Java.net Maven2 Repository" at "http://download.java.net/maven/2/",
  )
 )
)

lazy val root = project
  .in(file("."))
  .aggregate(projA,projB)

lazy val projA = project.settings(
    // project A settings and library dependencies

libraryDependencies += "org.springframework.boot" % "spring-boot-starter- 
parent" % "2.1.6.RELEASE" pomOnly()

libraryDependencies += "org.springframework.boot" % "spring-boot-starter- 
web" % "2.1.6.RELEASE"

libraryDependencies += "org.springframework.data" % "spring-data-hadoop- 
hbase" % "2.3.0.RELEASE"

libraryDependencies += "org.mortbay.jetty" % "jetty" % "7.0.0.pre5"

libraryDependencies += "io.netty" % "netty-all" % "5.0.0.Alpha2"

libraryDependencies += "commons-beanutils" % "commons-beanutils" % "1.9.4"

libraryDependencies += "commons-beanutils" % "commons-beanutils-core" % 
"1.8.3"

libraryDependencies += "xerces" % "xercesImpl" % "2.12.0"

libraryDependencies += "org.apache.hadoop" % "hadoop-yarn-server- 
nodemanager" % "3.2.0"

libraryDependencies += "org.apache.hadoop" % "hadoop-common" % "3.2.0"

libraryDependencies += "org.apache.hadoop" % "hadoop-common" % "2.7.0"

libraryDependencies += "org.apache.hadoop" % "hadoop-client" % "3.2.0"

libraryDependencies += "org.apache.hbase" % "hbase-client" % "2.1.1"

libraryDependencies += "org.apache.hbase" % "hbase" % "2.1.1" pomOnly()

libraryDependencies += "org.apache.hbase" % "hbase-common" % "2.1.1"
)

lazy val projB = project
 .dependsOn(projA)
  .settings(
   // project B settings and library dependencies
libraryDependencies += "org.springframework.boot" % "spring-boot-starter- 
parent" % "2.1.6.RELEASE" pomOnly()

libraryDependencies += "org.springframework.boot" % "spring-boot-starter- 
web" % "2.1.6.RELEASE"

libraryDependencies += "org.springframework.data" % "spring-data-hadoop- 
hbase" % "2.3.0.RELEASE"

libraryDependencies += "org.mortbay.jetty" % "jetty" % "7.0.0.pre5"

libraryDependencies += "io.netty" % "netty-all" % "5.0.0.Alpha2"

libraryDependencies += "commons-beanutils" % "commons-beanutils" % "1.9.4"

libraryDependencies += "commons-beanutils" % "commons-beanutils-core" % 
"1.8.3"

libraryDependencies += "xerces" % "xercesImpl" % "2.12.0"

libraryDependencies += "org.apache.hadoop" % "hadoop-yarn-server- 
nodemanager" % "3.2.0"

libraryDependencies += "com.fasterxml.jackson.core" % "jackson-databind" % 
"2.10.0.pr2"

libraryDependencies += "org.apache.hadoop" % "hadoop-common" % "3.2.0"

libraryDependencies += "org.apache.hadoop" % "hadoop-client" % "3.2.0"

libraryDependencies += "org.apache.hbase" % "hbase-client" % "2.1.1"

libraryDependencies += "org.apache.hbase" % "hbase" % "2.1.1" pomOnly()

libraryDependencies += "org.apache.hbase" % "hbase-common" % "2.1.1"

)

An error is thrown while sbt compile after the below library dependency in both project settings projA and projB

libraryDependencies += "org.springframework.boot" % "spring-boot-starter- 
web" % "2.1.6.RELEASE"

')' expected but string literal found is thrown for this line in projA settings and

';' expected but string literal found is thrown for this line in projB settings.

I couldnt get much clue with this err.

Upvotes: 0

Views: 1638

Answers (1)

laughedelic
laughedelic

Reputation: 6460

Looking at the two snippets you posted, I'm guessing that you have two separate build.sbt files, one for each subproject. This makes them independent and one project just doesn't see the other. While it may be possible to have multiple build.sbt files for the subprojects, it's recommended to define the whole multiproject build in a single build.sbt file in the root of the project.

For example, if you structure your project like this:

├── project
│  ├── build.properties
│  └── plugins.sbt
├── projA
│  └── src
├── projB
│  └── src
└── build.sbt

Then you can put all the build settings and subproject relations in the root build.sbt:

inThisBuild(
  Seq(
    organization := "com.org",
    version := "1",
    resolvers ++= Seq(
      "Typesafe" at "http://repo.typesafe.com/typesafe/releases/",
      "Java.net Maven2 Repository" at "http://download.java.net/maven/2/",
    )
  )
)

lazy val root = project
  .in(file("."))
  .aggregate(projA, projB)

lazy val projA = project
  .settings(
    // project A settings and library dependencies
  )

lazy val projB = project
  .dependsOn(projA)
  .settings(
    // project B settings and library dependencies
  )

Then if you launch an sbt shell from the root of the project, you can call compile (or any other task) to compile both projA and projB, or you can call projA/compile to compile that subproject specifically.

You are already reading documentation, so you know where to find more information. Notice that the link you provided points to the old documentation, at the top there is a banner pointing to the new page: https://www.scala-sbt.org/1.x/docs/Multi-Project.html

Upvotes: 1

Related Questions