Tuesday, February 5, 2013

How create zip files with Maven

In some maven projects is important generate a  compressed file, for example to send by email.
So, in this post I will show you how you can generate compressed files using Maven.
In you pom.xml file you need incorporate the maven-assembly-plugin plugin, something like that:


<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-assembly-plugin</artifactId>
 <configuration>
  <descriptor>src/assembly/bin.xml</descriptor>
  <finalName>${plugin.name}</finalName>
 </configuration>
 <executions>
  <execution>
   <phase>package</phase>
   <goals>
    <goal>single</goal>
   </goals>
  </execution>
 </executions>
</plugin>

So, as you can see, you need define a bin.xml file in src/assembly/ path, you can define the name and path that you want. Is only mandatory have a specific structure content. That you can see a example below:

<assembly
 xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
 <formats>
  <format>zip</format>
  <format>tar</format>
  <format>tar.gz</format>
  <format>tar.bz2</format>
 </formats>
 <includeBaseDirectory>false</includeBaseDirectory>
 <dependencySets>
  <dependencySet>
   <includes>
    <include>groupId.example:artifactId.example</include>
   </includes>
   <unpack>false</unpack>
   <scope>runtime</scope>
   <outputDirectory>${plugin.name}</outputDirectory>
  </dependencySet>
 </dependencySets>
 <fileSets>
  <fileSet>
   <directory>resources</directory>
   <outputDirectory>${plugin.name}</outputDirectory>
   <includes>
    <include>file-example.xml</include>
    <include>file-example.png</include>
   </includes>
  </fileSet>
  <fileSet>
   <directory>target</directory>
   <outputDirectory>${plugin.name}</outputDirectory>
   <includes>
    <include>build-*.jar</include>
   </includes>
  </fileSet>
 </fileSets>
</assembly>

That example, permit create zip, tar, tar.gz, tar.bz2. If you want put all dependencies on the compress file, you don't need define includes tag.

More details about maven-assembly-pluginhttp://maven.apache.org/plugins/maven-assembly-plugin



I hope that information is useful for you. Feel free to comment this post.

No comments:

Post a Comment