Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export maven as jar file with all dependencies

I have my own java library created as maven project and has some dependencies included in pom.xml

I want to export project as jar and include it into others maven projects.

The problem is that I need to copy all dependencies from pom.xml of my library into maven projects where is imported my library to make it to work.

How to export my library to not be necessary to copy dependencies of my library.

like image 629
KunLun Avatar asked Oct 17 '22 17:10

KunLun


2 Answers

That is easy to do; the central feature of Maven is that it manages the project dependencies for you.

You need to mvn install your project from the command line; that will install the jar and the pom files to your local repository.

You can then include your library as a Maven dependency in other Maven based projects; Maven will resolve the (transitive) dependencies for your project.

like image 176
Daniele Avatar answered Oct 20 '22 23:10

Daniele


Normally you don't need to list all the dependencies in the project that imports your library. Maven should fetch them for you. What you need to do is to declare dependencies in your library.

Make sure you declare correct types of dependencies. Here is more info. In your case you need to make sure that dependencies you want to copy to the downstream projects are marked as 'compile'

There are tools that make 'Fat' jars by copying all dependencies inside. But they are mostly used to build the final project such as a deployable WAR file or a desktop app. Not in case of the libraries

like image 32
Vlad Avatar answered Oct 20 '22 23:10

Vlad