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.

No comments:

Post a Comment