Search code examples
javamavenjar

Maven duplicates all my .class files in the JAR


I've been having running optimizers on my JAR. I've always wondered why. Today I wanted to replace single .class file, and when I opened the jar, I looked at this in the /cz/autoclient/ directory:

image description

No wonder archivers couldn't handle that mess. I found some advice on maven-compiler-plugin configuration, that didn't help. This is my build configuration in the 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>

I really need to resolve it. Apart of the JAR optimizers, it also breaks java official tools:

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)

What causes the duplicate class files and how to resolve this issue? Only files in /cz/autoclient/ - that's the compiled source - seem to be duplicated.


Solution

  • First you should remove that:

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

    Except you know 100% what you are doing but based on your post you don't. So remove it..

    Furthermore use an more uptodate version of maven-assembly-plugin and not such ancient version... See here: https://maven.apache.org/plugins/