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 17:00:13 UTC

How to define a task for all subprojects

Hi,

is it possible to define a task project wide, so that it's available to
each submodule?

In my case I want to define checkstyle once and have it available in
each submodule. It would be great if I could invoke
  buildr checkstyle
directly in the submodule.

Is this possible without creating an extension?

I'm already trying to create a Checkstyle extension that uses ant
internally (ant('checkstyle') do |ant| ant.taskdef ...), but with this
buildr complains about "undefined method `ant'"...

Thanx for your help,
cheers,
Martin


Re: How to define a task for all subprojects

Posted by Martin Grotzke <ma...@javakaffee.de>.
On Sun, 2009-06-14 at 15:25 -0700, Alex Boisvert wrote:
> On Sun, Jun 14, 2009 at 2:52 PM, Martin Grotzke <
> martin.grotzke@javakaffee.de> wrote:
> 
> > Just what I wonder: why is this Buildr::ant()? Isn't ant() an instance
> > method of the Buildr::Ant module (according to [1])?
> 
> 
> Yes, it is... and that's why you can't call it directly ;)
> 
> In Ruby, modules have no instances.  Instance methods are available only in
> classes that mix-in the module.
> 
> The reason why the ant() method is available in Buildr is because
> 
> 1) the Buildr module includes the Ant module (see buildr/java/ant.rb)
> 
> module Buildr
>   ...
>   include Ant
> 
> 
> and 2) the Buildr module extends itself so it gets all the instance methods
> of the modules it includes (see buildr/buildr.rb)
> 
> # Methods defined in Buildr are both instance methods (e.g. when included in
> Project)
> # and class methods when invoked like Buildr.artifacts().
> module Buildr ; extend self ; end
> 
> Nifty, eh?   (I still do get confused by all this mixin stuff myself)
Ok, thanx for this great explanation!

IMHO this should also go into some understanding-buildr documentation :)

Cheers,
Martin


> 
> alex

Re: How to define a task for all subprojects

Posted by Alex Boisvert <bo...@intalio.com>.
On Sun, Jun 14, 2009 at 2:52 PM, Martin Grotzke <
martin.grotzke@javakaffee.de> wrote:

> Just what I wonder: why is this Buildr::ant()? Isn't ant() an instance
> method of the Buildr::Ant module (according to [1])?


Yes, it is... and that's why you can't call it directly ;)

In Ruby, modules have no instances.  Instance methods are available only in
classes that mix-in the module.

The reason why the ant() method is available in Buildr is because

1) the Buildr module includes the Ant module (see buildr/java/ant.rb)

module Buildr
  ...
  include Ant


and 2) the Buildr module extends itself so it gets all the instance methods
of the modules it includes (see buildr/buildr.rb)

# Methods defined in Buildr are both instance methods (e.g. when included in
Project)
# and class methods when invoked like Buildr.artifacts().
module Buildr ; extend self ; end

Nifty, eh?   (I still do get confused by all this mixin stuff myself)

alex

Re: How to define a task for all subprojects

Posted by Martin Grotzke <ma...@javakaffee.de>.
Great, this works!

Just what I wonder: why is this Buildr::ant()? Isn't ant() an instance
method of the Buildr::Ant module (according to [1])?

Probably this is just basics and I should start learning this? (you
probably already know that I have no knowledge of ruby at all) ;)

Thanx && cheers,
Martin


[1] http://buildr.apache.org/rdoc/

