Search code examples
javamavenjarmaven-dependency-pluginuberjar

Maven package in-project repository dependencies inside jar


I am using the in-project repository dependency solution to include a third party jar as a dependency in my Maven project. I'm following the instructions on this blog for that.

Now, I want that when I package my Maven project into a jar, the jar that is created should have a lib folder with the third party jar in it. However, I do NOT want the other dependencies to be packaged in the jar. (I don't want a fat jar with ALL dependencies packaged inside it, I just want a jar with the one third party dependency jar packaged inside it).

I have been trying to play around with the maven-dependency-plugin and the maven-jar-plugin, but I've not been able to achieve what I want.

Can someone please help me out?


Solution

  • You can use maven-dependency-plugin (look here) as shown below, this plugin will provide lots of options to include jars which ArtifactId (i.e., <includeArtifactIds>) or groupId (<includeGroupIds>), etc...

    <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>
                    <includeArtifactIds>YOUR_THIRDPARTY_JAR_NAME</includeArtifactIds>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    

    So, the above code will add YOUR_THIRDPARTY_JAR_NAME.jar into your final .jar file's lib folder.