'Maven assembly: rename unpacked file
Multiple of my Maven projects using maven-assembly-plugin need to copy two binary files into a folder.
These are the two original files common to all of my projects:
procrun.exe
procrunw.exe
I'd like this set of files to be reused. They could come from a dependency such as a JAR or a ZIP file to later unpack them as part of my build process. That way, if I later chose to upgrade the binaries I could create a new version of my common project and just change the dependencies to my common project. Another reason to do this is because I want to use a minimize the number of build-related files in individual projects.
Now, the tricky part is that I need to rename each file while unpacking the dependencies. For instance, for project A, I need the files to be copied as follows:
bin/projectA_procrun.exe
bin/projectA_procrunw.exe
Partial solution
My partial solution is to have these two files in the src/main/build/bin folder on each of the Maven projects.
It's not ideal but at least I'm able to reuse the contents of a common assembly file as described in this page:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>my_assembly</id>
<formats>
<format>zip</format>
</formats>
<files>
<file>
<source>src/main/build/bin/procrun.exe</source>
<outputDirectory>bin</outputDirectory>
<destName>${myexecutable.name}.exe</destName>
</file>
<file>
<source>src/main/build/bin/procrunw.exe</source>
<outputDirectory>bin</outputDirectory>
<destName>${myexecutable.name}w.exe</destName>
</file>
</files>
<!-- Additional details ommitted -->
</assembly>
Failed attempt
The outputFileNameMapping attribute in Assembly allows renaming artifacts but not the files inside the assembly.
One failed attempt consisted in registering each file as an individual Maven artifact. That way, I could use outputFileNameMapping inside sections corresponding to each of my artifacts.
The problem I run into is that Nexus doesn't like having an 'exe' as an artifact.
Question
Any ideas as to how I can achieve the expected result, either by enhancing my partial or by adopting an alternative approach?
Solution 1:[1]
Using outputFileNameMapping specifically doesn't apply to the contents of JAR as you've discovered. You can see this issue for the gory details: MASSEMBLY-312.
This other, fairly old issue: MASSEMBLY-45 proposed an enhancement to allow Ant-style filename mapping which probably would give us the functionality that we need. It was partially implemented but was never finished because of unit testing issues.
I ended up taking khmarbaise's advice and unpacking the files that I needed with the dependencies plugin and then repacking them with assembly.
Solution 2:[2]
I know this is an old question, so the answers might be outdated.
I was able to do the trick by using maven-dependency-plugin with unpack goal and fileMappers to rename the destination of the unpacked files.
This official documentation with an example helped me a lot: https://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-filemapper.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jgibson |
| Solution 2 | Felipe Mosso |
