Skip to content

Commit 54f4533

Browse files
author
R. Tyler Croy
committed
Refactor some code out of JRubyPlugin and create a JRubyWar task
Fixes #24
1 parent 75eca76 commit 54f4533

File tree

4 files changed

+110
-41
lines changed

4 files changed

+110
-41
lines changed

src/main/groovy/com/lookout/jruby/JRubyPlugin.groovy

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,68 @@ import org.gradle.api.tasks.Delete
88
import org.gradle.api.tasks.bundling.War
99

1010
class JRubyPlugin implements Plugin<Project> {
11+
static final String TASK_GROUP_NAME = 'JRuby'
12+
13+
static final String RUBYGEMS_RELEASE_URL = 'http://rubygems-proxy.torquebox.org/releases'
14+
1115
void apply(Project project) {
1216
project.apply plugin: 'java'
1317
project.apply plugin: 'war'
1418
project.extensions.create('jruby', JRubyPluginExtension, project)
1519

16-
if(!project.repositories.metaClass.respondsTo(project.repositories,'rubygemsRelease')) {
20+
if (!project.repositories.metaClass.respondsTo(project.repositories, 'rubygemsRelease')) {
1721
project.repositories.metaClass.rubygemsRelease << { ->
18-
maven { url 'http://rubygems-proxy.torquebox.org/releases' }
22+
maven { url RUBYGEMS_RELEASE_URL }
1923
}
2024
}
2125

2226
// Set up a special configuration group for our embedding jars
2327
project.configurations {
2428
jrubyEmbeds
25-
jrubyWar
2629
gems
2730
}
2831

2932
project.configurations.create(JRubyExec.JRUBYEXEC_CONFIG)
33+
project.configurations.create(JRubyWar.JRUBYWAR_CONFIG)
3034
JRubyExecDelegate.addToProject(project)
3135

3236
// In order for jrubyWar to work we'll need to pull in the warbler
3337
// bootstrap code from this artifact
3438
project.afterEvaluate {
35-
if(project.jruby.defaultRepositories) {
39+
if (project.jruby.defaultRepositories) {
3640
project.repositories {
3741
jcenter()
3842
rubygemsRelease()
3943

4044
// Required to pull in our warbler-bootstrap dependency
4145
maven { url 'http://dl.bintray.com/rtyler/jruby' }
42-
4346
}
4447
}
48+
4549
project.dependencies {
4650
jrubyEmbeds group: 'com.lookout', name: 'warbler-bootstrap', version: '1.+'
47-
jrubyWar group: 'org.jruby', name: 'jruby-complete', version: project.jruby.defaultVersion
48-
jrubyWar (group: 'org.jruby.rack', name: 'jruby-rack', version: '1.1.+') {
49-
exclude module : 'jruby-complete'
50-
}
5151
}
5252

5353
JRubyExec.updateJRubyDependencies(project)
54+
JRubyWar.updateJRubyDependencies(project)
5455
}
5556

5657
project.task('jrubyClean', type: Delete) {
57-
group 'JRuby'
58+
group TASK_GROUP_NAME
5859
description 'Clean up the temporary dirs used by the JRuby plugin'
5960
mustRunAfter 'clean'
6061
delete '.jarcache/'
6162
}
6263

6364
project.task('jrubyPrepareGems', type: JRubyPrepareGems) {
64-
group 'JRuby'
65+
group TASK_GROUP_NAME
6566
description 'Prepare the gems from the `gem` dependencies, extracts into jruby.installGemDir'
6667
gems project.configurations.gems
6768
outputDir project.jruby.gemInstallDir
6869
}
6970

7071
project.task('jrubyCacheJars', type: Copy) {
71-
group 'JRuby'
72+
group TASK_GROUP_NAME
7273
description 'Cache .jar-based dependencies into .jarcache/'
7374

7475
from project.configurations.jrubyWar
@@ -77,38 +78,12 @@ class JRubyPlugin implements Plugin<Project> {
7778
}
7879

7980
project.task('jrubyPrepare') {
80-
group 'JRuby'
81+
group TASK_GROUP_NAME
8182
description 'Pre-cache and prepare all dependencies (jars and gems)'
8283
dependsOn project.tasks.jrubyCacheJars, project.tasks.jrubyPrepareGems
8384
}
8485

85-
project.task('jrubyWar', type: War) {
86-
group 'JRuby'
87-
description 'Create a executable JRuby-based web archive'
88-
dependsOn project.tasks.jrubyPrepare
89-
90-
from "$project.buildDir/classes/main"
91-
// Bring our vendored gems into the created war file
92-
webInf {
93-
from project.jruby.gemInstallDir
94-
into 'gems'
95-
}
96-
97-
// Bring the jrubyWar configuration's dependencies into
98-
// WEB-INF/libs
99-
classpath project.configurations.jrubyWar
100-
101-
// note that zipTree call is wrapped in closure so that configuration
102-
// is only resolved at execution time. This will take the embeds
103-
// from within the `jrubyEmbeds` configuration and dump them into the war
104-
from {
105-
project.configurations.jrubyEmbeds.collect { project.zipTree(it) }
106-
}
107-
108-
// By adding the WarMain class as the main-class we can have a
109-
// runnable war
110-
manifest { attributes 'Main-Class' : 'com.lookout.jruby.WarMain' }
111-
}
86+
project.task('jrubyWar', type: JRubyWar)
11287
}
11388

11489
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.lookout.jruby
2+
3+
import org.gradle.api.Project
4+
import org.gradle.api.tasks.bundling.War
5+
6+
/**
7+
*
8+
* @author R. Tyler Croy
9+
*/
10+
class JRubyWar extends War {
11+
12+
static final String mainClass = 'com.lookout.jruby.WarMain'
13+
static final String JRUBYWAR_CONFIG = 'jrubyWar'
14+
15+
JRubyWar() {
16+
super()
17+
group JRubyPlugin.TASK_GROUP_NAME
18+
description 'Create a JRuby-based web archive'
19+
dependsOn project.tasks.jrubyPrepare
20+
21+
// Bring in any compiled classes from our project
22+
from "$project.buildDir/classes/main"
23+
24+
// Bring our vendored gems into the created war file
25+
webInf {
26+
from project.jruby.gemInstallDir
27+
into 'gems'
28+
}
29+
30+
// Bring the jrubyWar configuration's dependencies into
31+
// WEB-INF/libs
32+
classpath project.configurations.jrubyWar
33+
34+
// note that zipTree call is wrapped in closure so that configuration
35+
// is only resolved at execution time. This will take the embeds
36+
// from within the `jrubyEmbeds` configuration and dump them into the war
37+
from {
38+
project.configurations.jrubyEmbeds.collect {
39+
project.zipTree(it)
40+
}
41+
}
42+
43+
// By adding the WarMain class as the main-class we can have a
44+
// runnable war
45+
manifest {
46+
attributes 'Main-Class' : mainClass
47+
}
48+
}
49+
50+
/** Update dependencies on the project to include those necessary for
51+
* building JRubyWar's
52+
*/
53+
static void updateJRubyDependencies(Project project) {
54+
project.dependencies {
55+
jrubyWar group: 'org.jruby', name: 'jruby-complete', version: project.jruby.defaultVersion
56+
jrubyWar (group: 'org.jruby.rack', name: 'jruby-rack', version: '1.1.+') {
57+
exclude module : 'jruby-complete'
58+
}
59+
}
60+
}
61+
}

src/test/groovy/com/lookout/jruby/JRubyExecSpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class JRubyExecSpec extends Specification {
2828
project.buildDir = TESTROOT
2929
project.logging.level = LIFECYCLE
3030
project.apply plugin: 'com.lookout.jruby'
31-
execTask = project.task(TASK_NAME,type: JRubyExec)
31+
execTask = project.task(TASK_NAME, type: JRubyExec)
3232
}
3333

3434
def "Do not allow JRubyExec to be instantiated if plugin has not been loaded"() {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.lookout.jruby
2+
3+
import org.gradle.testfixtures.ProjectBuilder
4+
import spock.lang.*
5+
import static org.gradle.api.logging.LogLevel.*
6+
7+
/**
8+
* @author R. Tyler Croy
9+
*
10+
*/
11+
class JRubyWarSpec extends Specification {
12+
static final File TEST_SCRIPT_DIR = new File( System.getProperty('TEST_SCRIPT_DIR') ?: 'src/test/resources/scripts')
13+
static final File TESTROOT = new File(System.getProperty('TESTROOT') ?: 'build/tmp/test/unittests')
14+
static final String TASK_NAME = 'WarWarTask'
15+
16+
def project
17+
def warTask
18+
19+
void setup() {
20+
project = ProjectBuilder.builder().build()
21+
project.buildDir = TESTROOT
22+
project.logging.level = LIFECYCLE
23+
project.apply plugin: 'com.lookout.jruby'
24+
warTask = project.task(TASK_NAME, type: JRubyWar)
25+
26+
}
27+
28+
def "basic sanity check"() {
29+
expect: "warTask to be an instance"
30+
warTask instanceof JRubyWar
31+
warTask.group == 'JRuby'
32+
}
33+
}

0 commit comments

Comments
 (0)