Skip to content

Instantly share code, notes, and snippets.

@aboisvert
Created April 5, 2013 22:25
Show Gist options
  • Save aboisvert/5323180 to your computer and use it in GitHub Desktop.
Save aboisvert/5323180 to your computer and use it in GitHub Desktop.
Proguard plugin for Apache Buildr
#
# Usage:
#
# Add the following at the top of your buildfile,
#
# require 'proguard.rb'
#
# And specify your proguard jar in your project,
#
# package(:proguard).tap do |proguard|
# proguard.keep << 'class Hello'
# # other options ...
# end
#
module Buildr
module Proguard
REQUIRES = [ "net.sf.proguard:proguard-base:jar:4.8" ]
Java.classpath << lambda { REQUIRES }
class ProguardTask < Rake::FileTask
attr_accessor :dependencies
attr_accessor :libraries
attr_accessor :keep
def initialize(*args)
super
@dependencies ||= []
@keep ||= []
rt_jar ||= begin
home = ENV['JAVA_HOME'] or fail 'Are we forgetting something? JAVA_HOME not set.'
['jre/lib/rt.jar', '../lib/rt.jar'].map { |path| File.expand_path(path, home) }.find { |path| File.exist?(path) }
end
@libraries ||= [rt_jar]
enhance do
Java.load
cmd_args = [
'-libraryjars', libraries.join(File::PATH_SEPARATOR),
'-injars', dependencies.join(File::PATH_SEPARATOR),
'-outjar', self.to_s,
'-keep', Array(keep).join(",")
]
trace "ProGuard.main #{cmd_args.inspect}"
Java.proguard.ProGuard.main(cmd_args.to_java(Java.java.lang.String))
end
end
end
end
def package_as_proguard(file_name)
Proguard::ProguardTask.define_task(file_name).tap do |proguard|
proguard.dependencies = [compile.target]
end
end
def package_as_proguard_spec(spec) #:nodoc:
spec.merge(:type => :jar)
end
class Project
include Proguard
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment