Configuring Maven’s maven-assembly-plugin
October 27th, 2008Maven provides an excellent plugin for packaging your application into a binary file called the maven-assembly-plugin. The latest version is 2.3-beta, and should be used to avoid any issues with missing manifest settings in the pom.xml. In my case, i needed to create an executable jar file from my eclipse project that is manage by m2eclipse
To use the maven-assemlby-plugin to create a jar, your pom.xml file should look like so. Note that i’ve commented out the <descriptorRef> element because I am defining a descriptor file that the plugin will reference using the more versatile <descriptors> element. The assembly-descriptor.xml file it needs to understand how to execute my desired jar-with-dependencies task is referenced as <descriptor> child element:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-3-SNAPSHOT</version>
<configuration>
<descriptors>
<descriptor>/assembly-descriptor.xml</descriptor>
</descriptors>
<archive>
<manifest>
<addClasspath>false</addClasspath>
<mainClass>
com.voyager.VCATApplication
</mainClass>
</manifest>
<manifestEntries>
<mode>development</mode>
<SplashScreen-Image>
vcat-splash.jpg
</SplashScreen-Image>
</manifestEntries>
</archive>
</configuration>
</plugin>
I then create the assembly descriptor file, placing it in the root path (/vcat) alongside the pom.xml. As long as you provide the correct path in the <descriptor> element, the plugin will find the descriptor file and use the settings defined in it. Since i wanted to exclude certain files in the final jar, i have to use the <directory> element to define the directory in which my <fileSet> will be valid. In this case, the files i want to exclude reside in the target/classes directory, and i want to exclude specifically certain some .properties files, which i name explicitly using the <exclude> element, and then all .sql, .csv files using wildcard characters. Note that this syntax will exclude the files defined in the /target/classes file from being output to the root path of the jar, which is what i want - i explicitly tell this to the plugin using the <outputDirectory> element with the root path (/) as the argument.
<assembly>
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>/target/classes</directory>
<outputDirectory>/</outputDirectory>
<excludes>
<exclude>encryptable.properties</exclude>
<exclude>hibernate.properties</exclude>
<exclude>hibernate.cfg.xml</exclude>
<exclude>market-data-securities.properties</exclude>
<exclude>*.csv</exclude>
<exclude>*.sql</exclude>
</excludes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>