简体   繁体   English

使用Eclipse从Java类中引用Groovy域类?

[英]Reference Groovy domain class from Java class using Eclipse?

How can I reference a Groovy domain class from Java class using Eclipse? 如何使用Eclipse从Java类引用Groovy域类?

I've put my domain class in package: 我将域类放在包中:

package com.me.myproject
public class Person {
    String name
    int age
}

Then in my Java class I attempt to reference com.me.myproject.Person . 然后在我的Java类中,尝试引用com.me.myproject.Person This works for grails run-app (command line) but not Eclipse. 这适用于grails run-app (命令行),但不适用于Eclipse。 Eclipse can't resolve the Groovy domain class. Eclipse无法解析Groovy域类。

I'm running Eclipse 3.4.1 with the latest Groovy and Grails Eclipse plugins: 我正在使用最新的Groovy和Grails Eclipse插件运行Eclipse 3.4.1:

  • Grails Eclipse Feature 0.1.0 20081120_2330 Grails Eclipse功能0.1.0 20081120_2330
  • GroovyFeature 1.5.7.20081120_2330 Groovy功能1.5.7.20081120_2330

I've tried setting the Eclipse default output folder to the same as Groovy compiler output location. 我尝试将Eclipse默认输出文件夹设置为与Groovy编译器输出位置相同。 I've also tried both enabling and disabling the “Disable Groovy Compiler Generating Class Files” setting. 我还尝试了启用和禁用“禁用Groovy编译器生成类文件”设置。 I've also tried not putting any of my classes in a package. 我也尝试过不将任何类放在包中。 None of these work. 这些都不起作用。

I haven't been able to make this work in eclipse either. 我也无法使这项工作日食。 Until spring gets the groovy / grails eclipse plugins whipped up to do something other than mere color coding your best bet is to use an IDE like Intelij which has the best groovy support at the moment. 直到spring出现groovy / grails eclipse插件,他们要做的不仅仅是单纯的颜色编码,您最好的选择是使用像Intelij这样的IDE,它目前具有最佳的Groovy支持。

Did you try the Groovy Plugin ? 您尝试过Groovy插件吗? It may do this. 它可能会这样做。

I also wasn't able to get it to work using the same output folder for Groovy and Java, but it does seem to work if you use different output folders. 对于Groovy和Java,我也无法使用相同的输出文件夹来使它工作,但是如果您使用不同的输出文件夹,它似乎确实可以工作。

Here's what I did. 这就是我所做的。 Via the project properties dialog: 通过项目属性对话框:

  1. Under Groovy Project Properties deselect the checkbox Disable Groovy Compiler Generating Class Files (Note to plugin devs: please use positive wording on checkbox labels. "Generate Groovy class files" would be much easier to understand than "disabling the disable option".) 在“ Groovy项目属性”下,取消选中“ 禁用Groovy编译器生成类文件 ”复选框(插件开发者注意:请在复选框标签上使用正词。“生成Groovy类文件”比“禁用禁用选项”更容易理解。)
  2. In Groovy compiler output location enter web-app/WEB-INF/groovy-classes Groovy编译器的输出位置中,输入web-app/WEB-INF/groovy-classes
  3. Under Java Build Path -> Libraries add the Groovy classes folder. Java Build Path-> Libraries下,添加Groovy classes文件夹。

You should now be able to use the compiled Groovy classes from Java. 您现在应该可以使用Java编译的Groovy类。

I think this is more of a hack than a solution, but it's doing the job for me for now. 我认为这更多的是破解而不是解决方案,但现在它对我来说确实有效。

Please upgrade to V2 of the groovy-eclipse plugin as this problem has been fixed. 由于此问题已解决,请升级到groovy-eclipse插件的V2。 V2 of the plugin does not build stubs and does not have any problem with circular dependencies between Java and Groovy. 插件的V2不会构建存根,并且Java与Groovy之间的循环依赖关系也没有任何问题。 The plugin now ships with a feature patch that patches the JDT compiler so that it can natively work with groovy files. 现在,该插件附带了一个功能补丁,该补丁修补了JDT编译器,以便它可以自然地处理groovy文件。

It seems I'm a little late to this party but I was running into the same problem. 看来我参加这个聚会有点晚了,但是我遇到了同样的问题。 I was able to resolve referencing a Groovy domain class from a Java class by converting the project to a Groovy Project. 通过将项目转换为Groovy项目,我能够解决从Java类引用Groovy域类的问题。 Instructions are as follows: 说明如下:

  1. Right click on the project in the Project Explorer 右键单击项目浏览器中的项目
  2. Select Convert to a Groovy Project from the Configure menu 从“ 配置”菜单中选择“ 转换为Groovy项目

My Environment is as follows: 我的环境如下:

  • Eclipse Kepler Service Release 1, Build id: 20130919-0819 Eclipse Kepler Service Release 1,内部版本号:20130919-0819
  • Groovy-Eclipse plugin 2.9.0.xx-20130828-1400-e43-RELEASE Groovy-Eclipse插件2.9.0.xx-20130828-1400-e43-RELEASE
  • Grails IDE 3.4.0.201310051518-RELEASE (I don't think this was necessary) Grails IDE 3.4.0.201310051518-RELEASE(我认为这没有必要)

Finally, I did not mess with any "Disable Groovy Compiler Generating Class Files" option. 最后,我惹任何“禁止Groovy编译生成类文件”选项。

While I haven't tried it, I believe you'll need to add the Domain folder as a src folder in your Java build path. 尽管我还没有尝试过,但是我相信您需要在Java构建路径中将Domain文件夹添加为src文件夹。

Right-click on project select Properties. 右键单击项目,选择“属性”。 Click on Java Build Path, then the Source tab. 单击“ Java构建路径”,然后单击“源”选项卡。 Click add-folder and make sure your project/grails-app/domain is in the path. 单击添加文件夹,并确保您的project / grails-app / domain在路径中。

If that doesn't work, then you're right, I'm not sure what the issue is. 如果那行不通,那么您是对的,我不确定是什么问题。

FYI, Spring-Source being such an Eclipse sponsor, I'd look forward to a much improved Eclipse plugin in the near future. 仅供参考,Spring-Source就是这样的Eclipse赞助商,我希望在不久的将来有一个经过改进的Eclipse插件。 But you're right. 但是你是对的。 For now, it's annoying. 现在,这很烦人。

I am not sure if referencing files or folders is the good way. 我不确定引用文件或文件夹是否是个好方法。 You should use modularization. 您应该使用模块化。 So you domain classes will be stored in separate "project". 因此,您的域类将存储在单独的“项目”中。 It is possible to do it by creating plugins. 可以通过创建插件来做到这一点。 You can find great explanation and source code here . 您可以在此处找到很好的解释和源代码。

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

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