Changing a filename using the maven assembly plugin

I’m currently working on a project which requires the build to produce a zip archive of the jar and some other stuff (doc files mainly). I’ve used the maven assembly plugin, with an assembly descriptor to help me out here. However, there was one slightly unusual requirement – I needed to change the name of the jar so that inside the zip, it has a non-standard name, and no version number (the reasons behind this are fairly weak, but basically it’s because a load of other scripts, which we can’t change, expect to find the jar in this non-standard format).

So, the build produces:

myproject-1.0.0.0-SNAPSHOT.jar

but in the zip I need to have:

my-project.jar

Here’s how I did it.

  • Include the maven-assembly plugin in the build:

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-1</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/kit.xml</descriptor>
</descriptors>
<finalName>${pom.artifactId}-${pom.version}</finalName>
<outputDirectory>build/maven/${pom.artifactId}/target</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

  • Create the kit.xml in src/main/assembly and specify <destName> in the file inclusion. Here’s my kit.xml:

<assembly>
<id>kit</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>doc</outputDirectory>
<includes>
<include>*.*</include>
</includes>
</fileSet>
</fileSets>
<files>
<file>
<source>build/maven/${artifactId}/target/${artifactId}-${version}.${packaging}</source>
<outputDirectory>/</outputDirectory>
<destName>my-project.jar</destName>
</file>
</files>
</assembly>

As you can see, the assembly descriptor has a “files” section, which is what does the trick for us. I’ve isolated the actual section below for clarity:

<files>
<file>
<source>build/maven/${artifactId}/target/${artifactId}-${version}.${packaging}</source>
<outputDirectory>/</outputDirectory>
<destName>my-project.jar</destName>
</file>
</files>

Advertisement

12 comments

  1. Anonymous · November 16, 2011

    Very useful link, but i’ve tried something similar and it creates two files, myfile.war, with all the default options, and myfile-permissions.war, with renamed files (that takes from the first one) and so on.

    “permissions” as “kit” in your example, is stored in kit and is appended to our project name for the secondary .war or .zip. I cannot leave it empty…do you know a way for renaming files inside the “primary” war or zip that is created?

    Thanks

    Dámaris.

  2. test1 · March 17, 2013

    I have to say that this information is awesome. I am extremely awed to uncover such useful guides on adjustable piano bench.

  3. Anonymous · May 17, 2013

    Thank you — saved my bacon

  4. keyword tool · May 29, 2013

    Wow, that’s what I was looking for, what a stuff! existing here at this blog, thanks admin of this web page.

  5. local seo · July 18, 2013

    Hi would you mind sharing which blog platform you’re working with? I’m looking to
    start my own blog soon but I’m having a tough time selecting between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for something unique.
    P.S Sorry for being off-topic but I had to ask!

  6. Anonymous · November 13, 2013

    Here is my solution via outputFileNameMapping using dependency sets with war files

    ${project.groupId}:*:war:${project.version}

    true
    /resources/webapps
    false
    ${artifact.artifactId}.${artifact.extension}

  7. Gilvanise · November 18, 2013

    Thanks Gede great input. I think that those things are imarptont (live reloading of settings etc) but should not be designed in from the outset (but then I’m an emergent design guy). Frameworks like OSGi are compelling, but charge a very high complexity tax. Allowing for fast (and automated) rolling restarts including re-deployment of externalised settings is most imarptont and as you say this requires redundancy and a simple architecture.

  8. Anonymous · February 7, 2014

    Awesome..thanks

  9. Anonymous · August 11, 2014

    Thank you ! Thats exactly what I am searching fory!

  10. Anonymous · November 21, 2014

    thank you.

  11. Cedric · October 21, 2015
  12. Souhail · January 20, 2016

    Thank for this solution, have you an idea on how modify files contents using maven assembly plugin?
    Regards.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s