5

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.

Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
  • What do you mean by "exist twice"? Two files cannot have the same path--what are the complete paths you're seeing? The plugin creates a jar file with all the project dependencies in it, so it can run standalone--not sure why else you'd use it; why did you *start* using it? – Dave Newton Dec 22 '11 at 22:31
  • when i view this jar with win rar, i can see for example two copies for each .sql,.xml file in my application. – Mahmoud Saleh Dec 22 '11 at 22:42
  • and i am using it because i want to make a standalone application with all dependencies inside it, is that a bad practice ? or should i use another plugin ? – Mahmoud Saleh Dec 22 '11 at 22:43
  • That's what the plugin is for. Regarding double files--I'm asking what the complete path is, because two files cannot have the same name. – Dave Newton Dec 22 '11 at 22:51
  • i can't tell the path,double files are inside the jar file, for example on the root of the jar file when you view it with winrar you will find two files named mydata.sql – Mahmoud Saleh Dec 23 '11 at 09:58
  • 1
    @DaveNewton -- MahmoudS is exactly correct. The JAR format can hold duplicates files (exact same path & name), and this is a known issue with maven-assembly-plugin. See: http://stackoverflow.com/questions/10308916/maven-generates-duplicate-pom-xml-and-pom-properties-files-in-a-jar – Thomas W Dec 23 '13 at 04:18

1 Answers1

3

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.

Community
  • 1
  • 1
Raghuram
  • 51,854
  • 11
  • 110
  • 122
  • oh sorry i forgot to mention that i am using the maven-assembly-plugin along with the maven-dependecy-plugin, i updated the code, do i have to use the assembly plugin along with dependency plugin to get my dependencies in the project jar ? – Mahmoud Saleh Dec 23 '11 at 12:04
  • @Msaleh. Updated answer to address the queries. – Raghuram Dec 23 '11 at 12:29
  • what can i see is that maven dependency plugin gather all jars and put them in a lib folder inside the application jar, so it does something useful here. and can you please guide me to a better configuration to gather all pom jars in the application generated jar ? – Mahmoud Saleh Dec 23 '11 at 12:59