Packaging a Groovy application - groovy

I want to package a Groovy CLI application in a form that's easy to distribute, similar to what Java does with JARs. I haven't been able to find anything that seems to be able to do this. I've found a couple of things like this that are intended for one-off scripts, but nothing that can compile an entire Groovy application made up of a lot of separate Groovy files and resource data.
I don't necessarily need to have the Groovy standalone executable be a part of it (though that would be nice), and this is not a library intended to be used by other JVM languages. All I want is a simply packaged version of my application.
EDIT:
Based on the couple of responses I got, I don't think I was being clear enough on my goal. What I'm looking for is basically a archive format that Groovy can support. The goal here is to make this easier to distribute. Right now, the best way is to ZIP it up, have the user unzip it, and then modify a batch/shell file to start it. I was hoping to find a way to make this more like an executable JAR file, where the user just has to run a single file.
I know that Groovy compiles down to JVM-compatible byte-code, but I'm not trying to get this to run as Java code. I'm doing some dynamic addition of Groovy classes at runtime based on the user's configuration and Java won't be able to handle that. As I said in the original post, having the Groovy executable is included in the archive is kind of a nice-to-have. However, I do actually need Groovy to be executable that runs, not Java.

The Gradle Cookbook shows how to make a "fat jar" from a groovy project: http://wiki.gradle.org/display/GRADLE/Cookbook#Cookbook-Creatingafatjar
This bundles up all the dependencies, including groovy. The resulting jar file can be run on the command line like:
java -jar myapp.jar

I've had a lot of success using a combination of the eclipse Fat Jar plugin and Yet Another Java Service Wrapper.
Essentially this becomes a 'Java' problem not a groovy problem. Fat Jar is painless to use. It might take you a couple of tries to get your single jar right, but once all the dependencies are flattened into a single jar you are now off an running it at the command line with
java -jar application.jar
I then wrap these jars as a service. I often develop standalone groovy based services that perform some task. I set it up as a service on Windows server using Yet Another Java Service and schedule it using various techniques to interact with Windows services.

Related

Embed external JARs in Groovy project

I think I have some trouble understanding the concepts of the Groovy program structure...
I would like to write a program which has a dependency on an external JAR. I can use this JAR if I add it to Groovy's /lib folder. However I would like to create the program in such a way that others can use it including the JAR so they won't need to download it from the internet, place it somewhere, etc.
How can I include the JAR into my Groovy project?

How can I get external jars added to the classpath of a executable jar created by GroovyWrapper

I have a simple groovy script that executes some sql and plays with the results. It runs quite happily from Eclipse when I add the SQL Server jar to the classpath. However, I now wish to hand it over to a co-worker as an executable jar.
I found the (GroovyWrapper) script which works great, so long as the script doesn't have any extra dependencies.
I can put together all the jars manually and pass them via the -cp option, which works but you can't use -cp with -jar so I needed some other solution.
I tried adding an optional parameter to the GroovyWrapper script to embed SQL Server classes but that didn't work in the end as the SQL Server classes are signed and therefore can't be copied in.
I then tried to add a Class-Path manifest entry to point to the sqljdbc4.jar in the current directory. I have done similar things previously when creating standalone jars from java without issues but for some reason it still doesn't work.
I don't want to play around with fat jars, custom class loaders or the like if I can avoid it as the script is nice and simple at the moment.
Has anyone a solution? Have I missed something obvious.

Creating own groovy library

Do anyone have an idea whats the best way of creating an own library for groovy.
I have several methods which i just dont want copy and paste into all my groovy scripts.
The perfect solution would be to do it by an
import myownmethods
How to create the library myownmethods.jar?
Thanks for any answer and solution
Cheers
The simplest method is to compile your groovy files with groovyc and then package them into a jar file with jar. For example:
groovyc -d classes myclasses.groovy
jar cvf myclasses.jar -C classes .
I'd also consider looking at gradle. To get started, you can use a build.gradle containing just:
apply plugin: 'groovy'
Then put your source files in a subdirectory called src/main/groovy and run gradle jar. It will build your source files into a jar file in build/libs.
You should follow the same process that you would for a Java library, i.e.
Create a project for the code
Configure your favorite build tool (Ant, Maven, etc.) to build a JAR for that project
Put the JAR somewhere where other projects can find it. If you're using a tool like Ivy or Maven that does dependency management you'll likely want to deploy it to a repository. Otherwise, you can probably just put it somewhere in source control †
Projects that depend on this library should either load it from the repository (if using dependency management), or have it copied into their lib directory (if not) †
† I know this sucks, but I can't remember how I used to manage dependencies without using a dependency management tool

Grails and dynamic modifications at runtime

A deployable Grails war-File contains groovy files as well as a groovy.jar.
Is there a groovy agent running when the application is deployed?
Is it possible to make dynamic modifications for the application via Groovy at runtime?
if yes, how can this be prevented?
Edit:
After the grails application war is deployed on let's say on Tomcat: Is there some kind of Groovy Shell/Process/Agent running where someone who has access to the system can connect to? And if this is possible can he make dynamic modifications to the application without touching any files on the file system?
You can use the Console plugin which provides a web-based version of the Groovy Console/Grails Console. Like the Swing-based app it has a ctx variable in scope to give you access to all of the app's Spring beans, and it will run arbitrary Groovy code in the context of your running web app, can access GORM, etc.
Obviously this is dangerous, so be sure to guard it with a security plugin
I wrote a blog post a while back about using a similar web-based console (which I've since merged into the plugin) to fix a bug in a running server: http://burtbeckwith.com/blog/?p=155
By default Grails apps do not ship with a console that will let you execute arbitrary Groovy code at runtime.

how to ship groovy scripts?

How should I package and ship my groovy scripts without assuming groovy to be installed (and on PATH) on client machine? However, JDK/JRE will be available on all client machines.
What I'm currently doing is to groovy-compile & bundle related scripts in a jar with groovy-all-xxx.jar included in the lib (Netbeans does this automatically). But the problem with this approach is - with every independent small script (project), I have to bundle the huge groovy-all jar creating a big binary.
Just wanted to know if there is any better way of doing this.
You might be able to use the extension mechanism for this - that way each client machine would have to download groovy-all-xxx.jar just once.

Resources