Embed external JARs in Groovy project - groovy

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?

Related

Packaging a Groovy application

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.

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

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.

Java ME bundle Microlog jars with my midlet jar

I'm pretty new to Java ME and i'm trying to use Microlog to handle logging in my midlet.
In eclipse i can reference Microlog jars and it's all good in dev time,
but when i try to launch the application the jars aren't found on the device.
How can i bundle those referenced libs into my midlet?
Someone have any experience with using Microlog in a midlet?
Thanks.
The classes are not found, because they are not inside the final MIDlet suite you are trying to run run in the device. In fact, you can't add external jars directly inside your midlet jar file as J2ME class loader is much more simple than the one in desktop Java. It only loads the classes found inside the midlet jar.
So, you have to extract everything from the Microlog jar and put them into you midlet jar just like you put your own classes and other resources. Probably you can configure your build tool to do this automatically for you.
Eclipse is able to find the classes because you have added the jar into your project's classpath.
Just a little addition. If you are using Eclipse to export the midlet you are able to generate the jar-file including all the classes of the dependencies for you.
Select "Java Build Path" of your project and then "Order and Export". Make sure the microlog jar-files are checked in that list. This will bundle everything into one jar-file.
Hope this helps.
I would recommend to use ProGuard to reduce the size of the final jar. Please read this article to figure out how to do it with Microlog:
http://myossdevblog.blogspot.com/2009/06/using-proguard-to-shrink-microlog.html

Resources