Skip to content

Instantly share code, notes, and snippets.

@wsams
Last active August 29, 2015 14:17
Show Gist options
  • Save wsams/65b545e85ead9e86ce5b to your computer and use it in GitHub Desktop.
Save wsams/65b545e85ead9e86ce5b to your computer and use it in GitHub Desktop.
This is a command line script I use for managing my Java projects. It uses the optparse library for parsing command line options.
#!/usr/bin/ruby
require 'optparse'
require 'fileutils'
@APPLICATION
@ENVIRONMENT = "dev".freeze
@DROPSPOT = "/opt/j2ee/deploy".freeze
@WC = "/Users/foobar/espresso/nb-wc".freeze
@SVN_WC = "/Users/foobar/espresso/nb-wc".freeze
# This is a Mac specific command that opens a browser given a URL. Maybe there
# is a UNIX/Linux/Windows equivalent.
# Set USE_OPEN = "no" if you don't have this command, or "yes" to use it.
@OPEN = "open".freeze
@USE_OPEN = FALSE.freeze
@VARS = {
"espresso-press" => {
:wc => "espresso-press-trunk",
:tomcat_home => "/Users/foobar/espresso/apache-tomcat-8.0.14-espresso-press",
:tomcat_context_dir => "/Users/foobar/espresso/apache-tomcat-8.0.14-espresso-press/webapps/espresso-press-dev",
:war => "espresso-press.war",
:target_war => "#{@WC}/espresso-press-trunk/target/espresso-press.war",
:url => "http://localhost:8120/espresso-press-dev/",
:logfile => "/Users/foobar/espresso/apache-tomcat-8.0.14-espresso-press/logs/catalina.out"
},
"espresso-tamp" => {
:wc => "espresso-tamp-trunk",
:tomcat_home => "/Users/foobar/espresso/apache-tomcat-8.0.14-espresso-tamp",
:tomcat_context_dir => "/Users/foobar/espresso/apache-tomcat-8.0.14-espresso-tamp/webapps/espresso-tamp-dev",
:war => "espresso-tamp.war",
:target_war => "#{@WC}/espresso-tamp-trunk/target/espresso-tamp.war",
:url => "http://localhost:8220/espresso-tamp-dev/",
:logfile => "/opt/j2ee/logs/espresso-tamp/#{@ENVIRONMENT}/espresso-tamp-#{@ENVIRONMENT}-log4j.log"
},
"espresso-grind" => {
:wc => "espresso-grind-trunk",
:tomcat_home => "/Users/foobar/espresso/apache-tomcat-8.0.14-espresso-grind",
:tomcat_context_dir => "/Users/foobar/espresso/apache-tomcat-8.0.14-espresso-grind/webapps/espresso-grind-dev",
:war => "espresso-grind.war",
:target_war => "#{@WC}/espresso-grind-trunk/target/espresso-grind.war",
:url => "http://localhost:8221/espresso-grind-dev/",
:logfile => "/opt/j2ee/logs/espresso-grind/#{@ENVIRONMENT}/espresso-grind-#{@ENVIRONMENT}-log4j.log"
},
"espresso-color" => {
:wc => "espresso-color-trunk",
:tomcat_home => "/Users/foobar/espresso/apache-tomcat-8.0.14-espresso-color",
:tomcat_context_dir => "/Users/foobar/espresso/apache-tomcat-8.0.14-espresso-color/webapps/espresso-color-dev",
:war => "espresso-color.war",
:target_war => "#{@WC}/espresso-color-trunk/target/espresso-color.war",
:url => "http://localhost:8121/espresso-color-dev/",
:logfile => "/Users/foobar/espresso/apache-tomcat-8.0.14-espresso-color/logs/catalina.out"
}
}.freeze
@TARGET_WAR
def verify_options
if @APPLICATION.empty?
puts "You must provide '-a appcode' argument first."
exit
end
Dir.chdir("#{@WC}/#{@VARS[@APPLICATION][:wc]}")
end
def clean_maven
system "mvn clean"
end
def run_tests
system "mvn clean test -Dapplication=#{@APPLICATION} -Denvironment=#{@ENVIRONMENT} -Dspring.profiles.active=dev"
end
def package_settings
system "mvn espresso:config espresso:readme -Dapplication=#{@APPLICATION} -Denvironment=#{@ENVIRONMENT}"
end
def start_tomcat
system "#{@VARS[@APPLICATION][:tomcat_home]}/bin/startup.sh"
system "#{@OPEN} #{@VARS[@APPLICATION][:url]}" if @USE_OPEN
end
def stop_tomcat
system "#{@VARS[@APPLICATION][:tomcat_home]}/bin/shutdown.sh"
end
def tail(tail_what)
system "tail -#{tail_what} #{@VARS[@APPLICATION][:logfile]}"
end
def clean_tomcat
FileUtils.rm_rf(@VARS[@APPLICATION][:tomcat_context_dir]) if Dir.exists?(@VARS[@APPLICATION][:tomcat_context_dir])
File.unlink("#{@VARS[@APPLICATION][:tomcat_context_dir]}.war") if File.exists?("#{@VARS[@APPLICATION][:tomcat_context_dir]}.war")
end
def run_project
clean_maven
system "mvn package -DskipTests=true -Dapplication=#{@APPLICATION} -Denvironment=#{@ENVIRONMENT} -Dspring.profiles.active=dev"
if File.exists?(@VARS[@APPLICATION][:target_war])
clean_tomcat
FileUtils.cp(@VARS[@APPLICATION][:target_war], "#{@VARS[@APPLICATION][:tomcat_context_dir]}.war")
stop_tomcat
start_tomcat
else
puts "Could not find #{@VARS[@APPLICATION][:target_war]}. Won't start Tomcat.";
end
end
def status
system "svn st"
end
def update
system "svn up"
end
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: espresso -a appcode [options]"
opts.version = "0.1.0"
opts.on("-a", "--appcode", "The application code e.g. espresso-press") do
@APPLICATION = ARGV[0]
@APPLICATION.freeze
end
opts.on("-A", "--start-tomcat", "Start Tomcat") do
verify_options
start_tomcat
end
opts.on("-c", "--clean-maven", "Clean with maven i.e. mvn clean") do
verify_options
clean_maven
end
opts.on("-l", "--tail", "Tail log file i.e. tail -f or tail -1000") do
verify_options
tail(ARGV[0])
end
opts.on("-p", "--config", "Package settings and security files i.e. mvn espresso:config") do
verify_options
package_settings
end
opts.on("-q", "--clean-tomcat", "Removes WAR and deployed code in Tomcat") do
verify_options
clean_tomcat
end
opts.on("-r", "--run-project", "Deploys and runs application") do
verify_options
run_project
end
opts.on("-s", "--status", "Check source code status i.e. svn st") do
verify_options
status
end
opts.on("-t", "--tests", "Run tests i.e. mvn test") do
verify_options
run_tests
end
opts.on("-u", "--update", "Update source code i.e. svn up") do
verify_options
update
end
opts.on("-Z", "--stop-tomcat", "Stop Tomcat") do
verify_options
stop_tomcat
end
end.parse!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment