Class: Buildr::Tomcat

Inherits:
Object
  • Object
show all
Defined in:
lib/tomcat.rb

Constant Summary collapse

VERSION =

Which version of Tomcat we’re using by default (change with options.tomcat.version).

"6.1.3"
SLF4J_VERSION =
"1.4.3"
TOMCAT_VERSION =
'7.0.21'
TOMCAT =
[ "org.apache.tomcat.embed:tomcat-embed-jasper:jar:#{TOMCAT_VERSION}", "org.apache.tomcat:tomcat-catalina:jar:#{TOMCAT_VERSION}", "org.apache.tomcat:tomcat-jasper:jar:#{TOMCAT_VERSION}", "org.apache.tomcat:tomcat-servlet-api:jar:#{TOMCAT_VERSION}", "org.apache.tomcat.embed:tomcat-embed-core:jar:#{TOMCAT_VERSION}", "org.apache.tomcat.embed:tomcat-embed-logging-log4j:jar:#{TOMCAT_VERSION}" ]
URL =

Default URL fort (change with options.tomcat.url).

"http://localhost:8080"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, url, webAppLocation) ⇒ Tomcat

:nodoc:



46
47
48
49
50
51
52
53
54
# File 'lib/tomcat.rb', line 46

def initialize(name, url, webAppLocation) #:nodoc:
  @webAppLocation = webAppLocation
  @url = url
  namespace name do
    @setup = task("setup")
    @teardown = task("teardown")
    @use = task("use") { fire }
  end
end

Instance Attribute Details

#urlObject

The URL for the Tomcat server. Leave as is if you want to use the default server (localhost:8080).



58
59
60
# File 'lib/tomcat.rb', line 58

def url
  @url
end

Class Method Details

.explode(project) ⇒ Object

Explode a war-file into the target directory.



36
37
38
39
40
41
42
43
# File 'lib/tomcat.rb', line 36

def explode(project)
  name = project.name.split(':').last
  dirname = "#{name}/target/#{name}-#{VERSION_NUMBER}"

  if !File.exists? dirname
    system("unzip -q #{name}/target/#{name}-#{VERSION_NUMBER}.war -d #{dirname}")
  end
end

.instanceObject

:call-seq:

instance() => Tomcat

Returns an instance of Tomcat.



28
29
30
# File 'lib/tomcat.rb', line 28

def instance()
  @instance ||= Tomcat.new("", URL)
end

Instance Method Details

#runObject



85
86
87
88
# File 'lib/tomcat.rb', line 85

def run() 
    puts "***** RUNNING IN THE BACKGROUND *****"
	use.invoke
end

#running?Boolean

:call-seq:

running?() => boolean

Returns true if it finds a running Tomcat server that supports the Buildr requests for deploying, stopping, etc.

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tomcat.rb', line 115

def running?()
  puts "Checking if already running..."
  uri = URI.parse(url)
  begin
    Net::HTTP.start(uri.host, uri.port) do |http|
      response = http.request_get("/buildr/")
      response.is_a?(Net::HTTPSuccess) && response.body =~ /Alive/
    end
  rescue Errno::ECONNREFUSED, Errno::EBADF
    false
  end
end

#setup(*prereqs, &block) ⇒ Object

:call-seq:

setup(*prereqs) => task
setup(*prereqs) { |task| .. } => task

This task executes when Tomcat is first used in the build. You can use it to deploy artifacts into Tomcat.



135
136
137
# File 'lib/tomcat.rb', line 135

def setup(*prereqs, &block)
  @setup.enhance prereqs, &block
end

#start(sync = nil) ⇒ Object

:call-seq:

start(pipe?)

Starts Tomcat. This method does not return, it keeps the thread running until Tomcat is stopped. If you want to run Tomcat parallel with other tasks in the build, invoke the #use task instead.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tomcat.rb', line 66

def start(sync = nil)
  begin
    puts "classpath #{Java.classpath.inspect}"
    port = URI.parse(url).port
    puts "***** Starting Tomcat at http://localhost:#{port}"
    Java.load
	tomcat = Java.com.technophobia.buildr.tomcat.TomcatWrapper.new(port, URI.parse(url).path, @webAppLocation)
	
	puts "***** Tomcat has started up *****"
    sync << "Started" if sync
    sleep # Forever
  rescue Interrupt # Stopped from console
	puts "Interrupted"
  rescue Exception=>error
    puts "#{error.class}: #{error.message}"
  end
  exit! # No at_exit
end

#stopObject

:call-seq:

stop()

Stops Tomcat. Stops a server running in a separate process.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tomcat.rb', line 94

def stop()
  puts "***** STOPPING TOMCAT *****"
  uri = URI.parse(url)
  begin
    Net::HTTP.start(uri.host, uri.port) do |http|
      http.request_post "/buildr/stop", ""
    end
  rescue Errno::ECONNREFUSED
    # Expected if Tomcat server not running.
  rescue EOFError
    # We get EOFError because Tomcat is brutally killed.
  end
  puts "***** Told tomcat server to stop"
end

#teardown(*prereqs, &block) ⇒ Object

:call-seq:

teardown(*prereqs) => task
teardown(*prereqs) { |task| .. } => task

This task executes when the build is done. You can use it to undeploy artifacts previously deployed into Tomcat.



145
146
147
# File 'lib/tomcat.rb', line 145

def teardown(*prereqs, &block)
  @teardown.enhance prereqs, &block
end

#use(*prereqs, &block) ⇒ Object

:call-seq:

use(*prereqs) => task
use(*prereqs) { |task| .. } => task

If you intend to use Tomcat, invoke this task. It will start a new instance of Tomcat and close it when the build is done. However, if you already have a server running in the background (e.g. tomcat:start), it will use that server and will not close it down.



157
158
159
# File 'lib/tomcat.rb', line 157

def use(*prereqs, &block)
  @use.enhance prereqs, &block
end