Skip to content

Instantly share code, notes, and snippets.

@dmcnulla
Created December 20, 2015 08:28
Show Gist options
  • Save dmcnulla/c32dca6fe84f32952cbe to your computer and use it in GitHub Desktop.
Save dmcnulla/c32dca6fe84f32952cbe to your computer and use it in GitHub Desktop.
require 'rexml/document'
PLUGIN_PATH = 'project/build/plugins/plugin'
ARTIFACT_PATH = 'artifactId'
COMPILER_NAME = 'maven-compiler-plugin'
NEW_PLUGIN = "<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>"
def get_file_list(dir)
Dir.glob("#{dir}/**/pom.xml")
end
def get_xml(pom)
file = File.open(pom)
REXML::Document.new(file)
end
def save_xml(file_name, xml)
File.open(file_name, 'w') do |file|
file.puts(xml)
end
# puts file_name
# puts xml
end
def is_compiler?(plugin)
artifacts = []
plugin.elements.each(ARTIFACT_PATH) do |artifact|
artifacts << artifact.text
end
artifacts[0] == COMPILER_NAME
end
def update_xml(xml)
new_plugin = REXML::Document.new(NEW_PLUGIN)
xml.root.insert_before("//plugin", new_plugin)
xml
end
def fix(pom)
good = false
myxml = get_xml(pom)
myxml.elements.each(PLUGIN_PATH) do |plugin|
good = true if is_compiler?(plugin)
end
save_xml(pom, update_xml(myxml)) unless good
end
poms = get_file_list(ARGV[0])
poms.each do |pom|
begin
fix(pom)
rescue Exception => e
# puts "Error updating #{pom}"
# puts e.message
# puts e.backtrace.inspect
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment