Archive for March, 2009
Maven with Ruby Part II
Want to add something to your projects Maven builds (like code coverage…) but don’t want to CTL-C/CTL-V your way to boredom? Use the below script to lend a hand. Substitute the XML in the SNIPPET variable to the artifact/plugin/dependency to be injected (leave the
$>ruby mavenInjector.rb /xpath/to/element
Here’s the code. Check the git repository for the latest.
In the beginning of the ‘for’ loop in the inject method I remove the read-only bit from the file. This is for those of us afflicted with ClearCase and can be removed for those fortunate enough to use a sane SCM.
#/usr/bin/env ruby
require 'rexml/document.rb'
require 'fileutils.rb'
class MavenInjector
def inject(path)
if(path == nil)
puts 'Must provide the root path of the node to inject under.'
exit
end
poms = Dir[Dir.pwd() + "/**/pom.xml"]
for pom in poms do
if(!File.writable?(pom))
FileUtils.chmod 0644, pom
end
pomXML = REXML::Document.new(File.new(pom))
element = pomXML.get_elements(path)[0]
if(element == nil)
parentElement = pomXML.get_elements("/project/build")[0]
element = REXML::Element.new("plugins", parentElement)
end
for plugin in getElementsToInject() do
element.add_element(plugin)
end
formatter = REXML::Formatters::Default.new(4)
formatter.write(pomXML, File.new(pom, "w+"))
end
end
def getElementsToInject()
elementXML = REXML::Document.new(SNIPPET)
return elementXML.get_elements("/inject/plugin")
end
end
SNIPPET = <
org.codehaus.mojo
cobertura-maven-plugin
html
HEAD
MavenInjector.new.inject(ARGV[0])
Any questions/problems/enhancements are welcome in the comments.
View CommentsUsing Ruby To Help Maven
Maven projects can become a wasteland of neglected, old dependencies: huge, unmanageable pom files and a local repository that’s catalog of every Apache project in the past 10 years. Who knows if your project still requires that old version of Xerces?
This script takes the name of a dependency and removes it from every pom.xml in any sub-directory from where it’s called, removing the dependency element for the given jar file from each one. The script also saves the
#/usr/bin/env ruby
require 'rexml/document.rb'
if(ARGV[0] == nil)
puts 'Must provide the name of the artifact to remove.'
exit
end
artifact = ARGV[0].chomp
poms = Dir[Dir.pwd() + "/**/pom.xml"]
for pom in poms do
pomXML = REXML::Document.new(File.new(pom))
element = pomXML.get_elements("//dependency[artifactId='" + artifact + "']")[0]
if(element != nil)
temp = File.new(artifact + ".xml", "w+")
formatter = REXML::Formatters::Default.new()
formatter.write(element, temp)
puts "Removing the dependency: " + artifact + "\nfrom " + pom
element.parent.delete(element)
pomXML.write(File.new(pom, "w+"))
else
puts "No artifacts with the name:'" + artifact + "' were found."
end
end
View Comments