module Mvnizer::TaskHelper

The TaskHelper provides different functions that can be used in tasks, such as function to create directories, generate files from templates, etc.

Constants

TEMPLATE_DIR

path to the template location.

Public Instance Methods

add_dependency(dependency, pom_location = File.join(Dir.pwd, "pom.xml")) click to toggle source

adds a list of dependencies to the pom file whose location can be given as pom_location. By default, this function looks up the pom in the current directory.

# File lib/mvnizer/task_helper.rb, line 36
def add_dependency(dependency, pom_location = File.join(Dir.pwd, "pom.xml"))
  raise FileNotFoundError, "The pom.xml file cannot be found." unless File.exists?(pom_location)

  coordinate_parser = CoordinateParser.new

  pom = Nokogiri::XML(File.open(pom_location)) do |c|
    c.noblanks
  end
  dependencies_node = pom.xpath("/pom:project/pom:dependencies", {"pom" => "http://maven.apache.org/POM/4.0.0"}).first

  dependency.each do |d|
    # First parse the dependency coordinates
    dep_project = coordinate_parser.parse_scoped_coordinates(d)

    Nokogiri::XML::Builder.with(dependencies_node) do |xml|
      xml.dependency {
        xml.groupId dep_project.group_id
        xml.artifactId dep_project.artifact_id
        xml.version dep_project.version
        xml.scope dep_project.scope if dep_project.scope != nil && dep_project.scope != "compile"
        xml.type dep_project.type if dep_project.type != "jar"
      }
    end
  end

  # Requires sparklemotion/nokogiri#772 to produce
  # identical pom files in both MRI and JRuby.
  pom.to_xml(indent: 2)
end
create_dir(*dir) click to toggle source

creates recursively all directories passed as a param.

# File lib/mvnizer/task_helper.rb, line 12
def create_dir(*dir)
  dir.each { |d| FileUtils.mkdir_p d }
end
generate_file(template, destination_file, binding) click to toggle source

generates the file destination_file from template template and object binding. binding must be an ERB binding. destination_file must be the aboslute location to the generated file. If the output folder in which the file gets generated does not exist, it automatically gets created.

# File lib/mvnizer/task_helper.rb, line 22
def generate_file(template, destination_file, binding)
  dir = File.dirname(destination_file)
  create_dir(dir) unless File.exists?(dir)

  template = File.open(template, 'r').read

  File.open(destination_file, "w") do |f|
    f.write(ERB.new(template).result(binding.get_binding))
  end
end