Maven: Creating deployable distribution: Part 1: Using the Appassembler plugin

Posted by {"name"=>"Palash Ray", "email"=>"paawak@gmail.com", "url"=>"https://www.linkedin.com/in/palash-ray/"} on August 12, 2014 · 2 mins read

Context

Often, we have a standalone Java application, from which we want to create a deployable distribution having all the dependencies in one place and with a shell script or bat file which can be then invoked from the command prompt. The Mojo Appassembler plugin helps do just that. We will be using the maven-distribution-example as reference. This project would need the project rmi-service-api to compile.

Using the Appassembler plugin

We have created a profile called all-in-one which can be called to create our distribution:

		
			all-in-one
			
				
					
						org.codehaus.mojo
						appassembler-maven-plugin
						1.8.1
						
							flat
							lib
							true
							
								unix
								windows
							
							
								
									com.swayam.demo.rmi.server.core.SpringNonSecureRmiServer
									run_rmi_server
								
							
						
						
							
								package
								
									assemble
								
							
						
					
					
					
						org.apache.maven.plugins
						maven-assembly-plugin
						2.4
						
							src/assembly/all-in-one-assembly-descriptor.xml
						
						
							
								assemble-all
								package
								
									single
								
							
						
					
				
			
		

This profile contains 2 parts:

  1. The appassembler plugin
    1. Collects all the maven dependencies in the target/appassembler/lib
    2. Generates the  .sh and .bat scripts to invoke the main class which is provided
  2. The maven assembly plugin which creates a tar archive from
    1. The appassembler directory
    2. readme.txt file

The maven assembly plugin works using the below assembly descriptor (all-in-one-assembly-descriptor.xml):


	all-in-one
	
		tar
	
	false
	
		
			${project.basedir}/src/static
			/
			
				readme.txt
			
		
		
			${project.build.directory}/appassembler
			/
			
				**/**
			
		
	

Next, we invoke Maven by:

mvn clean package -P all-in-one

Go to the tar file is generated in the target directory. You will find two directories inside the tar:

  1. bin: This contains the shell script and the bat file
  2. lib: This has all the dependant jars

You can now run the Rmi Server by invoking the shell or bat scripts from the prompt.
The sources can be found here: https://github.com/paawak/blog/tree/master/code/maven-ant-assembly-example