views:

863

answers:

3

I have a situation where some of my groovy code references my java files, but I also have different java files that reference the same groovy code. When trying to compile in maven, I either need to compile the groovy before or after the java, and that won't really work since the groovy code depends on some java files, and different java files depend on the groovy code. Is there a way to handle this sort of dependency?

A: 

You can partition your code in layers and have lower layers call upper layers but never vice versa. For example, in a Web app you can have a view layer, a service layer, and a persistence layer. The view layer calls the service layer and the service layer calls the persistence layer, but the persistence layer will never call the service layer or the view layer. If you want groovy/java code to exist in the same layer then make sure one calls the other but they don't both call each other. The bottom line is that you should avoid bi-directional dependencies.

rich
-1 There is joint Groovy-Java compiler.
Robert Munteanu
That's true but having bi-directional dependencies can lead to unnecessarily complex and less maintainable code. In the context of this question I agree that using GMaven is the right answer but I would still refactor the code to eliminate, or at least reduce the bi-directional relationships.
rich
+4  A: 

Yes, just use GMaven. Since it's a joint compiler, it automatically manages your java to groovy and groovy to java dependencies.

Briefly, you will need to:

  • include the gmaven-plugin in your pom.xml;
  • keep your groovy classes under src/main/groovy or src/test/groovy;
  • bind the gmaven plugin to the relevant lifecycle phases.

For more details see building groovy projects.

Robert Munteanu
+3  A: 

You should be able to compile your code by adding the gmaven plugin to your maven pom.xml. It generates Java stubs of your groovy code to deal with the type of inter-language referencing you're dealing with. I use it quite a bit and it works very well.

hohonuuli