On Sat, 2009-06-13 at 17:03 -0700, Alex Boisvert wrote:
> Try Buildr::ant() instead of just ant()
> 
> alex
> 
> 
> On Sat, Jun 13, 2009 at 3:22 PM, Martin Grotzke <
> martin.grotzke@javakaffee.de> wrote:
> 
> > Hi Daniel,
> >
> > On Fri, 2009-06-12 at 10:36 -0500, Daniel Spiewak wrote:
> > > In order to define a project-aware task which affects more than one
> > project,
> > > you will need to use the extension API.
> > Ok, so I'll try that road.
> >
> > Can you help me with the issue regarding ant('foo')? As I wrote in my
> > email:
> > I'm already trying to create a Checkstyle extension that uses ant
> > internally (ant('checkstyle') do |ant| ant.taskdef ...), but with this
> > buildr complains about "undefined method `ant'"...
> >
> > I asume this is because ant() is an instance method on Buildr, and my
> > extension does not have such an instance.
> >
> > My extension basically looks like
> >
> > ====================================================================
> > require 'buildr/java'
> > require 'buildr/java/ant'
> >
> > module CheckstyleAnt include Buildr::Extension
> >
> >  first_time do
> >    # Define task not specific to any projet.
> >    desc 'Check code conventions in current project'
> >    Project.local_task('checkstyle')
> >  end
> >
> >  before_define do |project|
> >    # Define the loc task for this particular project.
> >    task :checkstyle do |task|
> >      ant('checkstyle') do |ant|
> >          # define checkstyle task and target / execute
> >      end
> >
> >    end
> >  end
> >
> >  after_define do |project|
> >  end
> >
> >  def checkstyle()
> >    task('checkstyle')
> >  end
> >
> > end
> >
> > class Buildr::Project
> >  include CheckstyleAnt
> > end
> > ====================================================================
> >
> > And I just require this extension in my buildfile...
> >
> > What can I do to have ant() available in the extension?
> >
> > Thanx in advance,
> > cheers,
> > Martin
> >
> >
> >
> > >   Effectively, you will be triggering
> > > the re-definition of that same task every time a new project is created.
> > > You can embed the extension within your buildfile, so it really isn't a
> > real
> > > hardship.  To eliminate the project-name prefix (e.g. `buildr
> > > myproject:checkstyle` as opposed to `buildr checkstyle` in the
> > appropriate
> > > directory), you must use the following invocation:
> > >
> > >   Project.local_task :checkstyle
> > >
> > > That should do the trick!
> > >
> > > Daniel
> > >
> > > On Fri, Jun 12, 2009 at 10:00 AM, Martin Grotzke <
> > > martin.grotzke@javakaffee.de> wrote:
> > >
> > > > Hi,
> > > >
> > > > is it possible to define a task project wide, so that it's available to
> > > > each submodule?
> > > >
> > > > In my case I want to define checkstyle once and have it available in
> > > > each submodule. It would be great if I could invoke
> > > >  buildr checkstyle
> > > > directly in the submodule.
> > > >
> > > > Is this possible without creating an extension?
> > > >
> > > > I'm already trying to create a Checkstyle extension that uses ant
> > > > internally (ant('checkstyle') do |ant| ant.taskdef ...), but with this
> > > > buildr complains about "undefined method `ant'"...
> > > >
> > > > Thanx for your help,
> > > > cheers,
> > > > Martin
> > > >
> > > >
> >

Re: How to define a task for all subprojects

Posted by Alex Boisvert <bo...@intalio.com>.
Try Buildr::ant() instead of just ant()

alex


On Sat, Jun 13, 2009 at 3:22 PM, Martin Grotzke <
martin.grotzke@javakaffee.de> wrote:

> Hi Daniel,
>
> On Fri, 2009-06-12 at 10:36 -0500, Daniel Spiewak wrote:
> > In order to define a project-aware task which affects more than one
> project,
> > you will need to use the extension API.
> Ok, so I'll try that road.
>
> Can you help me with the issue regarding ant('foo')? As I wrote in my
> email:
> I'm already trying to create a Checkstyle extension that uses ant
> internally (ant('checkstyle') do |ant| ant.taskdef ...), but with this
> buildr complains about "undefined method `ant'"...
>
> I asume this is because ant() is an instance method on Buildr, and my
> extension does not have such an instance.
>
> My extension basically looks like
>
> ====================================================================
> require 'buildr/java'
> require 'buildr/java/ant'
>
> module CheckstyleAnt include Buildr::Extension
>
>  first_time do
>    # Define task not specific to any projet.
>    desc 'Check code conventions in current project'
>    Project.local_task('checkstyle')
>  end
>
>  before_define do |project|
>    # Define the loc task for this particular project.
>    task :checkstyle do |task|
>      ant('checkstyle') do |ant|
>          # define checkstyle task and target / execute
>      end
>
>    end
>  end
>
>  after_define do |project|
>  end
>
>  def checkstyle()
>    task('checkstyle')
>  end
>
> end
>
> class Buildr::Project
>  include CheckstyleAnt
> end
> ====================================================================
>
> And I just require this extension in my buildfile...
>
> What can I do to have ant() available in the extension?
>
> Thanx in advance,
> cheers,
> Martin
>
>
>
> >   Effectively, you will be triggering
> > the re-definition of that same task every time a new project is created.
> > You can embed the extension within your buildfile, so it really isn't a
> real
> > hardship.  To eliminate the project-name prefix (e.g. `buildr
> > myproject:checkstyle` as opposed to `buildr checkstyle` in the
> appropriate
> > directory), you must use the following invocation:
> >
> >   Project.local_task :checkstyle
> >
> > That should do the trick!
> >
> > Daniel
> >
> > On Fri, Jun 12, 2009 at 10:00 AM, Martin Grotzke <
> > martin.grotzke@javakaffee.de> wrote:
> >
> > > Hi,
> > >
> > > is it possible to define a task project wide, so that it's available to
> > > each submodule?
> > >
> > > In my case I want to define checkstyle once and have it available in
> > > each submodule. It would be great if I could invoke
> > >  buildr checkstyle
> > > directly in the submodule.
> > >
> > > Is this possible without creating an extension?
> > >
> > > I'm already trying to create a Checkstyle extension that uses ant
> > > internally (ant('checkstyle') do |ant| ant.taskdef ...), but with this
> > > buildr complains about "undefined method `ant'"...
> > >
> > > Thanx for your help,
> > > cheers,
> > > Martin
> > >
> > >
>

