views:

87

answers:

1

I'm a bit lost here as to why my rake task will not create the desired XML file, however it works fine when I have the method build_xml in an .rb file.

require 'rubygems'  
require 'nokogiri'  
require 'open-uri'  

namespace :xml do
  desc "xml build test"
  task :xml_build => :environment do
    build_xml
  end 
end

def build_xml
  # build xml docoument
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.root {
      xml.location {
        xml.value "test" 
      }
    }
  end
  File.open("test.xml", 'w') {|f| f.write(builder.to_xml) }
end
+1  A: 

When I execute your rakefile with rake xml:xml_build, I get an error that the task environment is not defined. If I remove environment as a dependency of xml_build and then run it again it works fine and creates the xml file.

So my guess is that your environment task causes an error and xml_build never runs (or the task doesn't exist in your real rakefile either, so just remove it as a dependency).

sepp2k
Indeed, he's probably copied this Rakefile code from a rails project (where `environment` would be defined for you) but isn't running it inside one now.
thenduks
Also, not a big deal but `open-uri` isn't needed for this script :)
thenduks
i have removed the environment dependency, script output xml to screen ok but the test.xml is still not created.
could it be some file permission issue when running the script as a rake task?again the same script successfully created the test.xml file when i had the code snippet in the .rb file
ok, i have worked out the problem. the File.open("test.xml", 'w') was not working, i had to include the full path to test.xml.my rake environment is probably not setup probably.