1. To use the Assembly Plugin in Maven, you simply need to:
- choose or write the assembly descriptor to use,
- configure the Assembly Plugin in your project's pom.xml, and
- run "mvn assembly:single" on your project.
2. How to configure it in pom.xml:
<plugin><artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.redhat.gss.jaxbTest.Main</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>src/main/assembly/descriptor.xml</descriptor>
</descriptors>
<outputDirectory>${basedir}/target/bin</outputDirectory>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
3. pre-defined desrciptor files
This descriptor specifies the type of assembly archive to create, the contents of the assembly, and the ways in which dependencies or its modules are bundled with an assembly.There are some pre-defined desrciptor files. Below is the jar-with-dependencies descriptor format:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<!-- TODO: a jarjar format would be better -->
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
It contains the binary output of your project, along with its unpacked dependencies. You can replace
<descriptors>
<descriptor>src/main/assembly/descriptor.xml</descriptor>
</descriptors>
with
<descriptorRefs>
<descriptor>jar-with-dependencies</descriptor>
</descriptorRefs>
4. Filter distribution files in descriptor.
Below is an example of filtering files before adding them into the assembly.
<fileSets>
<fileSet>
<directory>${basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.txt</include>
<include>*.xml</include>
</includes>
<excludes>
<exclude>README.txt</exclude>
<exclude>NOTICE.xml</exclude>
</excludes>
</fileSet>
</fileSets>
The above descriptor tells the assembly plugin to include all txt and xml files in ${basedir}, but exclude README.txt and NOTICE.xml.
5. Using Maven Archiver to configure the Manifest file (META-INF/MANIFEST.MF)
The manifest is a special file that can contain information about the files packaged in a JAR file.
(see:http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html
http://maven.apache.org/shared/maven-archiver/examples/manifest.html)
Here is a simple example
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.redhat.gss.jaxbTest.Main</mainClass>
</manifest>
</archive>
It adds the classpath to the manifest and specifies the main class. Thus the jar file is an executable file.