Last week, I re-vitalized a six year old struts web application, I once wrote for maintaining my music collection. It was neccessiary, since with the last Ubuntu upgrade, many changes to my system were made, among them a new PostgreSQL version.
Because of that, an annoying bug, caused by an old driver jar, came up and wanted to be fixed.
The application was created long ago with ant as build tool and manual dependeny management. Updating the jars was not really funny, working on the code was also pretty easy, but there was no real reliable build and installation process for the new webapp war file.
Since I’m using gradle on my daily job, the decision was soon made to transform my project into a nice gradle project, and I was really fascinated, how fast and easy the transition was. After 30 minutes, I got a clean gradle project, which not only does a proper and reliable dependency management, but also builds now in a reliable way.
Setting up the build.gradle file was so easy – here are the most important parts:
apply plugin: ‘java’
apply plugin: ‘war’
Instruct gradle, that we have a Java project here and want to get a war file as artifact
repositories {
mavenCentral()
}
All dependencies will be resolved by jars from the MavenCentral repository. That’s pretty standard, and unless you host your own repository, you can just keep it that way
dependencies {
compile group: ‘commons-beanutils’, name: ‘commons-beanutils’, version: ‘1.9.1’
compile group: ‘commons-collections’, name: ‘commons-collections’, version: ‘3.2.1’…
This is the actual list of first-level dependencies. All transitive dependencies of these jars are resolved automatically!
war {
from ‘WebRoot’ // adds a file-set to the root of the archive
}
In my application, there are some static jsps in a subdirectory called “WebRoot”. The Java source resides by default in src/main/java
task deployToTomcat(type: Copy) { from war
into “/opt/tomcat/webapps”
}
This is the last step, which just deploys the war artifact into the tomcat.
Now, with the magical line “gradle clean build war deployToTomcat” my whole application is build and re-installed into my tomcat, which (by default) automatically updates the web application.
Pretty easy and straightforward, isn’t it?