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

Commit 83ccd95

Browse files
committed
Can now build a working JAR with and without executable options
1 parent 3ebf32f commit 83ccd95

File tree

8 files changed

+385
-84
lines changed

8 files changed

+385
-84
lines changed

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,72 @@ 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 directory to the list of GEM installation directories
48+
gemDir '/path/to/my/gemDir'
49+
50+
// Make the JAR executable and use the default main class
51+
defaultMainClass()
52+
53+
// Make the JAR executable by supplying your own main class
54+
mainClass 'my.own.main.'
55+
56+
// Equivalent to calling defaultGems() and defaultMainClass()
57+
defaults 'gem, 'mainClass'
58+
59+
}
60+
61+
// All other JAR methods and properties are still valid
62+
}
63+
64+
task myJar (type :Jar) {
65+
jruby {
66+
// As above
67+
}
68+
69+
// All other JAR methods and properties are still valid
70+
}
71+
72+
```
73+
74+
Using the default main class method `defaultMainClass()` will include class files from
75+
[warbler-bootstrap](https://github.com/jruby-gradle/warbler-bootstrap)

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.1-SNAPSHOT'
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.+'
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.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.github.jrubygradle
2+
3+
import groovy.transform.PackageScope
4+
import org.gradle.api.Project
5+
import org.gradle.api.tasks.bundling.Jar
6+
7+
/** Helper class to add extra methods to {@code Jar} tasks in order to add JRuby specifics.
8+
*
9+
* @author Schalk W. Cronjé
10+
*/
11+
class JRubyJarConfigurator {
12+
13+
static final String DEFAULT_MAIN_CLASS = 'com.lookout.jruby.JarMain'
14+
15+
// This is used by JRubyJarPlugin to configure Jar classes
16+
@PackageScope
17+
static void configureArchive(Jar archive,Closure c) {
18+
JRubyJarConfigurator configurator = new JRubyJarConfigurator(archive)
19+
Closure configure = c.clone()
20+
configure.delegate = configurator
21+
configure()
22+
}
23+
24+
@PackageScope
25+
static void afterEvaluateAction( Project project ) {
26+
project.tasks.withType(Jar) { t ->
27+
if(t.manifest.attributes.containsKey('Main-Class')) {
28+
if(t.manifest.attributes.'Main-Class' == JRubyJarConfigurator.DEFAULT_MAIN_CLASS) {
29+
t.with {
30+
from({project.configurations.jrubyEmbeds.collect {project.zipTree(it)}}) {
31+
include '**'
32+
exclude '**/WarMain.class'
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
40+
/** Adds a GEM installation directory
41+
*/
42+
void gemDir(File f) {
43+
gemDir(f.absolutePath)
44+
}
45+
46+
/** Adds a GEM installation directory
47+
* @param dir Source folder. Will be handled by {@code project.files(dir)}
48+
*/
49+
void gemDir(Object dir) {
50+
archive.with {
51+
from(dir) {
52+
include '**'
53+
exclude 'cache/**'
54+
exclude 'gems/*/test/**'
55+
}
56+
}
57+
}
58+
59+
/** Makes the JAR executable by setting a custom main class
60+
*
61+
* @param className Name of main class
62+
*/
63+
void mainClass(final String className) {
64+
archive.with {
65+
manifest {
66+
attributes 'Main-Class': className
67+
}
68+
}
69+
}
70+
71+
/** Sets the defaults
72+
*
73+
* @param defs A list of defaults. Currently {@code gems} and {@code mainClass} are the only recognised values.
74+
* Unrecognised values are silently discarded
75+
*/
76+
void defaults(final String... defs ) {
77+
defs.each { String it ->
78+
switch(it) {
79+
case 'gems':
80+
case 'mainClass':
81+
"default${it.capitalize()}"()
82+
}
83+
}
84+
}
85+
86+
/** Loads the default GEM installation directory
87+
*
88+
*/
89+
void defaultGems() {
90+
gemDir({archive.project.jruby.gemInstallDir})
91+
}
92+
93+
/** Makes the executable by adding a default main class
94+
*
95+
*/
96+
void defaultMainClass() {
97+
mainClass(DEFAULT_MAIN_CLASS)
98+
}
99+
100+
101+
private JRubyJarConfigurator(Jar a) {
102+
archive = a
103+
}
104+
105+
private Jar archive
106+
}

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
package com.github.jrubygradle
22

3+
import org.gradle.api.Plugin
4+
import org.gradle.api.Project
5+
import org.gradle.api.tasks.bundling.Jar
6+
37
/**
4-
* Created by schalkc on 27/08/2014.
8+
* @author Schalk W. Cronjé
59
*/
6-
class JRubyJarPlugin {
10+
class JRubyJarPlugin implements Plugin<Project> {
711
void apply(Project project) {
8-
// MIGHT NEED: project.apply plugin: 'java', 'java-base'
912

10-
project.configurations.create(JRubyWar.JRUBYWAR_CONFIG)
13+
project.apply plugin : 'com.github.jruby-gradle.base'
1114

12-
// TODO: Should probably check whether it exists before creating it
13-
project.configurations {
14-
jrubyEmbeds
15-
}
15+
project.configurations.maybeCreate('jrubyEmbeds')
16+
project.configurations.maybeCreate('testGems')
17+
project.configurations.maybeCreate('runtimeGems')
1618

1719
project.dependencies {
18-
jrubyEmbeds group: 'com.lookout', name: 'warbler-bootstrap', version: '1.+'
20+
jrubyEmbeds group: 'com.lookout', name: 'warbler-bootstrap', version: '1.0.0'
1921
}
2022

2123
// TODO: This will depend on which plugin we pull in
@@ -28,12 +30,16 @@ class JRubyJarPlugin {
2830
// dependsOn 'jrubyPrepareGems'
2931
// }
3032

31-
project.task('jrubyJar', type: JRubyJar) {
32-
group JRubyPlugin.TASK_GROUP_NAME
33-
dependsOn project.tasks.jrubyPrepare
34-
dependsOn project.tasks.classes
35-
}
3633

34+
if(!Jar.metaClass.respondsTo(Jar.class,'jruby',Closure)) {
35+
Jar.metaClass.jruby = { Closure extraConfig ->
36+
JRubyJarConfigurator.configureArchive(delegate,extraConfig)
37+
}
38+
}
39+
40+
project.afterEvaluate {
41+
JRubyJarConfigurator.afterEvaluateAction(project)
42+
}
3743
}
3844

3945

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=com.github.jrubygradle.JRubyJarPlugin

0 commit comments

Comments
 (0)