It seems like the GlassFish and Grails integration article got some attention, mainly because it showed how to reduce WAR file size from 17 MB down to 200 KB using GlassFish integrated Grails support.
This article shows how to reduce Grails WAR file size for deployment in other containers. The instructions should work for most application servers.
Most of the WAR size (about 95%) comes from the libraries that are included by default in the file. Reducing size is a matter of striping the JARs from the deployment file and relying on shared libraries in the server.
First, we need to understand where those libraries come from. The first source of library files is Grails itself. Grails JAR files are located in $GRAILS_HOME/dist and account for about 2 MB. The second and most important source are Grails dependencies in $GRAILS_HOME/lib. Those can take up to 25 MB.
Other sources of libraries are plug-ins you might have installed with the install-plugin command or additional libraries such as JDBC drivers in your application lib/ directory.
Copy all JAR files to the shared libraries directory of your application server. If you use Tomcat, for instance, this usually is the $CATALINA_HOME/shared/lib directory:
cp $GRAILS_HOME/dist/*.jar $CATALINA_HOME/shared/lib cp $GRAILS_HOME/lib/*.jar $CATALINA_HOME/shared/lib cp plugins/*/lib/*.jar $CATALINA_HOME/shared/lib cp lib/*.jar $CATALINA_HOME/shared/lib
Now it is time to tell Grails not to include dependencies in the WAR file. Open grails-app/conf/Config.groovy and add this line:
grails.war.dependencies = {}
This removes most libraries from the WAR file, except for Grails itself. File size is reduced to about 2 MB. Part 2 of this article will show how to get rid of Grails JAR files.
Container Specific Information
Tomcat 5.5
The instructions work as showed for Tomcat 5.5.
Tomcat 6.0
Tomcat 6.0 does not come with the shared libraries directory enabled by default. First, create the directory:
mkdir -p $CATALINA_HOME/shared/lib
And modify the shared.loader= line in $CATALINA_HOME/conf/catalina.properties to:
shared.loader=${catalina.base}/shared/lib/*.jar
If you have additional information about other application servers, please post it in the comments below.
Friday, November 14, 2008 at 5:51 am
hmmm…. part 2 pronto!!! please… :)
Friday, November 14, 2008 at 8:14 am
that’s great .. i will try it at home . thx
Monday, November 24, 2008 at 5:47 am
Are you planning on an article that will show how to remove the Grails jars and also maybe the plugin jars??? Thanks
Tuesday, November 25, 2008 at 4:05 pm
@john
I’m sorry for the delay. I’ve been really busy the last couple of weeks working on my thesis experiment. Please, check for part 2 next week.
Thursday, April 9, 2009 at 11:23 am
Thank you! This is an easy to make change, and so useful. Saves so much SCP traffic!! And all my grails apps can share the same JAR files.
Thanks!