Sunday, September 2, 2012

The images and .mar files packed in war using maven are getting corrupted

We faced this strange issue - The images and .mar files packed in war using maven are getting corrupted.
The images in local work-space before invoking 'mvn package' were fine, but the same images packed inside war file by maven were getting corrupted.

The root cause of the issue was 'filterset' task invoked as part 'copy' task from maven-antrun-plugin.
Here is the code from pom.xml which was causing corrupted images
<project>
.
.

 <build>
  <plugins>
    <plugin>
      <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <id>prepare-webapp</id>
            <phase>process-resources</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>     
                <tasks>
                  <echo>copying the web-app folder</echo>


                <tstamp>
                  <format property="build.at" pattern="dd MMMM yyyy HH:mm Z" />
                </tstamp>


               <copy todir="${basedir}/target/main/webapp">
                 <fileset dir="${basedir}/../../../src/web/webapp" includes="**/*" />                                    
                    <filterset>
                         <filter token="VERSION" value="${pom.version}" />
                         <filter token="BUILD_DATE" value="${build.at}" />
                     </filterset>
                </copy>
              </tasks>
            </configuration>
           </execution>
          </executions>
         </plugin>
        </plugins>
      </build>
 </project>

Here we incorporated token replacement for all the files under 'webapp' directory. Due to which the 'filterset'  was corrupting binary files like images & .mar files.
Actually we are supposed to perform token replacement only on web.xml files. The fix was to perform it only on these files.
Here is the code used to fix the issue.
<plugin>
   <artifactId>maven-antrun-plugin</artifactId>
     <dependencies>
       <dependency>
<groupId>ant</groupId>
<artifactId>ant-nodeps</artifactId>
<version>1.6.5</version>
</dependency>
     </dependencies>
     <executions>
       <execution>
         <id>prepare-webapp</id>
         <phase>process-resources</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
          <tasks>
  <tstamp>
             <format property="build.at" pattern="dd MMMM yyyy HH:mm Z" />
           </tstamp>
  <echo>Token replacement in web.xml</echo>
  <replaceregexp byline="true">
    <regexp pattern="@VERSION@" />
    <substitution expression="${pom.version}" />
    <fileset dir="${basedir}/../../../src/web/webapp" includes="**/web.xml" />
  </replaceregexp>
  <replaceregexp byline="true">
    <regexp pattern="@BUILD_DATE@" />
    <substitution expression="${build.at}" />
    <fileset dir="${basedir}/../../../src/web/webapp" includes="**/web.xml" />
           </replaceregexp>
  <echo>copying the web-app folder</echo>                                
           <copy todir="${basedir}/target/main/webapp">
             <fileset dir="${basedir}/../../../src/web/webapp" includes="**/*" />                                  
           </copy>
          </tasks>
          </configuration>
        </execution>
     <executions>
</plugin>         

No comments: