You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@buildr.apache.org by Martin Grotzke <ma...@javakaffee.de> on 2009/06/12 01:25:16 UTC

How to generate sources for/before compile

Hi,

I'm just wondering what's the best way to generate sources before the
compile task is executed, whereas the generated sources shall also be
compiled.

The additional sources are e.g. generated with an ant task:

task :generatesources do
  ant('generate') do |ant|
    ant.taskdef :name=>'myTask', :classname=>'some.AntTask', :classpath=>'path/to/jar.jar'
    ant.myTask :targetDir=>_('target/generated-sources'), :someProp=>'some/path'
  end
end

Thanx for your help,
cheers,
Martin



Re: How to generate sources for/before compile

Posted by Alex Boisvert <bo...@intalio.com>.
On Fri, Jun 12, 2009 at 11:05 AM, Alex Boisvert <bo...@intalio.com>wrote:

> I'm having issues creating the page in the wiki right now (confluence fails
> on me).  I'll create it when possible.


http://cwiki.apache.org/confluence/display/BUILDR/How+to+generate+sources+before+compilation

Re: How to generate sources for/before compile

Posted by Alex Boisvert <bo...@intalio.com>.
On Fri, Jun 12, 2009 at 11:44 AM, Assaf Arkin <ar...@intalio.com> wrote:

> On Fri, Jun 12, 2009 at 11:05 AM, Alex Boisvert <boisvert@intalio.com
> >wrote:
>
> > On Fri, Jun 12, 2009 at 6:12 AM, Martin Grotzke <
> > martin.grotzke@javakaffee.de> wrote:
> >
> > > I needed to wrap the ant project with the generate = task ..., as
> > > otherwise (with just generate = ant('generate') do |ant| ...) the ant
> > > project was directly executed (AFAICS with the loading of the
> buildfile)
> > > and buildr complained with:
> > >  Buildr aborted!
> > >  Don't know how to build task 'Antwrap::AntProject[generate]'
> >
> >
> > Ooops. I forgot ant() didn't yeld a task; it executes right away.
> >
> > Do you think the stuff above is ok, so that we could push this to the
> > > buildr howtos (wiki)? Or is there still room for improvement?
> >
> >
> > I think the main improvement would be in wiring dependencies so the task
> > only executes when needed.
> >
> > Here's a revised version,
> >
> > define "generate-sources-example" do
> >
> >  # whatever you use to generate your sources
> >   sources = Dir[_("src/main/yak") + "/**/*.yk"]
>
>
> sources = FileList[_("src/main/yak/**/*.yk")]
>
> FileList, provided by Rake, is a better Dir. Also there's generally no need
> to decompose and recompose paths.
>
>
> >
> >  generate = file(path_to(:target, "generated-source") => sources) do
> |dir|
>
>
> likewise, file(_("target/generated-source")=>sources)


Thanks for the tips.  I've updated the How To page.

alex

Re: How to generate sources for/before compile

Posted by Assaf Arkin <ar...@intalio.com>.
On Fri, Jun 12, 2009 at 11:05 AM, Alex Boisvert <bo...@intalio.com>wrote:

> On Fri, Jun 12, 2009 at 6:12 AM, Martin Grotzke <
> martin.grotzke@javakaffee.de> wrote:
>
> > I needed to wrap the ant project with the generate = task ..., as
> > otherwise (with just generate = ant('generate') do |ant| ...) the ant
> > project was directly executed (AFAICS with the loading of the buildfile)
> > and buildr complained with:
> >  Buildr aborted!
> >  Don't know how to build task 'Antwrap::AntProject[generate]'
>
>
> Ooops. I forgot ant() didn't yeld a task; it executes right away.
>
> Do you think the stuff above is ok, so that we could push this to the
> > buildr howtos (wiki)? Or is there still room for improvement?
>
>
> I think the main improvement would be in wiring dependencies so the task
> only executes when needed.
>
> Here's a revised version,
>
> define "generate-sources-example" do
>
>  # whatever you use to generate your sources
>   sources = Dir[_("src/main/yak") + "/**/*.yk"]


sources = FileList[_("src/main/yak/**/*.yk")]

FileList, provided by Rake, is a better Dir. Also there's generally no need
to decompose and recompose paths.


>
>  generate = file(path_to(:target, "generated-source") => sources) do |dir|


likewise, file(_("target/generated-source")=>sources)

Assaf


