[英]How to add java project dependency to play framework 2.2.1 java project
[英]Play Framework 2.4.0 - How to add an external java project dependency to play-java application project
我使用以下命令創建了一個play-java Web應用程序:
激活器新的“ c:\\ workspace \\ PlayServer”
我正在Eclipse中工作,並在同一工作區中創建了另一個名為“ Common”的Java項目(常規Java項目)。
現在,我具有以下項目結構:
c:\workspace
\--- Common (Java project)
\--- src
\--- comm
\--- messages
\--- PlayServer (Play-java 2.4.0 web application project)
\--- app/controllers
\--- app/views
\--- conf
\--- project
我想訪問“ PlayServer”中“ Common”項目的類。 在Eclipse中-我已經將“ Common”項目依賴項添加到了Play應用程序項目的Java構建路徑中,並且所有項目都已成功構建,並且我可以在“ PlayServer” Play應用程序項目中訪問“ Common”的所有類。 。
但是,當我使用激活程序運行命令運行或調試“ PlayServer”時,出現了以下錯誤: [錯誤] c:\\ workspace \\ PlayServer \\ app \\ controllers \\ Application.java; 6
錯誤:程序包comm.messages.WebMessage不存在
[錯誤]導入comm.messages.WebMessage
這意味着該播放框架找不到“ Common” java eclipse項目的類。 在搜索了很多有關此問題的信息后,我發現通常有3種解決方案:
1.發布到我的外部Java項目到Ivy,並在play項目的build.sbt文件中進行解析,如下所示:
libraryDependencies ++= Seq(
// Adding it here somehow..
)
但是我沒有找到任何簡單的教程可以指導我如何發布外部Java項目並在play項目的build.sbt文件中解決該問題。 構建sbt文件對我來說有點神秘。
2.在播放項目的build.sbt中直接引用我的外部項目。 我嘗試在build.sbt上執行以下操作 - 這也沒有真正起作用:
// 選項1:
lazy val core = ProjectRef(file("../Common"), "Common")
lazy val root = (project in file(".")).enablePlugins(PlayScala).dependsOn(common )
//選項2:
lazy val common = RootProject(file("../common"))
lazy val root = (project in file(".")).enablePlugins(PlayScala).dependsOn(common )
//選項3:
lazy val common = file("../common").getAbsoluteFile.toURI
lazy val root = (project in file(".")).enablePlugins(PlayScala).dependsOn(common )
我已經閱讀了SBT文檔,我應該在“ Common”項目中添加一個“ build.sbt”文件,但是我不知道在其中聲明什么。 萬一“ Common”項目也必須是SBT項目,我知道所有Java源代碼都應該在“ / src / main / java”中,但我在SBT文檔中看到它是可配置的。 因此, 主要問題是我仍然無法使我的外部Java項目被我的play網絡應用程序編譯和識別。
現在,我只是在“公共”項目目錄中創建了一個空的build.sbt文件。
3.在我的play項目的根目錄中創建一個lib /目錄,然后將jar文件添加到該目錄中。 此解決方案將要求我的每個外部Java項目(如“ Common”)都需要在每次構建中自動創建“ jar”文件,並將生成的jar復制到“ PlayServer” lib目錄中 。 另外,我已經讀到“ lib”解決方案是針對非托管依賴的 ,因此我不確定該解決方案是否有效和正確,因為play框架將SBT與ivy一起使用。
我的問題是:哪種方法最適合使用和推薦使用? 我該如何實施呢?
我沒有找到有關此問題的最佳實踐,也沒有找到可以演示如何執行上述任何解決方案的基本教程。
在說明了Play用於打包獨立項目的結構的文檔之后 ,我發現這種方法可以很好地工作。
帶有Git的兩個存儲庫,一個用於common
存儲庫,一個用於PlayServer
存儲庫。 結帳Common
到自己的工作空間,但也簽入PlayServer
的工作空間中的文件夾命名中modules
:
c:\workspace
\--- Common (checked out from Git)
\--- src
\--- comm
\--- messages
\--- PlayServer (Play-java 2.4.0 web application project)
\--- app/controllers
\--- app/views
\--- conf
\--- project
\--- modules
\--- Common (checked out from Git)
\--- src
\--- comm
\--- messages
(請注意,如何兩次檢出Common
到其自己的工作區,以及在PlayServer/modules
。)
在PlayServer
應用程序的.gitignore
中,您可以告訴它忽略modules\\Common
因為它具有自己的單獨的Git存儲庫。
該文檔說明了如何使modules/Common
和PlayServer
作為單獨的項目一起工作,並且在GitHub上有一個示例顯示了Play子項目的工作方式。 請注意,此示例使用的是Play 2.3,但是2.4的《 遷移指南 》未提及任何更改,因此可以采取相同的步驟。
Build.sbt
類似於:
lazy val root = project.in(file("."))
.aggregate(common)
.dependsOn(common)
lazy val common = project.in(file("../common"))
其他所有內容均按照我上面鏈接的示例進行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.