gpt4 book ai didi

java - Maven阴影插件构建的无效或损坏的JAR文件

转载 作者:IT老高 更新时间:2023-10-28 20:55:34 27 4
gpt4 key购买 nike

在将Maven jFree依赖项添加到现有应用程序后,我将无法执行创建的jar。

我收到的唯一错误消息如下:

java -jar target/com.company.product-1.0.0-SNAPSHOT.jar 
Error: Invalid or corrupt jarfile target/com. company.product-1.0.0-SNAPSHOT.jar

完整的 pom.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion
<groupId>com.mycompany</groupId>
<artifactId>com.mycompany.test</artifactId>
<name>${project.artifactId}</name>
<version>1.0.0-SNAPSHOT</version>

<properties>
<java-version>1.7</java-version>
<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
<org.springframework.data-version>1.0.3.RELEASE</org.springframework.data-version>
<org.springframework.ws-version>2.0.4.RELEASE</org.springframework.ws-version>
<org.springframework.ws.oxm-version>1.5.10</org.springframework.ws.oxm-version>
<org.aspectj-version>1.6.12</org.aspectj-version>
<org.slf4j-version>1.5.10</org.slf4j-version>
<selenium-java-version>2.25.0</selenium-java-version>
<browser-mob-version>2.0-beta-6</browser-mob-version>
</properties>

<dependencies>

<!-- Hint A: If we delete this dependency it works -->
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.14</version>
<exclusions>
<exclusion>
<artifactId>itext</artifactId>
<groupId>com.lowagie</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.3.2</version>
</dependency>

<dependency>
<groupId>de.schlichtherle.io</groupId>
<artifactId>truezip</artifactId>
<version>6.6</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>apache-log4j-extras</artifactId>
<version>1.1</version>
</dependency>

<!-- Caching with ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.5.2</version>
<type>pom</type>
</dependency>

<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework-version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
<scope>test</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate.java-persistence</groupId>
<artifactId>jpa-api</artifactId>
<version>2.0-cr-1</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.5.1-Final</version>
</dependency>

<!-- Database Connectors (HSQL should be removed later) -->
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.7</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.16</version>
</dependency>

<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>

<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>