>
>    mkdir_p dir.to_s # ensure directory is created
>    # generate sources
>    # e.g.
>    # ant('generate') do |ant|
>    #  ...
>    # end
>  end
>
>  compile.from generate
> end
>
> I'm having issues creating the page in the wiki right now (confluence fails
> on me).  I'll create it when possible.
>
> alex
>

Re: How to generate sources for/before compile

Posted by Martin Grotzke <ma...@javakaffee.de>.
On Sat, 2009-06-13 at 17:12 -0700, Alex Boisvert wrote:
> On Sat, Jun 13, 2009 at 3:05 PM, Martin Grotzke <
> martin.grotzke@javakaffee.de> wrote:
> 
> > I have one question: What is "sources" for? From my understanding the
> > sources are generated _in_ (the body of) the generate task (a file task
> > in this case)?
> 
> 
> In most (all?) cases the generated sources are derived from something else.
> For example, you may have a code generator that uses templates or you wrote
> a grammar for a domain-specific language and you generate lexer/parser code
> from this grammar (e.g. JavaCC, ANTLR, ...) or maybe you're generating POJOs
> from a database schema, or some web services stubs from a WSDL definition,
> etc.
> 
> By wiring the sources as a dependency on the code generation task, if you
> change your template or your grammar then the generated source will be
> regenerated and recompiled automatically.
Great, this really helped!

And I read these days about rake tasks, which are essential for
understanding buildr IMHO, and helped me understanding your
explanation :)

From my point of view it would be good to have a kind of conceptual
documentation/explanation of how buildr works, additionally to the
currently existing documentation (which is good to read, but has lots of
text). This conceptual documentation should separate the different
concepts. E.g. to me (java user, coming from ant and maven, without any
ruby knowledge) it's not clear, what concept is part of ruby, rake,
buildr etc. To have a summary of the essentials would help to get more
out of buildr.

Cheers,
Martin


> 
> Hope this makes sense now...
> 
> alex

Re: How to generate sources for/before compile

Posted by Alex Boisvert <bo...@intalio.com>.
On Sat, Jun 13, 2009 at 3:05 PM, Martin Grotzke <
martin.grotzke@javakaffee.de> wrote:

> I have one question: What is "sources" for? From my understanding the
> sources are generated _in_ (the body of) the generate task (a file task
> in this case)?


In most (all?) cases the generated sources are derived from something else.
For example, you may have a code generator that uses templates or you wrote
a grammar for a domain-specific language and you generate lexer/parser code
from this grammar (e.g. JavaCC, ANTLR, ...) or maybe you're generating POJOs
from a database schema, or some web services stubs from a WSDL definition,
etc.

By wiring the sources as a dependency on the code generation task, if you
change your template or your grammar then the generated source will be
regenerated and recompiled automatically.

Hope this makes sense now...

alex

Re: How to generate sources for/before compile

Posted by Martin Grotzke <ma...@javakaffee.de>.
On Fri, 2009-06-12 at 11:05 -0700, Alex Boisvert wrote:
> On Fri, Jun 12, 2009 at 6:12 AM, Martin Grotzke <
> martin.grotzke@javakaffee.de> wrote:
> 
> > I needed to wrap the ant project with the generate = task ..., as
> > otherwise (with just generate = ant('generate') do |ant| ...) the ant
> > project was directly executed (AFAICS with the loading of the buildfile)
> > and buildr complained with:
> >  Buildr aborted!
> >  Don't know how to build task 'Antwrap::AntProject[generate]'
> 
> 
> Ooops. I forgot ant() didn't yeld a task; it executes right away.
> 
> Do you think the stuff above is ok, so that we could push this to the
> > buildr howtos (wiki)? Or is there still room for improvement?
> 
> 
> I think the main improvement would be in wiring dependencies so the task
> only executes when needed.
> 
> Here's a revised version,
> 
> define "generate-sources-example" do
> 
>   # whatever you use to generate your sources
>   sources = Dir[_("src/main/yak") + "/**/*.yk"]
> 
>   generate = file(path_to(:target, "generated-source") => sources) do |dir|
>     mkdir_p dir.to_s # ensure directory is created
>     # generate sources
>     # e.g.
>     # ant('generate') do |ant|
>     #  ...
>     # end
>   end
> 
>   compile.from generate
> end
Thanx for this!

I have one question: What is "sources" for? From my understanding the
sources are generated _in_ (the body of) the generate task (a file task
in this case)?

Cheers,
Martin


