user2761431
user2761431

Reputation: 985

Creating jar with external dependencies and other jars in Maven

Step 1 : I have a Utils.jar that I have to include in my Project1. I need the Utils.jar to contain all the maven dependencies within itself so that I don't have to specify the same in Project1 pom.xml.

Step 2 : I need to create Project1.jar and include it in Project2. I again need a way such that I do not replicate the pom of Project1.jar in Project2 pom.xml. Also, I need the Project1.jar to include Utils.jar so that I don't have to add Utils.jar to Project2.

What is the right way to do this?

I looked into the Maven assembly plugin, but it requires specifying the Main class. I do not have a main class in my Utils project. I am not sure what I am missing.

Upvotes: 0

Views: 1266

Answers (2)

J Fabian Meier
J Fabian Meier

Reputation: 35853

It is normally not a good idea to build a utils.jar as fat jar (containing the dependencies).

If you add a dependency to utils.jar in Project1, then all dependencies of utils.jar become dependencies of Project1 automatically. This is the Maven dependency resolution. You do not need to copy or retype the dependencies of utils.jar in Project1.

Upvotes: 0

Ani Aggarwal
Ani Aggarwal

Reputation: 361

What you want is called an uber jar or fat jar. Here is an article on how to do it.

https://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/

Hope it helps

p.s. main class is not needed except for when you need to make the jar executable.

Upvotes: 1

Related Questions