简体   繁体   English

sbt添加java项目

[英]sbt add java project

Working on some scala software in IDEA IntelliJ. 使用IDEA IntelliJ中的一些scala软件。 (It also features Android if it is essential). (如果必要,它还具有Android功能)。 Suppose I have two projects: 假设我有两个项目:

P1, written in scala, uses sbt as the build tool 用Scala编写的P1使用sbt作为构建工具

P2, written in java, uses gradle as the build tool 用Java编写的P2使用gradle作为构建工具

I want to call some code from P2, having its full source. 我想从P2调用一些代码,具有完整的源代码。 So I import P2 as a module in my IntelliJ scala project. 因此,我将P2导入为IntelliJ scala项目中的模块。 But when I compile my sbt project using sbt-shell, it tells me an error that it cannot find any definitions of classes and packages declared in P2. 但是,当我使用sbt-shell编译sbt项目时,它告诉我一个错误,即找不到在P2中声明的类和包的任何定义。 I guess I need to create a proper reference in my build.sbt file so it could understand where the definitions come from. 我想我需要在build.sbt文件中创建适当的引用,以便它可以理解定义的来源。 I've found one solution which suggests adding unmanaged java source paths to build.sbt like so: 我发现了一个建议将非托管Java源路径添加到build.sbt的解决方案,如下所示:

unmanagedSourceDirectories in Compile += file("mydependency")

but thing is that dependency has dependencies itself. 但问题是依赖项本身具有依赖项。 When I try to compile that, sbt tells me that it cannot find definitions of that dependency. 当我尝试编译该代码时,sbt告诉我它找不到该依赖项的定义。 So I need a way to reference full java project in build.sbt so that I could then compille whole thing via sbt shell (in fact, I forced to use the very sbt shell). 因此,我需要一种在build.sbt中引用完整Java项目的方法,以便随后可以通过sbt shell编译整个东西(实际上,我不得不使用sbt shell)。

Any suggestions? 有什么建议么?

根据我的理解,更好(但不同)的解决方案将是在您的本地存储库(例如publishLocal)中发布jar并依赖它。

When I try to compile that, sbt tells me that it cannot find definitions of that dependency. 当我尝试编译该代码时,sbt告诉我它找不到该依赖项的定义。

Two possible solutions I see: add those dependencies in your build.sbt project or as suggested build the gradle project separately, publish it to your local repository (or local folder) and reference it from sbt project. 我看到了两种可能的解决方案:将这些依赖项添加到build.sbt项目中,或者按照建议的方式分别构建gradle项目,将其发布到本地存储库(或本地文件夹)中,并从sbt项目中引用它。

Possibility three - define a multi module SBT project for both of your projects, and manage the java project also within sbt. 可能性三-为两个项目都定义一个多模块SBT项目,并在sbt中也管理Java项目。 This is a tedious solution, however, as you have to maintain two different build files, so this is only useful if you intend to switch whole hog to SBT. 但是,这是一个单调乏味的解决方案,因为您必须维护两个不同的构建文件,因此,仅当您打算将整头猪切换到SBT时,这才有用。

Otherwise the "publishLocal" solution is probably the least painful. 否则,“ publishLocal”解决方案可能会带来最少的痛苦。

  • P2/Java: put it under Maven, install it into your local maven repository P2 / Java:将其放在Maven下, installinstall到本地Maven存储库中
  • P1/Scala: in build.sbt add an Library Depdendency to P2/Java, eg libraryDependencies += "xyz" % "xyz" % "xyz" . P1 / Scala:在build.sbt向P2 / Java添加库依赖关系,例如libraryDependencies += "xyz" % "xyz" % "xyz" Now all dependencies of P2 are also accessible in P1 现在,在P1中也可以访问P2的所有依赖项
  • As an option for "more convenience": IntelliJ allows you to add an Project Structure (F4) Module Dependency to an separate Java module. 作为“更多便利”的选项:IntelliJ允许您将项目结构(F4)模块依赖项添加到单独的Java模块中。 If you combine the both things above, you can change P2, and those changes are reflected instantly in P1. 如果将以上两项结合在一起,则可以更改P2,这些更改会立即反映在P1中。 Otherwise you would have an binary maven/jar reference, and any change in P2 would need a maven publish, which is not convenient if you own both source bases P1 and P2 and change a lot both of them. 否则,您将有一个二进制Maven / jar引用,并且P2中的任何更改都需要发布Maven,如果同时拥有源库P1和P2并对其进行大量更改,这将不方便。 Side note: Any refresh on build.sbt would remove this explicity added IntellIJ-project-struture-module-dependency to P2. 旁注:对build.sbt任何刷新都会删除此显式添加到P2的IntellIJ-project-struture-module-dependency。 So I propose to turn sbt auto-import off in Intellij and the dependency is kept any time you restart IntelliJ 因此,我建议在sbt auto-import中关闭sbt auto-import ,并在您重新启动IntelliJ时保持依赖关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM