Maven Assembly Plugin Example

Handling build with maven is easy task where just you need to add dependencies in your pom.xml. Apart from this maven provides many more advance feature which can be use based on your project requirement. On of the mandatory requirement of any project is package the project in deploy on servers. As you know that any project can have multiple files which contains dependencies jars, properties files, xml files, bat files, sh files and many more. So if you want deploy you project on the server you will need all of these files. In this example I will show you how you can put all files together using maven assembly plugin.

To better understand I will create sample project with multiple module. Please see below parent and child module maven project structure and you can also download this project from Java Honk github here:

  • Parent maven project:

Maven Assembly Plugin Example

  • ChildModule1

Maven Assembly Plugin Example

  • ChildModule2

Maven Assembly Plugin Example

  • As you see above project structure we have create MavenAssemblyPlugin project which is parent project and to better understand two child project MavenAssemblyPluginChild1 and MavenAssemblyPluginChild2 also got created. To assemble this project you will have to add below maven assembly plugin to pom.xml and if you are using parent child module maven project then you don’t need to add assembly plugin to every project just add below to your parent pom.xml also please notice we have added maven-dependency-plugin which will all dependency jar into lib folder which ofcourse will be available in project target/lib directory and from here assembly plugin will pickup jar and put in zip, tar and tar.gz. file.

Parent pom.xml:

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>

	<groupId>MavenAssemblyPlugin</groupId>
	<artifactId>MavenAssemblyPlugin</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<modules>
		<module>MavenAssemblyPluginChild1</module>
		<module>MavenAssemblyPluginChild2</module>
	</modules>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
	</dependencies>

	<build>

		<resources>
			<resource>
				<directory>src/main/config</directory>
				<excludes>
					<exclude>**/*.xml</exclude>
					<exclude>**/*.properties</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<excludes>
					<exclude>**/*.xml</exclude>
					<exclude>**/*.properties</exclude>
				</excludes>
			</resource>
		</resources>

		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>2.8</version>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/lib</outputDirectory>
							<overWriteReleases>false</overWriteReleases>
							<overWriteSnapshots>false</overWriteSnapshots>
							<overWriteIfNewer>true</overWriteIfNewer>
						</configuration>
					</execution>
				</executions>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.6</version>
				<executions>
					<execution>
						<id>zip</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<descriptors>
						<descriptor>${basedir}/assembly.xml</descriptor>
					</descriptors>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<scm>
		<connection>scm:git:https://github.com/javahonk/MavenAssemblyPlugin.git</connection>
	</scm>

</project>
  • Parent assembly.xml – We will assemble project related file in three different format which is zip, tar and tar.gz and what file will be included you could see below assembly.xml file:
<assembly>
  <id>bin</id>
  <formats>
    <format>zip</format>
    <format>tar</format>
    <format>tar.gz</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.build.directory}/lib</directory>
      <outputDirectory>/lib</outputDirectory>
      <includes>
        <include>*.jar</include>       
      </includes>
    </fileSet>    
  </fileSets>
</assembly>
  • Separately add assembly.xml in your child project as you would like to pack file differently in every project. MavenAssemblyPluginChild1 project assembly.xml:
<assembly>
  <id>bin</id>
  <formats>
    <format>zip</format>
    <format>tar</format>
    <format>tar.gz</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.build.directory}/lib</directory>
      <outputDirectory>/lib</outputDirectory>
      <includes>
        <include>*.jar</include>       
      </includes>
    </fileSet>
    <fileSet>
      <directory>${project.build.directory}</directory>
      <outputDirectory>/lib</outputDirectory>
      <includes>
        <include>*.jar</include>       
      </includes>
    </fileSet>
    <fileSet>
      <directory>src/main/resources</directory>
      <outputDirectory>/config</outputDirectory>
      <includes>
        <include>*.xml</include>
        <include>*.properties</include>
      </includes>
    </fileSet>
    <fileSet>
      <directory>${basedir}/scripts</directory>
      <outputDirectory>/scripts</outputDirectory>
      <includes>
        <include>*.properties</include>
        <include>*.bat</include>           
      </includes>
    </fileSet>
  </fileSets>
</assembly>
  • MavenAssemblyPluginChild2 project assembly.xml:
<assembly>
  <id>bin</id>
  <formats>
    <format>zip</format>
    <format>tar</format>
    <format>tar.gz</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.build.directory}/lib</directory>
      <outputDirectory>/lib</outputDirectory>
      <includes>
        <include>*.jar</include>       
      </includes>
    </fileSet>
    <fileSet>
      <directory>${project.build.directory}</directory>
      <outputDirectory>/lib</outputDirectory>
      <includes>
        <include>*.jar</include>       
      </includes>
    </fileSet>
    <fileSet>
      <directory>src/main/resources</directory>
      <outputDirectory>/config</outputDirectory>
      <includes>
        <include>*.xml</include>
        <include>*.properties</include>
      </includes>
    </fileSet>
    <fileSet>
      <directory>${basedir}/scripts</directory>
      <outputDirectory>/scripts</outputDirectory>
      <includes>
        <include>*.properties</include>
        <include>*.bat</include>           
      </includes>
    </fileSet>
  </fileSets>
</assembly>
  • Now right click parent pom.xml and click maven install as shown below. This will start build, clean, verifiy and install jar of the project and if all goes successful you will message on the console:

Maven Assembly Plugin Example

Maven Assembly Plugin Example

  • Now if you refresh your project target folder you will below. For example I am including only MavenAssemblyPluginChild2 project generated file as this will be available for all projects including parent project:

Maven Assembly Plugin Example

  • If you extract any of the file you will see below structure:

Maven Assembly Plugin Example

  • For more information of maven assembly plugin please visit maven site here

download Download Project:  MavenAssemblyPlugin

Leave a Reply

Your email address will not be published. Required fields are marked *