Eddie
Eddie

Reputation: 939

Create a zip file and add it to a war file with maven

I have some example RESTful client projects that go along with a RESTful Java web service I am working on. Basically, these projects should not be built but instead zipped up and included in the war file so that they will be available as static resources when the war file is deployed. This makes it easy to update the example clients along with the actual Java web service and guarantee that they are deployed together.

I've been able to use the maven assembly plugin to create a zip file but that stage executes after the war stage. I haven't been able to figure out the maven incantation needed to create the zip file then add it to the war file.

Here is what I have so far, but it only does about half the job. Also, I need to move the ExampleProject directory so the unzipped files don't go into the final war file.

pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptor>src/assembly/AssembleExamples.xml</descriptor>
        <finalName>examples.zip</finalName>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

AssembleExamples.xml

<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">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/WebContent/docs/</directory>
            <outputDirectory/>
            <includes>
              <include>ExampleProject/pom.xml</include>
              <include>ExampleProject/src/**</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

Upvotes: 0

Views: 4173

Answers (2)

Mithun
Mithun

Reputation: 8067

Two of the plugins maven-war-plugin and maven-assembly-plugin are required to be executed in the same phase, package. First, maven-assembly-plugin and then maven-war-plugin. You need to add the plugins in the following order to make sure that they run in same phase and correct order:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.0.1</version>
    <executions>
        <execution>
            <id>prepare-war</id>
            <phase>package</phase>
            <goals>
                <goal>exploded</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptor>src/assembly/AssembleExamples.xml</descriptor>
        <finalName>examples.zip</finalName>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

You can generate the war using mvn package.

Upvotes: 2

Eddie
Eddie

Reputation: 939

Here is how I ended up doing it.

Thanks to Mithun I realized that there are two ways to configure the war plugin and the way I was doing it was not appropriate for my situation.

This is what I added to my pom file:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <configuration>
                <descriptor>src/assembly/AssembleJavaExample.xml</descriptor>
                <finalName>myexample</finalName>
                <appendAssemblyId>false</appendAssemblyId>
                <outputDirectory>${project.build.directory}/${project.build.finalName}/docs</outputDirectory>
            </configuration>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <warSourceDirectory>WebContent</warSourceDirectory>
        <failOnMissingWebXml>true</failOnMissingWebXml>
        <warName>mywar</warName>
        <warSourceExcludes>docs/myexample/**</warSourceExcludes>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>war</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The assembly plugin had to go first so it would execute before the war plugin. The assembly plugin creates the zip file and places it in the directory that the war plugin uses to create the war file (the outputDirectory configuration). I then had to exclude the example sources from being included in the war file (the warSourceExcludes configuration). I'm not sure if this is the best way but it seems to be working out quite well.

Upvotes: 0

Related Questions