Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven-dependency-plugin generates duplicate files in jar-with-dependencies.jar file

i am creating a standalone java application with maven, and i am including the dependencies in the jar file with maven-dependecy-plugin as follows:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>theMainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>create-my-bundle</id>
                            <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                     <configuration>
                       <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                       </descriptorRefs>

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

this includes the dependencies in the generated jar file in a lib folder, and the jar runs and works fine, but the issue is in the other generated jar file appname-1.0-jar-with-dependencies.jar.

ISSUE: i am not sure if it's an issue or not, but i noticed in target folder in the generated appname-1.0-jar-with-dependencies.jar, that it contains duplicate application files like:

  1. all sql,property files,xml files exists twice.
  2. all java classes .class file exists twice.
  3. there are lots of overview.html and license.txt files related to the dependencies.

i am not sure if that's feels right or not, also i need someone to clarify for me what is the importance of this generated jar file, since i am not familiar with this plugin.

please advise, thanks.

like image 950
Mahmoud Saleh Avatar asked Oct 23 '22 14:10

Mahmoud Saleh


1 Answers

Since you have mentioned jar-with-dependencies, I assume you are using maven assembly plugin to assemble your project artifacts along with the dependant jars into a single jar.

I suspect that your project artifacts are getting into the jar-with-dependencies twice - due to a property useProjectArtifact of dependencySet which is true, by default. You can set this property to true in your assembly descriptor and see if it addresses your issue.

In the specific case above, maven dependency plugin does not seem to be doing anything useful. maven assembly plugin automatically packages all its dependencies into a single distribution as per configuration.

But do note classpath issues if you create an executable jar-with-dependencies. You may want to create a zip or tar.gz instead.

The configuration used above is the default and does not allow for customization. You may want to use an assembly descriptor file, where you can set the property mentioned earlier or other options.

like image 164
Raghuram Avatar answered Oct 26 '22 23:10

Raghuram