Dark theme | Light theme

October 6, 2020

Mastering Maven: Replace Files In Archive

In a previous post we learned how to replace a file in an archive with Gradle. In this blog post we use Maven to achieve the same goal. With Maven we first have to extract the contents of the archive and then assemble a new archive where we use a file replacement to replace an original file from the archive. To extract the contents we use the task unzip with the maven-antrun-plugin. To create a new archive we use the maven-assembly-plugin. The destination directory of the extracted contents is the input fileset for the assembly definition together with the files we want to overwrite. The end result is a new archive with replaced files.

In the following pom.xml we configure the maven-antrun-plugin and maven-assembly-plugin:

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
                                <!-- We unzip the original archive to target/archive-contents -->
                                <unzip src="lib/" dest="target/archive-contents"/>
                    <!-- Name of new archive -->
                    <!-- We don't want the id of the assembly descriptor in the filename -->
                        <!-- Descriptor of the contents of the new archive -->

The descriptor of the assembly plugin is sample.xml and here we define the contents of the new archive:

<assembly xmlns="" xmlns:xsi=""
            <!-- Files that are extracted from the original archive -->
            <!-- New files that replace files from the original archive -->

Written with Maven 3.6.3.