0

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

anonymous
  • 37
  • 7
  • You should look at this https://stackoverflow.com/questions/207281/what-is-the-difference-between-mvn-deploy-to-a-local-repo-and-mvn-install – T.K Nov 17 '21 at 14:09
  • thank you but that's not my point at all, I am not asking about the difference between maven deploy and install, I am just wondering how to jars downloaded as dependencies locally are managed when running deploy, so when I need to run my code, can I run it without having the jar files in the same location? or they should be deployed with the jar containing my code? – anonymous Nov 17 '21 at 14:14
  • Well yes and no because if you'd really know the difference you wont ask your question. But clearly @Szprota21 answered better than me at your question – T.K Nov 17 '21 at 14:17

1 Answers1

1

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.

Morph21
  • 1,111
  • 6
  • 18
  • you mean that everyone wants to execute the uploaded jar, he has to run maven which will download the dependencies and then execute the jar? if that's what u meant what if I want to run this jar in a jenkins? I mean to download the jar from the repository and I just want to execute it, how is that possible? because I don't think it is efficient to run maven and download dependencies before executing the jar – anonymous Nov 17 '21 at 14:19
  • I eddited my answer, hope that helps a bit – Morph21 Nov 17 '21 at 14:32
  • okay very clear, but the point is, when I run maven deploy or package, when reading the contents of the jar file, I can only see my classes, I can't see dependencies that maven downloaded when I was running my code locally. – anonymous Nov 17 '21 at 14:39
  • that's because on default maven won't include dependencies jars inside your jar. Try maven-shade-plugin there are tutorials on google how to set it up, then on mvn clean install you will receive your jar with dependencies inside it. https://stackoverflow.com/questions/13865255/maven-deploy-jar-with-dependencies-to-repo – Morph21 Nov 17 '21 at 14:46