简体   繁体   English

整合 Groovy 与 Java

[英]Integrating Groovy with Java

I am currently working on integrating Groovy with an existing Java project.我目前正在将 Groovy 与现有的 Java 项目集成。 The Groovy script basically parses XML into objects and then will use existing DAO's to save, update, etc. I have 2 projects. Groovy 脚本基本上将 XML 解析为对象,然后将使用现有的 DAO 来保存、更新等。我有 2 个项目。 One groovy. One java. Using Maven (I used the gmaven plugin), I build Java and then I build the Groovy project.一个 groovy。一个 java。使用 Maven(我使用了 gmaven 插件),我构建了 Java,然后构建了 Groovy 项目。

I would like to be able to call Groovy scripts from the Java project and vice versa if the case need be.如果需要,我希望能够从 Java 项目调用 Groovy 脚本,反之亦然。 I am sure there is a way to do this, but I am unable to do this.我确信有办法做到这一点,但我无法做到这一点。

For example My Java classes are例如我的 Java 类是

State(1 Field)
StateDAO

The Groovy script to be called basically parses an XML document and then populates a table using the DAO with the value,s and for now all I would like it to do is post it to a database.要调用的 Groovy 脚本基本上解析 XML 文档,然后使用 DAO 将值填充到表中,现在我希望它做的就是将其发布到数据库中。

Please let me know if you have any suggestions.如果您有任何建议,请告诉我。 Thanks!谢谢!

Generally speaking, there are two approaches for integrating Groovy with Java: 一般来说,有两种方法可以将Groovy与Java集成:

  1. Make the Java code compile and execute Groovy code at runtime 使Java代码在运行时编译并执行Groovy代码
  2. Compile and package Groovy code as part of your build, just like you do for Java code 编译和打包Groovy代码作为构建的一部分,就像您对Java代码一样

Use 1. if you need a "scripting" solution and the Groovy code to be executed is only known at runtime. 使用1.如果需要“脚本”解决方案,并且要执行的Groovy代码仅在运行时已知。 For example, the code could be loaded from a database, or entered in a GUI screen. 例如,代码可以从数据库加载,或者输入GUI屏幕。 If you simply want to make your life easier by writing some parts of your application in Groovy, go with 2. Two typical examples where this can quickly pay off are XML parsing and testing (of course there are many more). 如果您只是想通过在Groovy中编写应用程序的某些部分来使您的生活更轻松,请使用2.两个典型的示例,其中可以快速得到回报的是XML解析和测试(当然还有更多)。

Assuming you are more interested in 2., you can either compile Groovy and Java separately or together. 假设您对2.更感兴趣,可以单独或一起编译Groovy和Java。 If you compile them separately, static references can only go in one direction, just as if you had two Java modules. 如果单独编译它们,静态引用只能朝一个方向进行,就像你有两个Java模块一样。 If you compile them together, you can mix Groovy and Java code arbitrarily, as if it was all Java code in the same module. 如果将它们一起编译,可以任意混合使用Groovy和Java代码,就好像它是同一模块中的所有Java代码一样。

Compiling Java and Groovy code together is made possible by a Groovy compiler feature called joint compilation . 通过称为联合编译的Groovy编译器功能,可以将Java和Groovy代码编译在一起。 Unfortunately, GMaven has serious problems with joint compilation, and there is no sign that this will change any time soon. 遗憾的是,GMaven在联合编译方面存在严重问题,并且没有任何迹象表明这种情况很快就会发生变化。 For smaller projects you might get away with it, but for larger projects it will bite you (I've been there). 对于较小的项目,你可能会逃脱它,但对于较大的项目,它会咬你(我去过那里)。 There are solutions, but they don't come for free. 有解决方案,但它们不是免费的。 If you are open to (G)Maven alternatives, consider switching to Gradle , which has much better Groovy support. 如果您对(G)Maven替代品持开放态度 ,请考虑切换到Gradle ,它具有更好的Groovy支持。 (Disclaimer: I'm one of the developers of Gradle.) (免责声明:我是Gradle的开发者之一。)

If you decide to stick to GMaven, make sure to get its configuration right. 如果您决定坚持GMaven,请确保正确配置。 Almost every project I see fails at this step, often without realizing it. 我看到的几乎每个项目都在这一步都失败了,往往没有意识到。 The GMaven documentation is outdated, but the Groovy mailing list contains several posts on the topic. GMaven文档已过时,但Groovy邮件列表包含有关该主题的几篇帖子。 You can also copy the configuration from Spock (one of my own projects). 您也可以从Spock (我自己的一个项目)复制配置。

To enable GMaven joint compilation, add the generateStubs goal. 要启用GMaven联合编译,请添加generateStubs目标。 As long as your project compiles without this goal, leave it out. 只要你的项目在没有这个目标的情况下编译,就把它留下来。 There is also a generateTestStubs goal, but it is rarely needed. 还有一个generateTestStubs目标,但很少需要它。

This is the short story. 这是短篇小说。 For the long story, check out the upcoming Manning book Making Java Groovy . 从长远来看,请查看即将发布的Manning一书“ Making Java Groovy” (I'm not affiliated with the author.) (我与作者无关。)

For Peter's #1 suggestion above, I put together a collection of examples using Java to run Groovy scripts in a blog post awhile back that comes along with code samples on github . 对于上面提到的Peter的#1建议,我将一组使用Java的示例放在一个博客文章中运行Groovy脚本 随后在github上附带代码示例 I can also back him up on his comments regarding GMaven and joint compilation - it works, but can end up causing more problems than it's worth. 我也可以支持他关于GMaven和联合编译的评论 - 它可行,但最终会导致更多的问题而不是它的价值。 +1 on considering Gradle as well. 关于Gradle的+1也是如此。 While most of my experience with using it for production is with Java source and Groovy test code, it has so far worked flawlessly for me. 虽然我使用它进行生产的大多数经验都是使用Java源码和Groovy测试代码,但到目前为止它对我来说完美无缺。 Best of luck! 祝你好运!

Adding to answer #1 the creation of GMaven plus , which improved on the joint compilation process and has nice example/doc poml files .添加到答案 #1 创建GMaven plus ,它改进了联合编译过程并且有很好的示例/文档poml 文件

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

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