views:

1111

answers:

5

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.

+1  A: 

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.

mugafuga
A: 

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.

Bill James
A: 

Did you try the Groovy Plugin? It may do this.

MetroidFan2002
A: 

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.

Tom Clift
A: 

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.

Andrew Eisenberg