Skip to content
This repository was archived by the owner on May 19, 2019. It is now read-only.

Commit 3335514

Browse files
author
R. Tyler Croy
committed
Merge pull request #4 from ysb33r/master
jruby-gradle-jar-plugin is walking upright
2 parents f054671 + b5e5fd6 commit 3335514

File tree

9 files changed

+440
-110
lines changed

9 files changed

+440
-110
lines changed

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,85 @@ jruby-gradle-jar-plugin
44
[![Build Status](https://buildhive.cloudbees.com/job/jruby-gradle/job/jruby-gradle-jar-plugin/badge/icon)](https://buildhive.cloudbees.com/job/jruby-gradle/job/jruby-gradle-jar-plugin/) [![Gitter chat](https://badges.gitter.im/jruby-gradle/jruby-gradle-plugin.png)](https://gitter.im/jruby-gradle/jruby-gradle-plugin)
55

66
Plugin for creating JRuby-based java archives
7+
8+
9+
## Compatilibity
10+
11+
This plugin requires Gradle 2.0 or better.
12+
13+
## Bootstrap
14+
15+
To add the plugin to your project
16+
```groovy
17+
buildscript {
18+
repositories {
19+
jcenter()
20+
}
21+
22+
dependencies {
23+
classpath group: 'com.github.jrubygradle', name: 'jruby-gradle-jar-plugin', version: '0.1.1'
24+
}
25+
}
26+
27+
apply plugin: 'com.github.jrubygradle.jar'
28+
```
29+
30+
## Implicit loaded plugins
31+
32+
This loads the following plugins if they are not already loaded:
33+
+ `com.github.jrubygradle.base`
34+
35+
## Using the plugin
36+
37+
This plugin does not add any new tasks or extensions, extends the `Jar` task type with a `jruby` closure. If the `java` plugin
38+
is loaded, then the `jar` task can also be configured.
39+
40+
```groovy
41+
jar {
42+
jruby {
43+
44+
// Use the default GEM installation directory
45+
defaultGems()
46+
47+
// Add this GEM installation directory to the JAR.
48+
// Can be called more than once for additional directories
49+
gemDir '/path/to/my/gemDir'
50+
51+
// Make the JAR executable and use the default main class
52+
defaultMainClass()
53+
54+
// Make the JAR executable by supplying your own main class
55+
mainClass 'my.own.main.'
56+
57+
// Equivalent to calling defaultGems() and defaultMainClass()
58+
defaults 'gem, 'mainClass'
59+
60+
}
61+
62+
// All other JAR methods and properties are still valid
63+
}
64+
65+
task myJar (type :Jar) {
66+
jruby {
67+
// As above
68+
}
69+
70+
// All other JAR methods and properties are still valid
71+
}
72+
73+
```
74+
75+
Using the default main class method `defaultMainClass()` will include class files from
76+
[warbler-bootstrap](https://github.com/jruby-gradle/warbler-bootstrap)
77+
78+
## Controlling the version of warbler-bootstrap
79+
80+
By default the version is set to `1.+` meaning anything version 1.0 or beyond. If your project wants to lock
81+
down the specific version, then it can be set via
82+
83+
```groovy
84+
jruby {
85+
warblerBootstrapversion = '1.0.0'
86+
}
87+
```
88+

build.gradle

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,43 @@ sourceCompatibility = '1.7'
1818
repositories {
1919
jcenter()
2020
mavenLocal()
21+
maven { url 'http://dl.bintray.com/rtyler/jruby' }
22+
}
23+
24+
configurations {
25+
testWarbler
2126
}
2227

2328
dependencies {
2429
compile gradleApi()
2530
compile localGroovy()
31+
compile 'com.github.jruby-gradle:jruby-gradle-plugin:0.1.2'
2632

2733
testCompile ("org.spockframework:spock-core:0.7-groovy-${gradle.gradleVersion.startsWith('1.')?'1.8':'2.0'}") {
2834
exclude module : 'groovy-all'
2935
}
36+
testWarbler group: 'com.lookout', name: 'warbler-bootstrap', version: '1.0.0'
37+
38+
}
39+
40+
ext {
41+
testWarblerDir = new File(buildDir,'tmp/test/repo' )
3042
}
3143

3244
test {
3345
testLogging {
3446
showStandardStreams = true
3547
exceptionFormat "full"
3648
}
49+
50+
doFirst {
51+
copy {
52+
from project.configurations.testWarbler.files
53+
into testWarblerDir
54+
}
55+
}
56+
systemProperties TESTROOT : new File(buildDir,'tmp/test/unittests').absolutePath
57+
systemProperties WARBLER_LOCATION : testWarblerDir.absolutePath
3758
}
3859

3960
task sourcesJar(type: Jar, dependsOn: classes) {

src/main/groovy/com/github/jrubygradle/JRubyJar.groovy

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/main/groovy/com/github/jrubygradle/JRubyJarPlugin.groovy

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.github.jrubygradle.jar
2+
3+
import groovy.transform.PackageScope
4+
import org.gradle.api.Project
5+
import org.gradle.api.tasks.bundling.Jar
6+
import com.github.jrubygradle.GemUtils
7+
8+
/** Helper class to add extra methods to {@code Jar} tasks in order to add JRuby specifics.
9+
*
10+
* @author Schalk W. Cronjé
11+
*/
12+
class JRubyJarConfigurator {
13+
14+
static final String DEFAULT_MAIN_CLASS = 'com.lookout.jruby.JarMain'
15+
16+
// This is used by JRubyJarPlugin to configure Jar classes
17+
@PackageScope
18+
static void configureArchive(Jar archive,Closure c) {
19+
JRubyJarConfigurator configurator = new JRubyJarConfigurator(archive)
20+
Closure configure = c.clone()
21+
configure.delegate = configurator
22+
configure()
23+
}
24+
25+
@PackageScope
26+
static void afterEvaluateAction( Project project ) {
27+
project.tasks.withType(Jar) { t ->
28+
if(t.manifest.attributes.containsKey('Main-Class')) {
29+
if(t.manifest.attributes.'Main-Class' == JRubyJarConfigurator.DEFAULT_MAIN_CLASS) {
30+
t.with {
31+
from({project.configurations.jrubyEmbeds.collect {project.zipTree(it)}}) {
32+
include '**'
33+
exclude '**/WarMain.class'
34+
}
35+
}
36+
}
37+
}
38+
}
39+
}
40+
41+
/** Adds a GEM installation directory
42+
*/
43+
void gemDir(def properties=[:],File f) {
44+
gemDir(properties,f.absolutePath)
45+
}
46+
47+
/** Adds a GEM installation directory
48+
* @param Properties that affect how the GEM is packaged in the JAR. Currently only {@code fullGem} is
49+
* supported. If set the full GEM content will be packed, otherwise only a subset will be packed.
50+
* @param dir Source folder. Will be handled by {@code project.files(dir)}
51+
*/
52+
void gemDir(def properties=[:],Object dir) {
53+
archive.with GemUtils.gemCopySpec(properties,archive.project,dir)
54+
}
55+
56+
/** Makes the JAR executable by setting a custom main class
57+
*
58+
* @param className Name of main class
59+
*/
60+
void mainClass(final String className) {
61+
archive.with {
62+
manifest {
63+
attributes 'Main-Class': className
64+
}
65+
}
66+
}
67+
68+
/** Sets the defaults
69+
*
70+
* @param defs A list of defaults. Currently {@code gems} and {@code mainClass} are the only recognised values.
71+
* Unrecognised values are silently discarded
72+
*/
73+
void defaults(final String... defs ) {
74+
defs.each { String it ->
75+
switch(it) {
76+
case 'gems':
77+
case 'mainClass':
78+
"default${it.capitalize()}"()
79+
}
80+
}
81+
}
82+
83+
/** Loads the default GEM installation directory
84+
*
85+
*/
86+
void defaultGems() {
87+
gemDir({archive.project.jruby.gemInstallDir})
88+
}
89+
90+
/** Makes the executable by adding a default main class
91+
*
92+
*/
93+
void defaultMainClass() {
94+
mainClass(DEFAULT_MAIN_CLASS)
95+
}
96+
97+
98+
private JRubyJarConfigurator(Jar a) {
99+
archive = a
100+
}
101+
102+
private Jar archive
103+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.github.jrubygradle.jar
2+
3+
import com.github.jrubygradle.internal.WarblerBootstrap
4+
import org.gradle.api.Plugin
5+
import org.gradle.api.Project
6+
import org.gradle.api.Task
7+
import org.gradle.api.tasks.bundling.Jar
8+
import org.gradle.api.tasks.testing.Test
9+
10+
/**
11+
* @author Schalk W. Cronjé
12+
*/
13+
class JRubyJarPlugin implements Plugin<Project> {
14+
void apply(Project project) {
15+
16+
project.apply plugin : 'com.github.jruby-gradle.base'
17+
project.configurations.maybeCreate('jrubyEmbeds')
18+
19+
// In order to update the testing cycle we need to tell unit tests where to
20+
// find GEMs. We are assuming that if someone includes this plugin, that they
21+
// will be writing tests that includes jruby and that they might need some
22+
// GEMs as part of the tests.
23+
def testConfiguration = { Task t ->
24+
environment GEM_HOME : project.extensions.getByName('jruby').gemInstallDir
25+
dependsOn 'jrubyPrepareGems'
26+
}
27+
try {
28+
Task t = project.tasks.getByName('test')
29+
if( t instanceof Test) {
30+
project.configure(t,testConfiguration)
31+
}
32+
} catch(UnknownTaskException) {
33+
project.tasks.whenTaskAdded { Task t ->
34+
if(t.name == 'test' && t instanceof Test) {
35+
project.configure(t,testConfiguration)
36+
}
37+
}
38+
}
39+
try {
40+
Task t = project.tasks.getByName('jar')
41+
if( t instanceof Jar) {
42+
t.dependsOn 'jrubyPrepareGems'
43+
}
44+
} catch(UnknownTaskException) {
45+
project.tasks.whenTaskAdded { Task t ->
46+
if (t.name == 'jar' && t instanceof Jar) {
47+
t.dependsOn 'jrubyPrepareGems'
48+
}
49+
}
50+
}
51+
52+
if(!Jar.metaClass.respondsTo(Jar.class,'jruby',Closure)) {
53+
Jar.metaClass.jruby = { Closure extraConfig ->
54+
JRubyJarConfigurator.configureArchive(delegate,extraConfig)
55+
}
56+
}
57+
58+
project.afterEvaluate {
59+
WarblerBootstrap.addDependency(project)
60+
JRubyJarConfigurator.afterEvaluateAction(project)
61+
}
62+
}
63+
64+
65+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=com.github.jrubygradle.jar.JRubyJarPlugin

0 commit comments

Comments
 (0)