Skip to content

Instantly share code, notes, and snippets.

@cacoco
Last active September 25, 2015 12:28
Show Gist options
  • Save cacoco/921897 to your computer and use it in GitHub Desktop.
Save cacoco/921897 to your computer and use it in GitHub Desktop.
Buildr Extension for precompiling JSP files.
module Precompiler
include Extension
first_time do
# Define task not specific to any projet.
desc 'Precompile jsps'
Project.local_task('precompile' => 'compile') { |name| "Precompiling #{name}" }
end
before_define do |project|
# Define the precompile task for this particular project.
project.task('precompile'=>project.task('compile'))
project.group ||= project.parent && project.parent.group || project.name
project.version ||= project.parent && project.parent.version
end
# To use this method in your project:
# precompile :keep=>true
def precompile(*args)
options = args.pop
keep = options[:keep]
Rake::Task.define_task 'precompile' do |task|
webxml = File.join(project.path_to(:src, :main, :webapp), "WEB-INF", "web.xml")
ant('precompile') do |ant|
ant.taskdef :resource=>'org/apache/catalina/ant/catalina.tasks' do |ant|
ant.classpath do |ant|
Dir.glob(File.join(TOMCAT_HOME, "lib", "*.jar")) do |f|
ant.fileset :file=>f
end
JASPER.each do |f|
ant.fileset :file=>artifact(f)
end
project.compile.dependencies.each do |f|
ant.fileset :file=>artifact(f)
end
ant.pathelement :location => project.path_to(:target, :classes)
ant.fileset :file=>artifact(:taglibs)
end
end
# Jasper is not creating the generated_web.xml, so start with the original web.xml
mkdir_p File.join(project.path_to(:target, :generated), "WEB-INF")
cp webxml, File.join(project.path_to(:target, :generated), "WEB-INF", "generated_web.xml"), :verbose=>true
ant.jasper2 :validateXml=>false, :uriroot=>project.path_to(:src, :main, :webapp), :compilerTargetVM=>"1.5", :compilerSourceVM=>"1.5", :webXmlFragment=>File.join(project.path_to(:target, :generated), "WEB-INF", "generated_web.xml"), :outputDir=>File.join(project.path_to(:target, :generated))
if keep
# if we're not just precompiling for a sanity check, compile the servlets into the WEB-INF/classes dir
ant.javac :includeantruntime=>false, :fork=>true, :destdir=>project.path_to(:target, :classes), :optimize=>"on", :failonerror=>false, :srcdir=>File.join(project.path_to(:target, :generated)), :memoryInitialSize=>"512m",
:memoryMaximumSize=>"1024m", :excludes=>"**/*.smap", :source=>"1.5", :target=>"1.5", :includes=>"**/*.java" do
ant.compilerarg :line=>"-Xlint:none"
ant.classpath do |ant|
Dir.glob(File.join(TOMCAT_HOME, "lib", "*.jar")) do |f|
ant.fileset :file=>f
end
ant.pathelement :location => project.path_to(:target, :classes)
project.compile.dependencies.each do |f|
ant.fileset :file=>artifact(f)
end
ant.fileset :file=>artifact(:taglibs)
end # end classpath
ant.include :name=>"**"
ant.exclude :name=>"tags/**"
end # end javac
# Copy the generated_web.xml content into the web.xml in the generated directory
open(File.join(project.path_to(:target, :generated), "WEB-INF", "web.xml"), 'w') do |file|
original = File.read(webxml)
generated = File.read(File.join(project.path_to(:target, :generated), "WEB-INF", "generated_web.xml"))
file.write(original.gsub("</web-app>", generated))
file.write("</web-app>")
end # end new web.xml
project.packages.first.enhance do |task|
project.packages.first.include(File.join(project.path_to(:target, :generated), "WEB-INF", "web.xml"), :as=>'WEB-INF/web.xml')
end # end tell war about new web.xml
# sh %{svn revert #{webxml}}
end # end if keep
end # end ant precompile task
end # end rake define_task
end # end def precompile()
end
class Buildr::Project
include Precompiler
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment