Skip to content

Commit

Permalink
refactorng to follow ruby-maven and allow extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mkristian committed Mar 1, 2013
1 parent 6361b01 commit 2822aff
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 167 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
.pom.xml
target
*.gem
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -27,7 +27,7 @@ TODO more advanced example and current config

## running any given war-file ##

jetty-war war /path/to/war-file
jetty-run war /path/to/war-file

with this you warble your warfile and use jetty-run to start it with jetty.

Expand All @@ -40,3 +40,8 @@ see
# note #

orginally the code was part the jruby-maven-plugins and slowly the functionality moved to the ruby side of things. so things are on the move and there is room for improvements . . .

# meta-fu #

bug-reports and pull request are most welcome.

56 changes: 21 additions & 35 deletions bin/jetty-run
@@ -1,30 +1,39 @@
#!/usr/bin/env ruby
require 'maven/jetty/ruby_maven'
require 'maven/jetty/cli'
require 'thor'

class JettyCommand < Thor
no_tasks do
def mvn
@mvn ||= Maven::Jetty::RubyMaven.new
@mvn ||= Maven::Jetty::Cli.new
end

def exec(*args)
ARGV.clear # clean up in case another script gets executed
mvn.exec(args)
end

def maven_args
args = ARGV.dup
if i = args.index("--")
maven_args = args[i..-1]
end
maven_args ||= []
if options[ :pom ]
maven_args << '--pom'
maven_args << options[ :pom ]
end
maven_args
end
end

desc "[run]", "runs jetty with a rails filesystem layout"
method_option :environment, :aliases => '-e', :type => :string, :required => false, :desc => 'rails environment'
desc "[run]", "runs jetty with either rails filesystem layout or with config.ru"
method_option :environment, :aliases => '-e', :type => :string, :required => false, :desc => 'rails environment (no effect on rackup setups)'
method_option :port, :type => :numeric, :default => 8080, :desc => 'http port'
method_option :sslport, :type => :numeric, :default => 8443, :desc => 'https port'
method_option :pom, :type => :string, :default => '.jetty-pom.xml', :desc => 'name of pom file which get generated (jetty-run just generates a pom.xml which gets execued by maven)'
def server(*args)
args = ARGV.dup
args.delete('server')
if i = args.index("--")
maven_args = args[i..-1]
end
maven_args ||= []
maven_args = self.maven_args
maven_args << "-Drails.env=#{options[:environment]}" if options[:environment]
maven_args << "-Djetty.port=#{options[:port]}"
maven_args << "-Djetty.sslport=#{options[:sslport]}"
Expand All @@ -35,18 +44,14 @@ class JettyCommand < Thor
method_option :port, :type => :numeric, :default => 8080, :desc => 'http port'
method_option :sslport, :type => :numeric, :default => 8443, :desc => 'https port'
def war(file, *args)
args = ARGV.dup
if i = args.index("--")
maven_args = args[i..-1]
end
maven_args ||= []
maven_args = self.maven_args
maven_args << "-Djetty.war=#{file}"
maven_args << "-Djetty.port=#{options[:port]}"
maven_args << "-Djetty.sslport=#{options[:sslport]}"
exec(*(["jetty:deploy-war", "-Pwar"] + maven_args))
end

desc "pom", "dump the pom used into pom.xml"
desc "pom", "dump the jetty pom into pom.xml"
method_option :force, :type => :boolean, :default => false, :desc => 'force to overwrite pom.xml'
def pom(*args)
if File.exists?('pom.xml') && !options[:force]
Expand All @@ -55,29 +60,10 @@ class JettyCommand < Thor
mvn.dump_pom(options[:force])
end
end

def help(*args)
super(*args)
goal =
case args[0]
when 'server'
'run'
when 'war'
'deploy-war'
end
# TODO not sure whether or not these options are needed/helpful
# if goal
# puts
# puts "Maven Options: (from jetty maven plugin)"
# exec ["jetty:help", "-Ddetail=true", "-Dgoal=#{goal}"]
# end
end

