#!/usr/bin/ruby
#
# This script modifies or creates a gradle.properties file
# and injects Nexus credentials in it to be used to release
# artifacts to Nexus
#
# Usage:
# nexus_gradle_properties.rb \
# -u nexus_username \
# -p nexus_password
#
require 'optparse'
require 'logger'
# Set up logger
@logger = Logger.new(STDOUT)
@logger.formatter = proc do |severity, datetime, progname, msg|
"#{severity}: #{msg}\n"
end
@logger.level = Logger::INFO
# Read in config file, modify contents, write it back
def main(options = parse_args)
if !File.exists? gradle.properties
create_file
end
end
# Create gradle.pzroperties file
def create_file() {
@logger.info 'No gradle.properties file found. Creating gradle.properties'
gradle_properties <<-EOF
nexus_username=${ARGV[0]}
nexus_password=${ARGV[1]}
EOF
File.open('gradle.properties', 'w') {|f| f.write(gradle_properties)}
end
# Parse arguments
def parse_args()
options = {}
optparse = OptionParser.new do |opts|
opts.banner <<-EOF
Modifies or creates a gradle.properties file
and injects Nexus credentials in it
to be used to release artifacts to
Nexus \nUsage: #{__FILE__} [options]"
EOF
opts.on("-u", "--nexus-username NEXUS_USERNAME", String, "Nexus username") do |f|
options[:nexus_username] = u
end
opts.on("-p", "--nexus-password NEXUS_PASSWORD", String, "Nexus password") do |p|
options[:nexus_password] = p
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end
begin
optparse.parse!
if options.length < 2
exit_with_error "Both the username and password for Nexus must be supplied\n\n#{optparse}"
end
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
exit_with_error $!.to_s
end
options
end
def exit_with_error(message, status_code = 1)
@logger.error message
exit status_code
end
main