3

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.

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
Kiran Mohan
  • 2,696
  • 6
  • 36
  • 62
  • to clarify: you need to add a file into a jar which is an external library, a maven dependency? – A_Di-Matteo Feb 04 '16 at 14:38
  • yes. the file is to be inserted into the jar which is added as maven dependency in pom.xml – Kiran Mohan Feb 04 '16 at 14:40
  • The file you want is listed as a dependency and it isn't on the tar generated file? Thats odd... – Jorge Campos Feb 04 '16 at 14:42
  • Create a sub-module responsible for creating the modified jar, and use overlays. – Thorbjørn Ravn Andersen Feb 04 '16 at 14:42
  • 1
    not of my business for sure, but it might be an [XY](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) problem here, why would you need to modify an external library? Is the library from another department or is it really a public library from the official maven repository? – A_Di-Matteo Feb 04 '16 at 14:42
  • @A.DiMatteo - OK. So 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. – Kiran Mohan Feb 04 '16 at 15:01

2 Answers2

5

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:

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.

Community
  • 1
  • 1
A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
0

It seems that you're trying to modify a dependency of the maven project before packaging it into the final artifact of the project. This is dodgy. If that jar/library (the dependency) is yours, then modify the pom of that jar, rebuild it, and then have the current project resolve the latest artifact.

If, otherwise, the dependency jar is not yours, then it should suffice to have the resource (prj-pakacge-module/src/main/resources/file.xml) in the current project (the one you're building now), as the end result will be the same (as long as the assembly plugin is set to flatten all artifact jars in the target uber/fat jar...

ernest_k
  • 44,416
  • 5
  • 53
  • 99