0

I need to generate some Java sources that will then be compiled by Maven. The problem is the legacy code that generates those sources is written in Java. The solution (work-around) used was to have:

  • project A with the code that generates Java sources
  • project B that depends on project A and calls antrun in Maven to execute the classes in project A

What happens when you build project B is that Maven will:

  • compile project A (code that generates sources) and do whatever else the pom.xml of project A tells it to do.
  • antrun those classes (as requested by the pom.xml of project B) - thus sources will get generated and added to project B
  • compile project B sources

This because Antrun requires the classes in project A to already be compiled when it's executed. This is however an ugly solution, and project A and B should actually be just one project. I know I should use:

<phase>generate-sources</phase>

and I saw an example with Groovy (http://blog.retep.org/2009/11/07/using-groovy-to-generate-java-sources-in-maven/), but I would like to know if there's any simpler way to do this while having everything in one project and not having to change the code-generation from Java to groovy or something else.

Thanks.

ccc
  • 2,129
  • 4
  • 24
  • 31

1 Answers1

2

I use Groovy for situations like that, but if you don't want to, then having two separate projects is the correct thing to do.

And I would not compile one project from the other, I would just leave them as separate projects. Usually the code generator project won't change as often as the "real" project, so there's no need to build it all the time.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 1
    +1 - I also remember [this](http://stackoverflow.com/questions/6691723/m2e-generated-code-with-exec-maven-plugin) question of you which addresses the problem and offers a working solution. – FrVaBe Dec 11 '12 at 16:24
  • @FrVaBe true, only I have long since switched to IntelliJ and no longer have such problems :-) – Sean Patrick Floyd Dec 11 '12 at 16:29
  • I still like eclipse and I am thankful for the community effort to enhance it - but the maven integration (or is it the eclipse Builder concept?) is really 'strange'. – FrVaBe Dec 11 '12 at 16:41
  • @FrVaBe I'm not saying Eclipse is bad, not by far. But once you get used to it, IntelliJ idea is superior in many ways. However, it took me almost a year to get used to it. – Sean Patrick Floyd Dec 12 '12 at 10:52