> 
> I'm having issues creating the page in the wiki right now (confluence fails
> on me).  I'll create it when possible.
> 
> alex

Re: How to generate sources for/before compile

Posted by Alex Boisvert <bo...@intalio.com>.
On Fri, Jun 12, 2009 at 6:12 AM, Martin Grotzke <
martin.grotzke@javakaffee.de> wrote:

> I needed to wrap the ant project with the generate = task ..., as
> otherwise (with just generate = ant('generate') do |ant| ...) the ant
> project was directly executed (AFAICS with the loading of the buildfile)
> and buildr complained with:
>  Buildr aborted!
>  Don't know how to build task 'Antwrap::AntProject[generate]'


Ooops. I forgot ant() didn't yeld a task; it executes right away.

Do you think the stuff above is ok, so that we could push this to the
> buildr howtos (wiki)? Or is there still room for improvement?


I think the main improvement would be in wiring dependencies so the task
only executes when needed.

Here's a revised version,

define "generate-sources-example" do

  # whatever you use to generate your sources
  sources = Dir[_("src/main/yak") + "/**/*.yk"]

  generate = file(path_to(:target, "generated-source") => sources) do |dir|
    mkdir_p dir.to_s # ensure directory is created
    # generate sources
    # e.g.
    # ant('generate') do |ant|
    #  ...
    # end
  end

  compile.from generate
end

I'm having issues creating the page in the wiki right now (confluence fails
on me).  I'll create it when possible.

alex

Re: How to generate sources for/before compile

Posted by Martin Grotzke <ma...@javakaffee.de>.
Hi Alex,

On Thu, 2009-06-11 at 17:45 -0700, Alex Boisvert wrote:
> On Thu, Jun 11, 2009 at 4:25 PM, Martin Grotzke <
> martin.grotzke@javakaffee.de> wrote:
> 
> >
> > I'm just wondering what's the best way to generate sources before the
> > compile task is executed, whereas the generated sources shall also be
> > compiled.
> >
> > The additional sources are e.g. generated with an ant task:
> >
> > task :generatesources do
> >  ant('generate') do |ant|
> >    ant.taskdef :name=>'myTask', :classname=>'some.AntTask',
> > :classpath=>'path/to/jar.jar'
> >    ant.myTask :targetDir=>_('target/generated-sources'),
> > :someProp=>'some/path'
> >  end
> > end
> >
> 
> I think it would be something along these lines:
> 
> sources = # whatever you use to generate your sources
> 
> generate = ant('generate' => sources) do |ant|
>  # ...
> end
> 
> compile.from file(path_to(:target, "generated-source") => generate)

Now I have the following:

generate = task :generatesources do
  ant('generate') do |ant|
    ant.taskdef :name=>'myTask', :classname=>'some.AntTask', :classpath=>'path/to/jar.jar'
    ant.myTask :targetDir=>_('target/generated-sources'), :someProp=>_('src/main/resources/')
  end
end
    
compile.with( LIBS ).from( file( _('target/generated-sources') => generate ) )

I needed to wrap the ant project with the generate = task ..., as
otherwise (with just generate = ant('generate') do |ant| ...) the ant
project was directly executed (AFAICS with the loading of the buildfile)
and buildr complained with:
  Buildr aborted!
  Don't know how to build task 'Antwrap::AntProject[generate]'

Do you think the stuff above is ok, so that we could push this to the
buildr howtos (wiki)? Or is there still room for improvement?

Cheers,
Martin


> 
> alex

Re: How to generate sources for/before compile

Posted by Alex Boisvert <bo...@intalio.com>.
On Thu, Jun 11, 2009 at 4:25 PM, Martin Grotzke <
martin.grotzke@javakaffee.de> wrote:

>
> I'm just wondering what's the best way to generate sources before the
> compile task is executed, whereas the generated sources shall also be
> compiled.
>
> The additional sources are e.g. generated with an ant task:
>
> task :generatesources do
>  ant('generate') do |ant|
>    ant.taskdef :name=>'myTask', :classname=>'some.AntTask',
> :classpath=>'path/to/jar.jar'
>    ant.myTask :targetDir=>_('target/generated-sources'),
> :someProp=>'some/path'
>  end
> end
>

I think it would be something along these lines:

sources = # whatever you use to generate your sources

generate = ant('generate' => sources) do |ant|
 # ...
end

compile.from file(path_to(:target, "generated-source") => generate)

alex