java 使用 Maven 构建时 jar 文件已损坏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10797831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:36:48  来源:igfitidea点击:

jar file gets corrupted while building with maven

javamaven-pluginmaven-3

提问by pranay

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 .

在构建War文件时,我将一组 jars 从一个位置复制到War内的文件夹中。虽然文件确实被复制了,但是我认为它们会被损坏,因为在War之外提取的 jar 的相同类文件用调试器打开,而从War文件中提取后它没有打开。

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

这是我的War pom.xml 的一部分,我在其中复制了罐子

<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>

SomeSourceDirectoryhas 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

SomeSourceDirectory有一些 jars 和一些其他文件 结果是: myWar/somefolder/a.jar 但是当我在调试器中打开这个 jar 中的类时..我在 WinZip 中得到错误

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.

但是,当我在原始文件夹中查看它时,可以查看相同的类文件,即War之外。那么在复制 jars 时是否有错误?谢谢。

回答by Emmanuel Bourg

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

删除<filtering>true</filtering>,它会损坏 jar 文件。

回答by Benoit

Also, you can continue benefit to use maven filtering without corrupting jars inside (yes I do need to package with jars inside AND use filtering too)

此外,您可以继续受益于使用 maven 过滤而不会损坏内部的罐子(是的,我确实需要在内部包装罐子并使用过滤)

We choose to exclude jar from filtered extensions.

我们选择从过滤的扩展中排除 jar。

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

在父 pom 的 pluginManagement 部分中,我们放置了此配置

 <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).

请注意,我们添加了 jar 扩展名以及默认的 maven 排除文件类型(毕竟它是一个 zip)。

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

除了避免档案损坏之外,它还可以加快进程,因为它不必过滤大文件。

回答by spierobi

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.

到我在 ANT 中的复制任务。它在复制 jar 文件时损坏了它们。我通过仅在目标文本文件而不是 jar 文件上应用过滤器解决了这个问题。

回答by eldur

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

试试Maven 程序集插件。这是我最喜欢的将自定义资源添加到 *.war 文件的插件。另请参阅预定义描述符文件

回答by Evgeni Dimitrov

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>