Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 618
pranay Avatar asked May 29 '12 11:05

pranay


People also ask

Does Maven build create jar?

Maven can build a Fat JAR from your Java project. A Fat JAR is a single JAR file that contains all the compiled Java classes from your project, and all compiled Java classes from all JAR files your project depends on (see Maven Dependencies).

What is jar file in Maven?

jar'. The resulting 'jar' file contains the compiled java class files as well as the files from src/main/resources. Usually there is no need to mentioned the 'maven-jar-plugin' explicit cause it's bound to the Maven Build Life Cycle. For full documentation, click here.


2 Answers

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

like image 129
Emmanuel Bourg Avatar answered Sep 21 '22 11:09

Emmanuel Bourg


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.

like image 44
Benoit Avatar answered Sep 24 '22 11:09

Benoit