end
if i = ARGV.index('run')
ARGV[i] = 'server'
end
#p ARGV.detect {|a| p a ;a =~ /^[a-z]/ }
unless ARGV[0] =~ /^[a-z]/
ARGV.insert(0, 'server')
end
Expand Down
11 changes: 11 additions & 0 deletions lib/maven/jetty/Mavenfile
@@ -0,0 +1,11 @@
#-*- mode: ruby -*-

jar("org.jruby.rack:jruby-rack", '1.1.13.1')
jar("org.jruby:jruby-core", "${jruby.version}")

# lock down versions
properties['jruby.version'] = '1.7.2'
properties['jruby.plugins.version'] = '0.29.3'
properties['jetty.version'] = '8.1.9.v20130131'

# vim: syntax=Ruby
14 changes: 14 additions & 0 deletions lib/maven/jetty/cli.rb
@@ -0,0 +1,14 @@
require 'maven/ruby/cli'
require 'maven/jetty/pom_magic'

module Maven
module Jetty
class Cli < Maven::Ruby::Cli

def magic_pom( dir = '.', *args )
PomMagic.new.generate_pom( File.expand_path( dir ), *args )
end

end
end
end
90 changes: 90 additions & 0 deletions lib/maven/jetty/jetty_project.rb
@@ -0,0 +1,90 @@
require 'maven/tools/gem_project'

module Maven
module Jetty
class JettyProject < Maven::Tools::GemProject

tags :dummy

private

CONNECTOR_XML = <<-XML
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>${jetty.port}</port>
</connector>
<connector implementation="org.eclipse.jetty.server.ssl.SslSelectChannelConnector">
<port>${jetty.sslport}</port>
<keystore>${project.build.directory}/jetty/server.keystore</keystore>
<keyPassword>123456</keyPassword>
<password>123456</password>
</connector>
XML

public

def rails?( dir = '.' )
File.exists? File.join( dir, 'config', 'application.rb' )
end

def connector_xml
if File.exists?( 'src/test/resources/server.keystore' )
CONNECTOR_XML.sub( /..project.build.directory..jetty/, '${project.basedir}/src/test/resources' )
else
CONNECTOR_XML
end
end

def web_xml
if File.exists?( File.join( 'config', 'web.xml' ) )
File.join( 'config', 'web.xml' )
elsif File.exists?('web.xml')
'web.xml'
end
end

def add_defaults
super
self.properties.merge!({
"jetty.war" => "${project.build.directory}/${project.build.finalName}.war",
"jetty.port" => '8080',
"jetty.sslport" => '8443'
})

self.properties[ 'rails.env' ] = 'development' if rails?

profile(:war).plugin("org.mortbay.jetty:jetty-maven-plugin",
"${jetty.version}")do |jetty|
options = {
:war => "${jetty.war}",
:connectors => connector_xml
}
jetty.with options
end

