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:
- 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 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:
- Separately add assembly.xml in your child project as you would like to pack file differently in every project. MavenAssemblyPluginChild1 project assembly.xml:
- MavenAssemblyPluginChild2 project assembly.xml:
- 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:
- 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:
- If you extract any of the file you will see below structure:
- For more information of maven assembly plugin please visit maven site here
Download Project: MavenAssemblyPlugin