Assembling a zip with Maven2

At one of my customers, when we create a new release of our application (consisting of several WAR files) we have to supply one zip-file with all the WAR files in it. Also a zip file containing the compiled Jasper reports must be added to it. Although this can be done by hand, it is of course much nicer to make part of the automated build cycle of our Maven build!
Now luckily there is a standard plugin available which can do this for us. The plugin is called ‘maven-assembly-plugin’ and it takes an ‘assembly.xml’ file to be configured. To configure the plugin in your project to package all compiled reports you will get something like:

1
2
3
4
5
6
7
8
<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptors>
      <descriptor>src/main/jasperreports/assemble.xml</descriptor>
    </descriptors>
  </configuration>
</plugin>

As you can see I have added the ‘assemble.xml’ file to the directory ‘src/main/jasperreports/’. In this ‘assemble.xml’ file I have put the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<id>reports</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <fileSets>
    <fileSet>
      <directory>src/main/jasperreports/compiled</directory>
      <includes>
         <include>*.jasper</include>
       </includes>
       <outputDirectory>/</outputDirectory>
    </fileSet>
  </fileSets>
</assembly>

This configuration is based on the module described in this post about compiling Jasper reports with Maven2. This will create the zip file containing all compiled reports in this project.
Since there are some problems with the plugin and multi-module Maven project (like described here) I also added a new module to my project to create the overall zip file: my-app-distribution.
In the pom file of this module I added:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
...
<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <descriptors>
          <descriptor>src/main/resources/assemble.xml</descriptor>
        </descriptors>
      </configuration>
    </plugin>
  </plugins>
</build>
<dependencies>
    <dependency>
      <groupId>net.pascalalma</groupId>
      <artifactId>my-app-1</artifactId>
      <version>1.0.0</version>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>net.pascalalma</groupId>
      <artifactId>my-app-2</artifactId>
      <version>1.0.0</version>
      <type>war</type>
    </dependency>
</dependencies>
...

And in the ‘assemble.xml’ I put:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<assembly>
  <id>kit</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <excludes>
        <exclude>net.pascalalma:my-app-distribution</exclude>
      </excludes>
    </dependencySet>
  </dependencySets>
  <files>
    <file>
      <source>..\my-app-1\target\my-app-1-reports.zip</source>
    </file>
  </files>
</assembly>

This assembly will zip all dependencies (except the .jar of its own module) and the reports.zip we have created before. Of course you can do much, much more with this plugin. You can read about that here including more examples.

categories: Maven

About Pascal Alma

Pascal started as an Oracle Developer in 1997 and developed numerous applications with Oracle Designer/Developer and PL/SQL. Since 2001 Pascal becomes more and more active with the development of software at the Java/J2EE platform. Nowadays Pascal is a senior JEE Developer/ Architect and has a lot of experience with several open source initiatives/ frameworks especially within the Enterprise Integration area. Besides these technical skills Pascal is a big Scrum enthusiastic.

Comments are closed.