Question

I'm trying to use a code generator inside a buildr-based java project. I would like to...

  • call the generator
  • compile the generated classes, package them
  • eclipse to see the generated stuff (i.e. have the .classpath contain those sources)

I've spend lots of time googling around for a complete example, but to no avail. the example here: https://cwiki.apache.org/confluence/display/BUILDR/How+to+generate+sources+before+compilation

gives be plenty of errors. this is what I've tried:

 define 'generator' do
    compile.with ALL_COMMON_MODULES
  end
  define 'extras' do
    p 'calling generator..'
    Java.classpath << ALL_COMMON_MODULES
    Java.classpath << projects('generator')
    Java.org.foo.generator.Main.main(['../modules/daos', 'target/generated-sources'])
    sources = FileList[_("src/main/jeannie/*.*")]
    generate = file(_("target/generated-sources") => sources).to_s do |dir|
      puts 'generating...'
      mkdir_p dir.to_s # ensure directory is created
    end
    compile.from generate
  end

gives me an error like this:

RuntimeError: Circular dependency detected: TOP ...

so I'm obviously doing something very very wrong. I'd be very glad to see a working example or project that uses code generation.

Was it helpful?

Solution

I finally got it working with help from the buildr mailinglist. for anyone interested: The example mentioned here contains a problem. this:

compile.from generate

should be:

compile.from generate.to_s

Now it works beautifully! buildr also automatically extends the .classpath for eclipse (and idea, in case you use that) if 'compile.from' points to another location.

OTHER TIPS

You cause the circular dependency by calling the parent project in your 'extras' project. At that line: Java.classpath << projects('generator')

Maybe you should put everything on the same level, which is also what the example shows. In this scenario your "generate" reference wouldn't be available either as it is in the 'extras' scope.

Something like this (untested):

  define "generator" do
    generate = file(_("target/generated-sources") => sources).to_s do |dir|
      p 'calling generator..'
      Java.classpath << ALL_COMMON_MODULES
      Java.classpath << projects('generator')
      Java.org.foo.generator.Main.main(['../modules/daos', 'target/generated-sources'])
      sources = FileList[_("src/main/jeannie/*.*")]
      puts 'generating...'
      mkdir_p dir.to_s # ensure directory is created
    end
    compile.with ALL
    compile.from generate
  end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top