【问题标题】:Maven duplicates all my .class files in the JARMaven 复制了我在 JAR 中的所有 .class 文件
【发布时间】:2016-04-18 05:58:42
【问题描述】:

我一直在我的 JAR 上运行优化器。我一直想知道为什么。今天想替换单个.class文件,打开jar看到/cz/autoclient/目录下的这个:

难怪档案馆无法处理这种混乱局面。我找到了some advice on maven-compiler-plugin 配置,但没有帮助。这是我在pom.xml 中的构建配置:

  <build>
    <testSourceDirectory>${test.dir}</testSourceDirectory>
    <sourceDirectory>${src.dir}</sourceDirectory>
    <resources>
      <resource>
        <directory>${resource.dir}</directory>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <compilerArgument>-proc:none</compilerArgument>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2-beta-4</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>cz.autoclient.Main</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>src/</classpathPrefix>
              <mainClass>cz.autoclient.Main</mainClass>
            </manifest>
          </archive>
          <outputDirectory>${project.build.directory}/</outputDirectory>
        </configuration>
      </plugin>

    </plugins>

  </build>

我真的需要解决它。除了 JAR 优化器,它还破坏了 java 官方工具:

jar uf AutoClient.jar version
java.util.zip.ZipException: duplicate entry: cz/autoclient/autoclick/ColorPixel.class
        at java.util.zip.ZipOutputStream.putNextEntry(ZipOutputStream.java:233)
        at java.util.jar.JarOutputStream.putNextEntry(JarOutputStream.java:109)
        at sun.tools.jar.Main.update(Main.java:630)
        at sun.tools.jar.Main.run(Main.java:269)
        at sun.tools.jar.Main.main(Main.java:1233)

是什么导致了重复的类文件以及如何解决这个问题?只有/cz/autoclient/ 中的文件——即编译后的源代码——似乎是重复的。

【问题讨论】:

  • 请发布您的完整 pom,或 maven 运行的完整日志。 ${src.dir}、${test.dir} 和 ${resource.dir} 的值是多少?它们可能重叠。您可能对程序集插件使用的东西有重复。

标签: java maven jar


【解决方案1】:

首先你应该删除它:

 <testSourceDirectory>${test.dir}</testSourceDirectory>
 <sourceDirectory>${src.dir}</sourceDirectory>
 <resources>
    <resource>
      <directory>${resource.dir}</directory>
    </resource>
 </resources>

除非您 100% 知道自己在做什么,但根据您的帖子您不知道。所以删除它..

此外,使用更新版本的 maven-assembly-plugin 而不是那么古老的版本...请参阅此处:https://maven.apache.org/plugins/

【讨论】:

  • 从那个确切的链接复制我的插件的版本号解决了这个问题,没有任何其他更改。
【解决方案2】:

检查匿名类(在我的例子中通过 IntelliJ)可能会显示(在我的例子中)动态地图创建等内容

myMethod(new HashMap<String, String>(){{
    put("1", "2");
    put("3", "4");
    put("5", "6");
}});

在我的班级中出现了 12 个,warfile 中有 12 个额外的类文件。

【讨论】:

    猜你喜欢
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 2011-05-14
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多