How to Build an Executable War or Jar Files

Posted by admin on March 26, 2014  /   Posted in J2EE, Web Development


In the effort to bend-over-backwards for our clients, we sometimes have to take a step back and think outside of the box.

99% of our Custom System are deployed as a web applications whether it is hosted on our server or the client’s. But for this particular client, they require a way to deploy the application on an isolated machine.  So we had to improvise a way to package a webserver *and* the application into something that can be donwloaded, and run without any complicated setup.

Sharing our Findings

So here is a simple way I found to make my maven webapp project into a self-executing jar file. Glad to share it for all to take advantage of. This has the obvious advantage of not having to setup a Tomcat server on each client that will use the application. And since an executable .jar files are OS independent, you can use this whether you are a Windows or UNIX shop.

1) Client needs to have Java installed
2) Your project must have a packaging pom or war.
3) Your web app should already be working and can compile without error.
4) This is only supported with the Tomcat7 plug-in

Now go into your pom.xml file and add the plug-in with your other plug-ins.

Here’s the code for the Maven plug-in:

 <packaging>war or pom</packaging>
 <!-- optional only if you want to use a preconfigured server.xml file -->
 <!-- optional values which can be configurable -->
 <attachArtifactClassifier>default value is exec-war but you can customize</attachArtifactClassifier>
 <attachArtifactClassifierType>default value is jar</attachArtifactClassifierType>

This plug-in code is from the Apache site. I usually remove the server.xml option and I will set the ArtifactClassifier and ArtifactClassifierType to ‘exec-war’ and ‘jar’ respectively. But it should work will all the optional tags removed.

After you have your pom.xml file saved, drill your command line to folder that has the pom.xml file and run the following command:

mvn clean package -Prunnable-war

Things to note with this command.

  1. There is no space between the ‘-P’ argument and the name of the profile
  2. The ‘runnable-war’ is just a generic profile name. You will use the name of your profile that is in the pom.xml file. If you have more than one build then select the most appropriate. I usually have a ‘dev’, ‘test’ and ‘prod’ profiles. I typically use the ‘dev’

Once the command has been completed 3 new files will be created.

  • ${yourapp-version-war-exec}.jar: A runnable JAR that contains the tomcat embedded runtime
  • war.exec.manifest: a manifest file containing the main class to run
  • war-exec.preperties: a properties file containing some Tomcat config options and info.

NOTE: If your project has multiple modules then these 3 new files will be created in each folder.
For example, I typically have 5 folders for my web apps.

 ---- my-project-common
 ---- my-project-engine
 ---- my-project-test
 ---- my-project-web

I create the jar with the parent pom.xml, but the 3 new files that I will use are going where my web app is. (‘my-project-web’)

So for the final step, go into the target folder of your web app (‘my-project-web’ in my case) and run the following command to start the Tomcat server:

java -jar ${yourapp-version-war-exec}.jar

Open up a browser and go to http://localhost:8080 and your app should be there.
First run is usually slow to start because of all the extraction that happens.

Now you can simply copy these three files to another computer that has Java and start you web app with the same command.

Esteban Martinez
Senior Developer

A good Spring book

Posted by admin on April 28, 2010  /   Posted in J2EE

Book review: Pro Spring 2.5
Click here to go to the Publisher’s link.

Spring 2.5

I had the pleasure of reading through this book — yes all 1,000,000 pages in it — at my favorite Barnes & Nobles bookstore while preparing for a session. One and a half hour later, I find myself strangely educated.

Despite the bad reviews that I read about this book, I think it strikes just the right level of complexity and I appreciate the realistic problems that is tackled throughout the book. Such as how to measure the performance of your Hibernate layer via JMX.

This book is perfect for those who would like to have it as a reference that you can flip back and forth depending on which section of Spring Framework you’re tackling at the moment.

We serve businesses of any type and size
Contact Us Today