How to create an executable jar with 3rd party dependencies included in the jar

If you have written a small java utility that has dependencies on other 3rd party jar and you would like other people to use it without building your jar, you will need to include those jars in your jar. To do this you need to use the maven-assembly-plugin .

So inside <build> ... <plugins>  of your pom.xml put

<plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>your.fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
</plugin>

Then run maven using :  mvn clean compile assembly:single

No comments:

Post a Comment