pranay
pranay

Reputation: 2369

jar file gets corrupted while building with maven

while building a war file i am copying a set of jars from a location to a folder inside the war. While the files do get copied , however i think they get corrupted because the same class files of the jar when taken outside the war opens with a debugger while it does not open after taking from war file .

This is a part of my war pom.xml where i copy the jars

<execution> 
    <id>copy-jars</id> 
    <phase>process-resources</phase> 
    <goals> 
        <goal>copy-resources</goal> 
    </goals> 
    <configuration> 
        <outputDirectory>${basedir}/target/${project.artifactId}-${buildNumber}/somefolder</outputDirectory> 
         <resources> 
            <resource> 
                <directory>SomeSourceDirectory</directory> 
                <filtering>true</filtering> 
                <includes> 
                    <include>**/**</include> 
                </includes>
            </resource>
        </resources> 
    </configuration> 
</execution>

SomeSourceDirectory has some jars and some other files The result is: myWar/somefolder/a.jar but when i open the classes inside this jar in a debugger..i get error in WinZip that

Invalid compressed data to extract.
Severe Error:  Compressed data is invalid

However the same class file can be viewed when i view it in original folder i.e outside the war. So is there a mistake while copying the jars? Thanks.

Upvotes: 32

Views: 21164

Answers (6)

Anshuman Chatterjee
Anshuman Chatterjee

Reputation: 962

My problem was there were multiple spaces & new-line in-between the ClassPaths. Had to make it 1 space separated. Checked it through jd-gui

Space in Classpath in Manifest file

Upvotes: 0

Benoit
Benoit

Reputation: 3598

Also, you can continue benefit to use maven filtering without corrupting jars inside.

We choose to exclude jar from filtered extensions.

In th pluginManagement section of the parent pom we put this configuration

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.5</version>
    <configuration>
      <encoding>${project.build.sourceEncoding}</encoding>
      <nonFilteredFileExtensions>
        <nonFilteredFileExtension>jar</nonFilteredFileExtension>
        <nonFilteredFileExtension>pdf</nonFilteredFileExtension>
        <nonFilteredFileExtension>swf</nonFilteredFileExtension>
        <nonFilteredFileExtension>zip</nonFilteredFileExtension>
        <nonFilteredFileExtension>bz2</nonFilteredFileExtension>
        <nonFilteredFileExtension>gz</nonFilteredFileExtension>
        <nonFilteredFileExtension>acp</nonFilteredFileExtension>
        <nonFilteredFileExtension>bin</nonFilteredFileExtension>
        <nonFilteredFileExtension>odt</nonFilteredFileExtension>
        <nonFilteredFileExtension>doc</nonFilteredFileExtension>
        <nonFilteredFileExtension>xls</nonFilteredFileExtension>
      </nonFilteredFileExtensions>
    </configuration>
  </plugin>

Note we added jar extensions as well as default maven excluded filetypes (its a zip after all).

Besides avoiding corruption of the archive it also speeds up the process as it does not have to filter large files.

Upvotes: 17

Evgeni Dimitrov
Evgeni Dimitrov

Reputation: 22506

Just as addition to the other answers, the other option is to enable the filtering only for the resources that require filtering:

<build>
...
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>core.properties</include>
                </includes>
            </resource>
            <resource>
                <filtering>false</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>pdf/color_profile/sRGB.icc</include>
                </includes>
            </resource>
        </resources>
</build>

Upvotes: 1

spierobi
spierobi

Reputation: 140

I had a similar error when I've added

<copy ...>
<fileset ... />
<filterchain>
    <tokenfilter>
         <replacestring from="..." to="..." />
    </tokenfilter>
</filterchain>
</copy>

to my copy task in ANT. It corrupted the jar files when copying them. I've solved this by applying the filter ONLY on the targeted text files and not on jar files.

Upvotes: 1

eldur
eldur

Reputation: 254

Try Maven Assembly Plugin. It's my favourite plugin to add custom resources to a *.war file. See also Pre-defined Descriptor Files.

Upvotes: 1

Emmanuel Bourg
Emmanuel Bourg

Reputation: 11038

Remove <filtering>true</filtering>, it corrupts the jar files.

Upvotes: 69

Related Questions