Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven assembly plugin: add a file into a dependency jar

There is this maven module, say prj-package-module, to package the project artifacts into a tar file using maven-assembly-plugin. There are also jars added as dependencies in the prj-package-module/pom.xml and packaged into the tar file.

Now the requirement is to add a file, prj-pakacge-module/src/main/resources/file.xml to one of these dependency jars before packaging into the tar file. How can I achieve this?

Edit: The file is a JNLP with list of dependency jars dynamically added to it. For security reasons, Javaws also requires the JNLP file to be added a jar and the jar to be signed. This is where I hit the problem.

like image 364
Kiran Mohan Avatar asked Oct 20 '25 12:10

Kiran Mohan


1 Answers

Here is a maven solution to dynamically unpack an existing dependency, add (copy) a resource to the unpacked folder, repack (jar) the hole and as such get a modified copy of the initial jar.

A sample pom file doing exactly that for the junit dependency:

<build>
    <plugins>
        <!-- unpack step -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>junit</groupId>
                                <artifactId>junit</artifactId>
                                <version>4.11</version>
                                <type>jar</type>
                                <outputDirectory>${project.build.directory}/unpack-tmp</outputDirectory>
                                <includes>**/*.class,**/*.xml</includes>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- add the additional resource step -->
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/unpack-tmp</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <include>test.properties</include>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- repack step -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>repack</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <classesDirectory>${basedir}/target/unpack-tmp</classesDirectory>
                        <finalName>junit-modified</finalName>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

What it is actually doing:

  • As part of the prepare-package phase we are adding three steps to create a modified jar from a dependency
  • First step: unpack the dependency via the Maven Dependency Plugin and its unpack goal: an unpacked folder will be created to the tmp folder unpack-tmp
  • Second step: copy the concerned resource via the Maven Resources Plugin and its copy-resources goal.
  • Third step: re-pack the whole via the Maven Jar Plugin and its classic jar goal

Running the sample above, the junit-modified.jar file will appear in the target folder of the maven project.

The order of the plugin configurations above is important to respect the steps flow as part of the same phase.

Then, you have one additional file to add to your fat-jar, which is indeed the modified jar you were probably looking for.


If you don't need a dynamic approach, a better approach would be to do it once and have a classified version of that dependency as explained in this other SO post.

Alternatively, move it to a Maven profile so that at least it is not part of the default build.

like image 159
A_Di-Matteo Avatar answered Oct 23 '25 03:10

A_Di-Matteo