Re: How to define a task for all subprojects

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

On Fri, 2009-06-12 at 10:36 -0500, Daniel Spiewak wrote:
> In order to define a project-aware task which affects more than one project,
> you will need to use the extension API.
Ok, so I'll try that road.

Can you help me with the issue regarding ant('foo')? As I wrote in my
email:
I'm already trying to create a Checkstyle extension that uses ant
internally (ant('checkstyle') do |ant| ant.taskdef ...), but with this
buildr complains about "undefined method `ant'"...

I asume this is because ant() is an instance method on Buildr, and my
extension does not have such an instance.

My extension basically looks like

====================================================================
require 'buildr/java'
require 'buildr/java/ant'

module CheckstyleAnt include Buildr::Extension

  first_time do
    # Define task not specific to any projet.
    desc 'Check code conventions in current project'
    Project.local_task('checkstyle')
  end

  before_define do |project|
    # Define the loc task for this particular project.
    task :checkstyle do |task|
      ant('checkstyle') do |ant|
          # define checkstyle task and target / execute
      end
      
    end
  end

  after_define do |project|
  end

  def checkstyle()
    task('checkstyle')
  end

end

class Buildr::Project
  include CheckstyleAnt
end
====================================================================

And I just require this extension in my buildfile...

What can I do to have ant() available in the extension?

Thanx in advance,
cheers,
Martin



>   Effectively, you will be triggering
> the re-definition of that same task every time a new project is created.
> You can embed the extension within your buildfile, so it really isn't a real
> hardship.  To eliminate the project-name prefix (e.g. `buildr
> myproject:checkstyle` as opposed to `buildr checkstyle` in the appropriate
> directory), you must use the following invocation:
> 
>   Project.local_task :checkstyle
> 
> That should do the trick!
> 
> Daniel
> 
> On Fri, Jun 12, 2009 at 10:00 AM, Martin Grotzke <
> martin.grotzke@javakaffee.de> wrote:
> 
> > Hi,
> >
> > is it possible to define a task project wide, so that it's available to
> > each submodule?
> >
> > In my case I want to define checkstyle once and have it available in
> > each submodule. It would be great if I could invoke
> >  buildr checkstyle
> > directly in the submodule.
> >
> > Is this possible without creating an extension?
> >
> > I'm already trying to create a Checkstyle extension that uses ant
> > internally (ant('checkstyle') do |ant| ant.taskdef ...), but with this
> > buildr complains about "undefined method `ant'"...
> >
> > Thanx for your help,
> > cheers,
> > Martin
> >
> >

Re: How to define a task for all subprojects

Posted by Daniel Spiewak <dj...@gmail.com>.
In order to define a project-aware task which affects more than one project,
you will need to use the extension API.  Effectively, you will be triggering
the re-definition of that same task every time a new project is created.
You can embed the extension within your buildfile, so it really isn't a real
hardship.  To eliminate the project-name prefix (e.g. `buildr
myproject:checkstyle` as opposed to `buildr checkstyle` in the appropriate
directory), you must use the following invocation:

  Project.local_task :checkstyle

That should do the trick!

Daniel

On Fri, Jun 12, 2009 at 10:00 AM, Martin Grotzke <
martin.grotzke@javakaffee.de> wrote:

> Hi,
>
> is it possible to define a task project wide, so that it's available to
> each submodule?
>
> In my case I want to define checkstyle once and have it available in
> each submodule. It would be great if I could invoke
>  buildr checkstyle
> directly in the submodule.
>
> Is this possible without creating an extension?
>
> I'm already trying to create a Checkstyle extension that uses ant
> internally (ant('checkstyle') do |ant| ant.taskdef ...), but with this
> buildr complains about "undefined method `ant'"...
>
> Thanx for your help,
> cheers,
> Martin
>
>