Wednesday, March 10, 2010

Binary files and Maven

Recently, I was frustrated while trying to package a Java keystore file from my Maven project to a project archive (tar or zip) using assembly.xml. Somehow, when I used the command:
keytool -keystore cacerts -list
on the certificate keystore inside the tar/zip archive, it would just say that the keystore is invalid. After some search, I found that there is a little setting in the pom.xml which instructs maven not to do any filtering tasks on the files you specify when you execute a maven command.

Lets talk a bit about maven resources filtering. Maven resources documentation has a very good explanation of this feature.  Basically, when you use filtering=true inside your resources tag, the output file in the target folder will contain dynamic values of fields (if it had any). Like, for example:

Suppose, src/main/resources has a Hello.txt, whose contents are Hello ${name}

If filtering is true, ${name} is going to be replaces with the actual value of the property "name" in the pom.xml, when the file is copied to target folder.

Now, even though this filtering is useful, sometimes, we don't want maven to apply this feature on all files in the directory - for example certificate key stores, which are inherently binary in nature. So how do you tackle this? Simple, just use something like this:

Here, I am manually excluding files on which I don't want to apply filters.

Also, in the assembly.xml, I would like to keep line endings, as they might also corrupt these files.
These simple steps will enable you to package any binary content files using Maven.


  1. Thank you! Safed my day. I did pack the 7za.exe in my zip which was corrupt afterwards.

  2. Attractive, post. I just stumbled upon your weblog and wanted to say that I have liked browsing your blog posts. After all, I will surely subscribe to your feed, and I hope you will write again soon! BinaryToday

  3. Superior post, keep up with this exceptional work. It's nice to know that this topic is being also covered on this web site so cheers for taking the time to discuss this! Thanks again and again! BinaryToday

  4. Titanium Flash Mica - Online Sports Betting, Casino, Slot Machines
    Betting. titanium knife Online Sports Betting. black oxide vs titanium drill bits Online stilletto titanium hammer Sports Betting, Casino, Slot Machines. The biggest sports bet in titanium granite the world! We offer you the best in titanium strength online betting with