简体   繁体   中英

Reference Groovy domain class from Java class using Eclipse?

How can I reference a Groovy domain class from Java class using Eclipse?

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 . This works for grails run-app (command line) but not Eclipse. Eclipse can't resolve the Groovy domain class.

I'm running Eclipse 3.4.1 with the latest Groovy and Grails Eclipse plugins:

  • Grails Eclipse Feature 0.1.0 20081120_2330
  • GroovyFeature 1.5.7.20081120_2330

I've tried setting the Eclipse default output folder to the same as Groovy compiler output location. I've also tried both enabling and disabling the “Disable Groovy Compiler Generating Class Files” setting. 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.

Did you try the Groovy Plugin ? 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.

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".)
  2. In Groovy compiler output location enter web-app/WEB-INF/groovy-classes
  3. Under Java Build Path -> Libraries add the Groovy classes folder.

You should now be able to use the compiled Groovy classes from Java.

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. V2 of the plugin does not build stubs and does not have any problem with circular dependencies between Java and Groovy. The plugin now ships with a feature patch that patches the JDT compiler so that it can natively work with groovy files.

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. 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

My Environment is as follows:

  • Eclipse Kepler Service Release 1, Build id: 20130919-0819
  • Groovy-Eclipse plugin 2.9.0.xx-20130828-1400-e43-RELEASE
  • Grails IDE 3.4.0.201310051518-RELEASE (I don't think this was necessary)

Finally, I did not mess with any "Disable Groovy Compiler Generating Class Files" option.

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.

Right-click on project select Properties. Click on Java Build Path, then the Source tab. Click add-folder and make sure your project/grails-app/domain is in the path.

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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