These are chat archives for jruby-gradle/jruby-gradle-plugin

15th
Mar 2017
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 16:47
Attempting to use the plugin to build an AWS Lambda jar using a mainClass. But I don't see in the documentation how to bundle the java file into the jar. Can anyone assist?
R. Tyler Croy
@rtyler
Mar 15 2017 19:47
@ryan-dyer-sp what do you mean bundle a java file into the jar? like compile it in?
have you used the java gradle plugin in conjunction with jruby-gradle?
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 20:55
@rtyler yes from what I can gather by looking at the plugin with my limited knowledge, I need to replace the mainClass (which defaults to the jruby-mains JarMain class) with my own which will effectively act as a 'wrapper' for JarMain
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 21:03
and no i wasn't using the java-gradle plugin. I'm guessing i need to do that in conjunction. I don't have any experience with gradle sorry.
R. Tyler Croy
@rtyler
Mar 15 2017 21:04
that would just make it sensible to build the .java file as the mainClass
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 21:09
ok so i use the java plugin to compile my replacement class, but still not sure how that gets bundled into the proj-jruby.jar file
and how do we get multiple ruby files bundled in it as well?
Blane Dabney
@raelik
Mar 15 2017 21:17
Sounds like you're wanting to combine a 'traditional' Java gradle project and a jruby one.
the way I did this was to have my java project as a subproject of my jruby project.
I used a settings.gradle file to make sure it was included in my main build.gradle.
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 21:20
ultimately i'm just trying to create an easy way to bundle ruby code into a jruby jar which can be used within aws lambda.
Blane Dabney
@raelik
Mar 15 2017 21:21
in my main (i.e. jruby) build.gradle, I defined an ivy repo in lib/repo, and in the subproject build.gradle, I set its uploadArchives config to publish to that ivy repo.
Then, I made the jrubyJar task depend on the subproject's uploadArchives task.
and then added the subproject's jar as a runtime dependency of the jruby project.
That way, it ended up in the main jar.
in your case, you could do what I did, and then redefine the main class of the jrubyJar to point to your subproject's main class.
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 21:24
ok, once i learn to speak gradle i'm sure that will make perfect sense :) Thanks!
Blane Dabney
@raelik
Mar 15 2017 21:24
how you then proceed to call your ruby code would be a bit non-standard though, as your AWS lambda entrypoint would need to spin up jRuby and load your ruby code.
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 21:25
I was thinking that would be something like:

import org.jruby.mains.JRubyMain;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class AWSLambdaJRuby implements RequestHandler<String, String>{
@Override
public String handleRequest(String input, Context context) {
JRubyMain.main(null, "uri:classloader:/", "uri:classloader://META-INF/jruby.home", input);
}

Blane Dabney
@raelik
Mar 15 2017 21:26
Oh I see, so you want to throw Ruby code at the lambda.
that might work
though I feel like you'd need to do something with that context too
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 21:28
just to be clear, i know 0 java or gradle, so if it doesn't make sense please feel free to enlighten
yeah i can stringify the context as well and pass it to my ruby code as second arg i assume, just trying to get the initial build process figured out to see if it is even possible or if we need to pursue another route
Blane Dabney
@raelik
Mar 15 2017 21:30
I don't think you can pass the Ruby code in as an argument like that though
that's actually a jruby argument
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 21:31
yeah i thought from looking at the code it was passed along to the ruby script as an argument
Blane Dabney
@raelik
Mar 15 2017 21:32
yeah, but it's expecting a filename.
the arguments there are literally the arguments that you would pass to the ruby command line.
you'd have to do some weird thing with a -e argument in front of that
OR
figure out how to pass the code as stdin to ruby
which I think you would do by instantiating a RubyInstanceConfig object
Blane Dabney
@raelik
Mar 15 2017 21:37
that constructor, in fact. You'd have to wrap your input string in an InputStream, like this new ByteArrayInputStream(input.getBytes("UTF-8"))
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 21:38
https://github.com/jruby/jruby-mains/blob/master/src/main/java/org/jruby/mains/JRubyMain.java doesn't that just use the bootstrap file and then append args?
Blane Dabney
@raelik
Mar 15 2017 21:39
yes, but I was assuming that you would want to pass in ruby code through the aws lambda
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 21:40
with ruby code itself being the lambda input?
Blane Dabney
@raelik
Mar 15 2017 21:40
I guess? I've never used AWS lambda
If you're just running code that's in the bootstrap, then disregard what I said about that.
That being said, the JRubyMain will try to send it's output to stdout
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 21:41
the lambda input/event is normally a json string that you operate on, i want that input to get passed ultimately to a ruby script
Blane Dabney
@raelik
Mar 15 2017 21:42
which doesn't seem like it would work with AWS lambda
if you don't care about what it's outputting, then you should be good
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 21:42
nope don't care about output
so back to the jruby-gradle-jar plugin. When I do jrubyJar { mainClass myClass } in my build.gradle and gradle jrubyJar .... what is my expectation for what this should do?
Blane Dabney
@raelik
Mar 15 2017 21:46
that class is already going to need to be compiled and available
you can either do that outside your jruby project, and just manually include it in some way
or you can go with the subproject route.
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 21:47
and where does the class file need to reside for it get packaged into the jruby jar?
Blane Dabney
@raelik
Mar 15 2017 21:47
Personally, I'd recommend just manually including it, since it doesn't look like it's going to change much if at all once you get it working.
it needs to be made available as a dependency
so you'd need to jar it up on its own, and put it in a repository that your gradle file is configured to see.
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 21:48
gotcha, sorry once again 0 gradle exp. and if our ruby project consists of multiple files, we would just add those as a dependency as well
Blane Dabney
@raelik
Mar 15 2017 21:49
I'd recommend an ivy repo, because it's like maven lite, with basically no overhead.
ryan-dyer-sp
@ryan-dyer-sp
Mar 15 2017 21:49
I think I see now, thanks
Blane Dabney
@raelik
Mar 15 2017 21:49
well, the ruby project needs to get packaged up
that I'm not exactly sure how you do with jrubyJar.
I've only ever used jrubyJar to make artifact jars.
(i.e. jRuby itself, all of the gems and jar dependencies)
I believe you just stick a from '<dir>' in your jrubyJar configuration in the build.gradle file.
that way it will put all of the files in <dir> in the jar.
Everything I've done with jRuby and jruby-gradle has been a standalone daemon, so all my project's ruby files live on the actual filesystem.
but that's not typical.