4

I have 3 modules (jvm,stateAccess, persistence), with the following dependency:

jvm->stateAccess->persistence.

I would like to isolate jvm from persistence completely, as if from the point of view of jvm persistence did not exist. So jvm should not be able to import anything from persistence.

However with the following build.sbt file, this is not the case:

lazy val jvm = (project in file( "jvm" ))
  .settings(...)
  .dependsOn( stateAccess % "compile->compile;test->test" )

lazy val persistence = (project in file( "persistence" ))
  .settings(...)

lazy val stateAccess = (project in file( "stateAccess" ))
  .settings(...)
  .dependsOn( persistence )

The problem is that jvm can see persistence .

It is due to a transitivity of the dependency relation in SBT: jvm can import classes from persistence and use them even though jvm does not depend directly on persistence.

In other words, I want to make sure that jvm only depends on stateAccess and cannot use anything from persistence.

Is there a way to forbid such transitive dependencies ?

jhegedus
  • 20,244
  • 16
  • 99
  • 167
  • Possible duplicate of [How to exclude transitive dependencies of other subproject in multiproject builds?](https://stackoverflow.com/questions/25035716/how-to-exclude-transitive-dependencies-of-other-subproject-in-multiproject-build) – thesamet Jan 08 '18 at 06:22
  • 1
    No. That is about multi project, this is about multi module. – jhegedus Jan 08 '18 at 06:24
  • multi module : http://xerial.org/blog/2014/03/26/buidling-multi-module-projects-in-sbt/ – jhegedus Jan 08 '18 at 06:30
  • Also that question talks about libraries. Here are no libraries. In addition to that here the transitivity is due to the dependencies defined inside the `build.sbt.` , in that question I don't even see where the transitivity might come from (it does not come from the build file that I can see). as in : `val coreLib = Projects.coreLib() val consoleApp = Projects.consoleApp().dependsOn(coreLib) val androidApp = Projects.androidProject().dependsOn(coreLib)` ... with these dependencies I don't see how transitivity even can arise. – jhegedus Jan 08 '18 at 06:35
  • If someone thinks that the other question answers this question then please show me how - because I don't see it. – jhegedus Jan 08 '18 at 06:37
  • My bad re the duplicate question. So, I hate to answer a question with a question, but can you define the difference between multimodule and multiproject? The post on Xerial you linked to doesn't clarify it. – thesamet Jan 08 '18 at 06:39
  • In my understanding multi project is where there are multiple jars/classpaths/build.sbt etc involved, each project is a separate compilation entity. Here however, there is only one .jar/build.sbt but several modules (i.e. different locations of source files). – jhegedus Jan 08 '18 at 06:42
  • Here is https://pathtogeek.com/multi-project-builds-with-sbt-0-12-for-scala-project and https://pathtogeek.com/adding-scalastyle-in-a-multi-module-sbt-scala-project ... . – jhegedus Jan 08 '18 at 06:47
  • There might be some confusion here... that I admit.... – jhegedus Jan 08 '18 at 06:49
  • Nevertheless, that other question does not answer this question. At least reading that I have no clue how to solve this question :( because the dependency structure is different and they are talking about dependencies on external projects (probably libraries) and here I am talking about dependencies between internal modules. – jhegedus Jan 08 '18 at 06:51
  • They talk about inter-project dependencies, I talk about intra-project dependencies. "What the projectDepenendencies is doing is what sbt, by default, attempts to do. It converts any inter-project dependencies into ModuleIDs which Ivy will use during resolution." – jhegedus Jan 08 '18 at 07:00
  • Try redefining the dependency between jvm and persistence to be only a runtime dependency. That might take it out of jvm's compile scope. – Dale Wijnand Jan 08 '18 at 09:26
  • Could `exportJars` help you? http://www.scala-sbt.org/1.0/docs/Howto-Classpaths.html#Use+packaged+jars+on+classpaths+instead+of+class+directories – Jorge Jan 08 '18 at 10:19
  • @DaleWijnand how ? – jhegedus Jan 09 '18 at 23:11
  • @JorgeVicenteCantero to me it seems that it just changes the format but not the logic... so i would expect the same behaviour.... if behaviour were different than that should have been documented there.... – jhegedus Jan 09 '18 at 23:14

1 Answers1

-1
lazy val stateAccess = (project in file( "stateAccess" ))
  .settings(...)
  .dependsOn( persistence  % "compile-internal;test-internal" )

Problem solved!

ApolloLi
  • 1
  • 1
  • 1
    Could you please use the [edit] link below the answer to add some explanation as to why the proposed approach solves the problem? – Cindy Meister Oct 10 '19 at 09:41