profile(:run) do |run|
overrideDescriptor = rails? ? '${project.build.directory}/jetty/override-rails-${rails.env}-web.xml' : '${project.build.directory}/jetty/override-rack-web.xml'
run.activation.by_default
run.plugin("org.mortbay.jetty:jetty-maven-plugin",
"${jetty.version}") do |jetty|
options = {
:webAppConfig => {
:overrideDescriptor => overrideDescriptor
},
:systemProperties => {
:systemProperty => {
:name => 'jbundle.skip',
:value => 'true'
}
},
:connectors => connector_xml,
:webXml => web_xml,
:webAppSourceDirectory => "."
}
jetty.with options
end
end
end
end
end
end
11 changes: 11 additions & 0 deletions lib/maven/jetty/override-rack-web.xml
@@ -0,0 +1,11 @@
<web-app>
<context-param>
<!-- must match the place where the gems are located -->
<param-name>gem.path</param-name>
<param-value>./target/rubygems:./target/rubygems-bundler-maven-plugin</param-value>
</context-param>
<context-param>
<param-name>jruby.rack.logging</param-name>
<param-value>stdout</param-value>
</context-param>
</web-app>
19 changes: 19 additions & 0 deletions lib/maven/jetty/override-rails-development-web.xml
@@ -0,0 +1,19 @@
<web-app>
<context-param>
<!-- must match the place where the gems are located -->
<param-name>gem.path</param-name>
<param-value>./target/rubygems:./target/rubygems-bundler-maven-plugin</param-value>
</context-param>
<context-param>
<param-name>rails.env</param-name>
<param-value>development</param-value>
</context-param>
<context-param>
<param-name>jruby.rack.logging</param-name>
<param-value>stdout</param-value>
</context-param>
<context-param>
<param-name>jruby.rack.layout_class</param-name>
<param-value>JRuby::Rack::RailsFilesystemLayout</param-value>
</context-param>
</web-app>
19 changes: 19 additions & 0 deletions lib/maven/jetty/override-rails-production-web.xml
@@ -0,0 +1,19 @@
<web-app>
<context-param>
<!-- must match the place where the gems are located -->
<param-name>gem.path</param-name>
<param-value>./target/rubygems:./target/rubygems-bundler-maven-plugin</param-value>
</context-param>
<context-param>
<param-name>rails.env</param-name>
<param-value>production</param-value>
</context-param>
<context-param>
<param-name>jruby.rack.logging</param-name>
<param-value>stdout</param-value>
</context-param>
<context-param>
<param-name>jruby.rack.layout_class</param-name>
<param-value>JRuby::Rack::RailsFilesystemLayout</param-value>
</context-param>
</web-app>
92 changes: 92 additions & 0 deletions lib/maven/jetty/pom_magic.rb
@@ -0,0 +1,92 @@
require 'maven/ruby/pom_magic'
require 'maven/jetty/jetty_project'
require 'fileutils'

module Maven
module Jetty
class PomMagic < Maven::Ruby::PomMagic

def generate_pom( dir = '.', *args )

proj = JettyProject.new

ensure_web_xml( dir, proj )
ensure_mavenfile( dir )
ensure_keystore( dir )
ensure_overlays( dir )

load_standard_files( dir, proj )

pom_xml( dir, proj, args )
end

def ensure_overlays( dir )
target = target_dir( dir )
Dir[ File.join( File.dirname( __FILE__ ), "override-*-web.xml" ) ].each do |f|
FileUtils.cp( f, target )
end
end

def ensure_web_xml( dir, proj, source = File.dirname( __FILE__ ), target = nil )
target ||= File.join( config_dir( dir ), 'web.xml' )
if !File.exists?( target ) && !File.exists?( File.join( dir, 'src', 'main', 'webapp', 'WEB-INF', 'web.xml' ) )
flavour = rails?( dir ) ? "rails" : "rack"
web_xml = File.join( source, "#{flavour}-web.xml" )
FileUtils.cp( web_xml, target )
warn "created #{target.sub( /#{dir}\/?/, './' )}"
end
end

def rails?( dir )
File.exists? File.join( dir, 'config', 'application.rb' )
end

def config_dir( dir )
if File.exists?( File.join( dir, 'config' ) )
File.join( dir, 'config' )
else
dir
end
end

def target_dir( dir )
target = File.join( dir, 'target', 'jetty' )
unless File.exists?( target )
FileUtils.mkdir_p target
end
target
end

def ensure_keystore( dir )
target = File.join( target_dir( dir ), 'server.keystore' )
if !File.exists?( target ) && !File.exists?( File.join( dir, 'src', 'test', 'resources', 'server.keystore' ) )
store = File.join( File.dirname( __FILE__ ), "server.keystore" )
FileUtils.cp( store, target )
end
end

# def first_gemspec( dir = '.' )
# gemspecs = Dir[ File.join( dir, "*.gemspec" ) ]
# if gemspecs.size > 0
# File.expand_path( gemspecs[ 0 ] )
# end
# end

# def file( name, dir = '.' )
# File.expand_path( File.join( dir, name ) )
# end

# def pom_xml( dir = '.', proj, args )
# index = args.index( '-f' ) || args.index( '--file' )
# name = args[ index + 1 ] if index
# pom = File.join( dir, name || '.pom.xml' )
# File.open(pom, 'w') do |f|
# f.puts proj.to_xml
# end
# args += ['-f', pom] unless name
# args
# end

end
end
end

0 comments on commit 2822aff

Please sign in to comment.