Extending Buildr
Organizing Tasks
A couple of things we learned while working on Buildr. Being able to write your own Rake tasks is a very powerful feature. But if you find yourself doing the same thing over and over, you might also want to consider functions. They give you a lot more power and easy abstractions.
For example, we use OpenJPA in several projects. It’s a very short task, but each time I have to go back to the OpenJPA documentation to figure out how to set the Ant MappingTool task, tell Ant how to define it. After the second time, you’re recognizing a pattern and it’s just easier to write a function that does all that for you.
Compare this:
To this:
I prefer the second. It’s easier to look at the Buildfile and understand what it does. It’s easier to maintain when you only have to look at the important information.
But just using functions is not always enough. You end up with a Buildfile containing a lot of code that clearly doesn’t belong there. For starters, I recommend putting it in the tasks
directory. Write it into a file with a .rake
extension and place that in the tasks
directory next to the Buildfile. Buildr will automatically pick it up and load it for you.
If you want to share these pre-canned definitions between projects, you have a few more options. You can share the tasks
directory using SVN externals, Git modules, or whichever cross-repository feature your source control system supports. Another mechanism with better version control is to package all these tasks, functions and modules into a Gem and require it from your Buildfile. You can run your own internal Gem server for that.
To summarize, there are several common ways to distribute extensions:
- Put them in the same place (e.g.
~/.buildr
) and require them from your
buildfile
- Put them directly in the project, typically under the
tasks
directory. - Put them in a shared code repository, and link to them from your project’s
tasks
directory - As Ruby gems and specify which gems are used in the settings file
You can also get creative and devise your own way to distribute extensions.
Sake is a good example of such initiative that lets you deploy Rake tasks on a system-wide basis.
Creating Extensions
The basic mechanism for extending projects in Buildr are Ruby modules. In fact, base features like compiling and testing are all developed in the form of modules, and then added to the core Project class.
A module defines instance methods that are then mixed into the project and become instance methods of the project. There are two general ways for extending projects. You can extend all projects by including the module in Project:
You can also extend a given project instance and only that instance by extending it with the module:
Some extensions require tighter integration with the project, specifically for setting up tasks and properties, or for configuring tasks based on the project definition. You can do that by adding callbacks to the process.
The easiest way to add callbacks is by incorporating the Extension module in your own extension, and using the various class methods to define callback behavior.
Method | Usage |
---|---|
first_time |
This block will be called once for any particular extension. You can use this to setup top-level and local tasks. |
before_define |
This block is called once for the project with the project instance, right before running the project definition. You can use this to add tasks and set properties that will be used in the project definition. |
after_define |
This block is called once for the project with the project instance, right after running the project definition. You can use this to do any post-processing that depends on the project definition. |
This example illustrates how to write a simple extension:
You may find interesting that this Extension API is used pervasively inside Buildr itself. Many of the standard tasks such as compile
, test
, package
are extensions to a very small core.
Starting with Buildr 1.4, it’s possible to define ordering between before_define
and after_define
code blocks in a way similar to Rake’s dependencies. For example, if you wanted to override project.test.compile.from
in after_define
, you could do so by in
Core extensions provide the following named callbacks: compile
, test
, build
, package
and check
.
Using Alternative Layouts
Buildr follows a common convention for project layouts: Java source files appear in src/main/java
and compile to target/classes
, resources are copied over from src/main/resources
and so forth. Not all projects follow this convention, so it’s now possible to specify an alternative project layout.
The default layout is available in Layout.default
, and all projects inherit it. You can set Layout.default
to your own layout, or define a project with a given layout (recommended) by setting the :layout
property. Projects inherit the layout from their parent projects. For example:
A layout is an object that implements the expand
method. The easiest way to define a custom layout is to create a new Layout
object and specify mapping between names used by Buildr and actual paths within the project. For example:
Partial expansion also works, so you can specify the above layout using:
If you need anything more complex, you can always subclass Layout
and add special handling in the expand
method, you’ll find one such example in the API documentation.
The built-in tasks expand lists of symbols into relative paths, using the following convention:
Path | Expands to |
---|---|
:source, :main, <lang/usage> |
Directory containing source files for a given language or usage, for example, :java , :resources , :webapp . |
:source, :test, <lang/usage> |
Directory containing test files for a given language or usage, for example, :java , :resources . |
:target, :generated |
Target directory for generated code (typically source code). |
:target, :main, <lang/usage> |
Target directory for compiled code, for example, :classes , :resources . |
:target, :test, <lang/usage> |
Target directory for compile test cases, for example, :classes , :resources . |
:reports, <framework/usage> |
Target directory for generated reports, for example, :junit , :coverage . |
All tasks are encouraged to use the same convention, and whenever possible, we recommend using the project’s path_to
method to expand a list of symbols into a path, or use the appropriate path when available. For example: