require "pathname" module Buildr # Global task "eclipse" generates artifacts for all projects. desc "Generate Eclipse artifacts for all projects" Project.local_task task("eclipse"=>"artifacts") Project.on_define do |project| eclipse = project.recursive_task("eclipse") project.enhance do |project| # We need paths relative to the top project's base directory. root_path = lambda { |p| f = lambda { |p| p.parent ? f[p.parent] : p.base_dir } ; f[p] }[project] # We're guessing the Rakefile is there, but keep in mind it might not exist # (most test cases don't create it). sources = [] sources << File.expand_path(Rake.application.rakefile, root_path) if Rake.application.rakefile # Only for projects that are Eclipse packagable. if project.packages.detect { |pkg| pkg.type =~ /(jar)|(war)|(rar)|(mar)|(aar)/ } eclipse.enhance [ file(project.path_to(".classpath")), file(project.path_to(".project")) ] # The only thing we need to look for is a change in the Rakefile. file(project.path_to(".classpath")=>sources) do |task| puts "Writing #{task.name}" if verbose # Find a path relative to the project's root directory. relative = lambda { |path| Pathname.new(path).relative_path_from(Pathname.new(project.path_to)).to_s } File.open(task.name, "w") do |file| xml = Builder::XmlMarkup.new(:target=>file, :indent=>2) xml.classpath do # Internal: projects that create artifacts we find on the classpath. # External: other artifacts, typically from the local repository. internal, external = project.compile.classpath.map(&:to_s). map { |path| projects.detect { |prj| prj.packages.detect { |pkg| pkg.to_s == path } } || path }. partition { |path| path.is_a?(Project) } # Collect all the classpaths, key is the kind, value is # string or array (or anything responding to to_a). { :src => project.compile.sources.map { |src| relative[src] }, :output => relative[project.path_to(:java_target_dir)], :con => "org.eclipse.jdt.launching.JRE_CONTAINER", :lib => external.map(&:to_s) }.each do |kind, paths| paths.to_a.each { |path| xml.classpathentry :kind=>kind, :path=>path } end internal.map(&:name).uniq.each do |prj_name| xml.classpathentry :kind=>"src", :combineaccessrules=>"false", :path=>"/#{prj_name}" end end end end # The only thing we need to look for is a change in the Rakefile. file(project.path_to(".project")=>sources) do |task| puts "Writing #{task.name}" if verbose File.open(task.name, "w") do |file| xml = Builder::XmlMarkup.new(:target=>file, :indent=>2) xml.projectDescription do xml.name project.name xml.projects xml.buildSpec do xml.buildCommand do xml.name "org.eclipse.jdt.core.javabuilder" end end xml.natures do xml.nature "org.eclipse.jdt.core.javanature" end end end end end end end end # module Buildr