Although the documentation can be better, this mailing list is very active.
Thanks!
The code below is for an extension that copies jars from local maven
repository to lib folder and for another extension that makes eclipse
classpath file independent of m2eclipse and makes it portable using relative
paths.
-----------tasks/copy_jars_to_lib.rake--------------
module Buildr
module CopyJarsToLib
include Extension
# called for each project
# This method copies jars from local maven repository to lib folder but does
not update the IDEA .iml files
after_define(:copy_jars_to_lib => :compile) do |project|
to_path = project._('lib')
mkpath to_path if not File.directory?(to_path)
project.compile.dependencies.each do |artifact| # todo: remove unused JARs
repo_pkg = artifact.to_s # JARs for dependent projects are copied in case we
want to run this eclipse project without their sources
if not artifact.instance_of?(String) # jars not downloaded from a maven repo
are assumed to have no javadoc
repo_doc = artifact.javadoc_artifact.to_s # if we have downloaded javadoc
(buildr artifacts:javadoc), then it copies them to lib
lib_doc = File.join(to_path,File.basename(repo_doc))
cp repo_doc, lib_doc if File.exists?(repo_doc) and (not
File.exists?(lib_doc) or File.ctime(lib_doc) < File.ctime(repo_doc))
end
lib_pkg = File.join(to_path,File.basename(repo_pkg))
#puts "copying for project #{project.name}{"
#puts repo_pkg
#puts "to #{lib_pkg}"
#puts'}'
cp repo_pkg, lib_pkg if File.exists?(repo_pkg) and (not
File.exists?(lib_pkg) or File.ctime(lib_pkg) < File.ctime(repo_pkg))
end
end
end
class Project
include CopyJarsToLib
end
end
-----------tasks/fix_eclipse_files.rake--------------
module Buildr
module FixEclipseFiles
include Extension
# called for each project
# makes eclipse classpath file independent of m2eclipse and makes it
portable using relative paths
after_define(:fix_eclipse_files => :eclipse) do |project|
to_path = project._('lib')
eclipsefile = project._('.classpath')
if File.exists?(eclipsefile)
puts "modifying #{eclipsefile}..."
File.open(eclipsefile) do |config_file|
config = REXML::Document.new(config_file)
config.elements.each("classpath/classpathentry") { |element|
if element.attributes["kind"]=="var"
element.attributes["kind"] = "lib"
# removes path from filepath (deletes all character until the last occurence
of file separator)
path = element.attributes["path"]; path.slice!(0..path.rindex('/'));
element.attributes["path"] = "lib/#{path}"
element.delete_attribute("sourcepath")
docpath = element.attributes["javadocpath"]
docpath.slice!(0..docpath.rindex('/')) # to_path is used as eclipse does not
accept relative paths, it would need user to set a variable
p = Pathname.new(to_path).parent
docpath = "jar:platform:/resource/#{project.eclipse.name}/lib/#{docpath}!/"
element.add_element('attributes').add_element 'attribute',
{'name'=>'javadoc_location', 'value'=>docpath}
element.delete_attribute("javadocpath")
end
}
File.open('tmp', 'w') do |result|
REXML::Formatters::Pretty.new.write(config, result); end
end
mv 'tmp', eclipsefile
end
end
end
class Project
include FixEclipseFiles
end
end
On Sat, Oct 9, 2010 at 4:50 PM, Alex Boisvert <[email protected]>wrote:
> Are you sure you're including the Extension module and including your
> extension into the Buildr::Project class?
>
> Also, I forgot that to be required automatically, extensions must have the
> .rake extension (not .rb) in the tasks/ subdirectory.
>
> If you can't get things working, please send a mockup of your code so I can
> better help you.
>
> alex
>
>
> On Sat, Oct 9, 2010 at 2:18 AM, Nikos Maris <[email protected]> wrote:
>
> > I have done that and it doesn't get executed at all. My initial code was:
> >
> > projects.each do |proj|
> > proj.compile.enhance do copy_jars_to_lib proj; end
> > task :eclipse do fix_eclipse proj; end
> > end;
> >
> >
> > thank you for your time,
> >
> > Nikos
> >
> >
> > On Fri, Oct 8, 2010 at 7:34 PM, Alex Boisvert <[email protected]
> > >wrote:
> >
> > > You mean it doesn't get executed at all? Or it doesn't work? You can
> > put
> > > a trace in your after_define block to check if it gets executed.
> > >
> > > Also, for your Eclipse tweak you should depend on the :eclipse
> extension
> > > being run first:
> > >
> > > after_define(:fix_eclipse => :eclipse) do |project|
> > > ...
> > > puts "Fix eclipse called"
> > > end
> > >
> > > alex
> > >
> > >
> > > On Fri, Oct 8, 2010 at 8:34 AM, Nikos Maris <[email protected]>
> wrote:
> > >
> > > > Now at my buildfile I just require 'tasks/copy_jars_to_lib.rb'
> > > > and 'tasks/fix_eclipse_files.rb''. Although the compile extension is
> > > > executed, the eclipse execution doesn't.
> > > >
> > > > On Fri, Oct 8, 2010 at 5:08 PM, Alex Boisvert <
> [email protected]
> > > > >wrote:
> > > >
> > > > > Simplest way to add extensions is to drop them into the "tasks"
> > > > directory.
> > > > > That way, they are required automatically. (This is the
> convention
> > > > over
> > > > > configuration part).
> > > > >
> > > > > Otherwise, you can simply require the extension that the top of
> your
> > > > > buildfile, whether it's a gem or a file on your filesystem (in
> source
> > > > > control or not).
> > > > >
> > > > > To create an extension, simply create a module, include the
> Extension
> > > > > module
> > > > > and mix it in the Project class:
> > > > >
> > > > > # Place this file under tasks directory (e.g.
> tasks/my_extension.rb)
> > > > > module Buildr
> > > > > module CopyJars
> > > > > include Extension
> > > > >
> > > > > # called for each project
> > > > > after_define(:copy_jars => :compile) do |project|
> > > > > copy_jars_to_lib(project)
> > > > > end
> > > > > end
> > > > >
> > > > > class Project
> > > > > include CopyJars
> > > > > end
> > > > > end
> > > > >
> > > > > And you can do similarly for your Eclipse task.
> > > > >
> > > > > Is that what you had in mind for your refactoring?
> > > > >
> > > > > alex
> > > > >
> > > > > On Fri, Oct 8, 2010 at 2:33 AM, Nikos Maris <[email protected]>
> > > wrote:
> > > > >
> > > > > > Newbie question: I would like to extend the default behavior of
> the
> > > > > compile
> > > > > > task.
> > > > > >
> > > > > > I currently enhance the compile task of all sub-projects with a
> > > > function
> > > > > > that takes a project as an argument in order to access methods
> like
> > > _()
> > > > > and
> > > > > > fields like compile.dependencies :
> > > > > > projects.each do |proj|
> > > > > > proj.compile.enhance do copy_jars_to_lib proj; end
> > > > > > end
> > > > > >
> > > > > > I would like to refactor this code and be able to commit that
> (e.g.
> > > > > adding
> > > > > > an extension at ~/.buildr is not an option).
> > > > > >
> > > > > > I would like also to refactor the following code:
> > > > > > projects.each do |proj|
> > > > > > task :eclipse do unboundVAR proj; end
> > > > > > end
> > > > > >
> > > > > > thank you for your time,
> > > > > > Nikos
> > > > > >
> > > > >
> > > >
> > >
> >
>