I am working on a project, when I run maven test, I get all the dependencies as jars, so I can use methods of classes in those jars when editing the code, however I was wondering when running maven deploy to package the code as a jar and put it on a remote repository, how does this jar execute? doesn't it need all the jars mentioned in the pom? because when reading the contents of the jar it only includes the compiled classes of the code which are under src/main/java. I think there is a point that I have misunderstood. please if there are some basics that I have missed or I should have known refer me with a good guide. thanks in advance
CodePudding user response:
It's like you said, to run a jar you need each dependency. But to put it on repository you don't need that.
So basically when you want to create runnable jar you need some maven build plugins which will pack every dependency inside your .jar file like maven-shade-plugin.
If you want to pack a jar to upload it to remote repository for other people to use you don't need that because now it is on the side of someone who will use your dependency to download each dependant jar of your created one (maven does it automatically).
Example
It all depends on what you need to do.
Imagine that you have a console application which shows you weather for Chicago, that application uses dependency for getting weather lets call it weather-dep so your application needs a dependency of weather-dep inside.
And now if you want your user to just run it (jar can be run from console "java -jar yourWeatherApp.jar") you need to package it in a way that yourWeatherApp.jar will have inside weather-dep.jar which maven will download on packaging process.
Second option is when you know that someone want's to show weather in Chicago using yourWeatherApp.jar so that person makes his application lets call it usaWeatherApp.jar and he will include your dependency inside his application he will then be able to use classes from yourWeatherApp.jar but also from weather-dep.jar because it's dependency inside your's app.
You just need to know which use case is best suited for you.
Short answer you package your jar including dependencies when someone wan't to just run your application and not include it's functions/classes etc. inside their app.