<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- Spring Data JPA dependencies -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${org.springframework.data-version}</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-oxm</artifactId>
<version>${org.springframework.ws.oxm-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-xml</artifactId>
<version>${org.springframework.ws-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>${org.springframework.ws-version}</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>

<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.0</version>
<scope>test</scope>
</dependency>

<!-- Common Utils -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>

<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>

<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>

<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium-java-version}</version>
</dependency>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>${selenium-java-version}</version>
</dependency>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>${selenium-java-version}</version>
</dependency>

<!-- CSV Lib for Keyword Checker -->
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.0</version>
</dependency>

<!-- Google Places API -->
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.10.3-beta</version>
<exclusions>
<exclusion>
<artifactId>jackson-core-asl</artifactId>
<groupId>org.codehaus.jackson</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client-appengine</artifactId>
<version>1.10.3-beta</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.9</version>
</dependency>

<!-- Google Geocode -->
<dependency>
<groupId>com.google.code.geocoder-java</groupId>
<artifactId>geocoder-java</artifactId>
<version>0.9</version>
</dependency>

<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>

<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>

<dependency>
<groupId>net.sf.jgrapht</groupId>
<artifactId>jgrapht</artifactId>
<version>0.8.3</version>
</dependency>

<dependency>
<groupId>jgraph</groupId>
<artifactId>jgraph</artifactId>
<version>5.13.0.0</version>
</dependency>

<!-- Apache Http Client -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.1</version>
</dependency>

<!-- Amazon web services client -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<artifactId>jackson-core-asl</artifactId>
<groupId>org.codehaus.jackson</groupId>
</exclusion>
</exclusions>
</dependency>

<!-- Docx4j - reading excel files -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>2.8.0</version>
</dependency>

<!-- Browser Mob Proxy -->

<dependency>
<groupId>biz.neustar</groupId>
<artifactId>browsermob-proxy</artifactId>
<version>${browser-mob-version}</version>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
</exclusion>
<exclusion>
<artifactId>icu4j</artifactId>
<groupId>com.ibm.icu</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-mapper-asl</artifactId>
<groupId>org.codehaus.jackson</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-core-asl</artifactId>
<groupId>org.codehaus.jackson</groupId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Hint B: If we copy this Apache POI dependencies to the top, it works -->
<!-- Apache POI - for reading xlsx files -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>

<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.5.0</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>

<!-- must be SURE to do this with both spring.handlers and spring.schemas.
otherwise you won't be able to use them in the spring config files. -->
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>

<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>

<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.mycompany.test.Start</mainClass>
</transformer>
</transformers>

<filters>
<filter>
<artifact>bouncycastle:bcprov-jdk15</artifact>
<excludes>
<exclude>META-INF/BCKEY.DSA</exclude>
<exclude>META-INF/BCKEY.SF</exclude>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
</filters>

</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>

如果执行jar,则不会得到其他任何调试信息。

是否可以验证jarfile?还是有类似 java -verbose的东西?

重现步骤:
  • 创建一个新文件夹
  • 从上方将Pom.xml复制到文件夹中。
  • 保存小的`public static void main(...)。进入src/main/java/com/mycompany/test/Start.java
  • 执行mvn clean package
  • 执行java -jar target/com.mycompany.test-1.0.0-SNAPSHOT.jar

  • 附件1:
    package com.mycompany.test;


    public class Start
    {
    public static void main(final String[] args)
    {
    System.out.println("If you are able to get this printed with java -jar you made it. Thanks a lot! :)");
    }
    }

    编辑1:

    我开始删除一些依赖项以识别问题。但是我还不清楚什么地方出了问题。

    现在我弄清楚了(请参阅pom.xml中的提示B),将Apache POI依赖项移到顶部将解决此问题。但是我仍然不知道如何解决问题以及问题出在哪里。

    最佳答案

    最后,如果我使用您展示给我们的pom.xml构建您的项目,并在apache-poi之后声明jfreechart,那么正如您所提到的,我得到了一个损坏的JAR。交换这两个依赖关系的顺序确实可以为我提供正确的JAR。

    我以前对maven-shade-plugin有一些经验,当我使用它时,在创建的JAR的META-INF目录中遇到了麻烦,因此我检查了缺陷。

    我试图复制(使用Total Commander)本地文件系统上的META-INF目录,我注意到在META-INF/licences/中复制文件时出错。如果我尝试将它们分别复制到某个地方并且可以工作,但无法复制整个内容。我的结论是JAR/ZIP存档已损坏。

    我所做的是在Total Commander中输入了该JAR(在JAR文件中为Ctrl + PgDown),然后将thirdpartylicenses.txt重命名为thirdpartylicenses.txt.wtf。这样做,Total Commander可以保存并重新打包JAR(我已经安装了Total7zip Total Commander Packer插件-如果有人尝试过并且不起作用,请尝试安装此插件)。

    在这之后。有用。

    (我也尝试过使用Cygwin的unzip/zip命令重新打包整个内容,而不重命名任何内容,但这没用,新的存档仍被破坏。TotalCommander或我提到的插件确实具有一定的魔力。)

    我猜maven-shade-plugin只会创建损坏或无效的ZIP/JAR存档。我不确定为什么,也许我所描述的内容对其他任何人都无效,但我想我会提到它,以便也许我可以帮上忙。

    我不能只留下这个,所以我更深入地研究了,我想我找到了答案。

    错误的JAR包含65608条目。
    好的JAR包含65450个条目。

    猜猜the number of entries for a plain ZIP file的上限是多少?是的Wiki文章谈到了克服此限制的ZIP64格式。

    好的JAR条目较少,因为实际依赖项会由于pom.xml中依赖项声明的位置而发生变化。 (As described by this answer.)

    我已经数过像这样的条目。

    Collections.list(new JarFile("...").entries()).size();

    我使用的是 Java 7 which seems to support the new ZIP64 format,因此,如果有人尝试使用Java 5或6计算不良JAR中的条目,则会收到错误消息(不过我不确定)。

    我还尝试运行爆炸的JAR。我已经将整个JAR解压缩到一个目录中,然后像这样运行整个程序。
    java -cp <dir/ com.mycompany.test.Start

    像魅力一样工作。

    底线。不要过度使用 maven-shade-plugin

    我有一个正在工作的项目,在这里我要建立我的项目。
  • 我使用 maven-dependency-plugin 复制项目的依赖项。 checkout copy-dependencies goal。如果我没记错的话,这会将您的依赖项复制到target/dependency中。
  • 使用 maven-jar-plugin ,使用插件配置中的这些选项,将这些依赖项作为MANIFEST.MF条目添加到我的最终JAR的Class-Path中。
    <classpathPrefix>dependency/</classpathPrefix>
    <addClasspath>true</addClasspath>

    因此,我将拥有Class-Pathdependency/<artifactId>-<version>.jar条目。
  • 之后,我使用 maven-assembly-plugin 创建一个发行版ZIP,其中包含我的最终JAR和整个dependency/文件夹。
  • 部署应用程序时,可以像java -jar final.jar一样运行它。

  • 最初,我选择使用此解决方案,因为在我的项目中,我使用了一些Bouncy CaSTLe JAR,这些JAR在其 META-INF目录中都有一些奢侈的东西。当我使用 maven-shade-plugin创建我最终的可运行JAR时,一切都变得一团糟,并且我很讨厌找不到该方法,并且此签名不是很正确的错误。

    您也应该这样做。这个Maven阴影业务太阴暗而无法使用(双关语意)。

    这是上面的 a blog post on the whole process that I've tried to describe(感谢
    baba),也许将来会对某人有所帮助。

    关于java - Maven阴影插件构建的无效或损坏的JAR文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13021423/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com