r/javahelp • u/Lvl20Rogue • Dec 29 '19
Understanding Maven
So i'm recently getting into maven for the first time in my life. I'm a bit confused and i think it's because i don't really know what maven does. I have this POM.XML file in my project. Do i just add a dependency there and maven will magically find the JAR and download it for me, or do i need to somehow also manually download the JAR and the POM just points at it?
Help would be greatly appreciated!
4
u/FrenchFigaro Software Engineer Dec 29 '19
Maven combines two tools: it's a build tool on one hand, and a dependency management tool on the other.
It's also useful in that it gives a structure to your project so that you can share it without having to share the project files specific to your IDE (so that if I prefer eclipse and you prefer IntelliJ, we can use whatever source management tool we like and work collaboratively without too much hassle)
Without a dependency management tool, you have to manually keep track of your dependencies, and of your dependencies' dependencies. This is what is called a dependency tree.
The problem is that for your project to compile and run smoothly, you need to have flat dependencies. Here's an example:
You use a library A, and a library B. These are your dependencies. But library B uses library A as well, and library C. These are B's dependency. B could be packaged in a standalone form that includes A and C.
The problem with that solution is that if you include both B and A, you might run into a conflict. Another one, is that B's binaries are no bigger because they include A and C's binaries. And finally, when you share code, you will either have to provide A and C (which might not be permitted by the licence) or force every new developer on the project to download the right version, which might be a source of error.
What maven does first, is that it flattens the dependency tree. In this case, it sees that you need A and B, and B needs A and C, so it downloads each one only once. As another user explained, it first checks in your local repo, and then it downloads from the distant (which is maven central by default but can be configured to something else).
There still might be some conflicts, for example if your project and library B do not use the same version of library A, but maven has defined behaviour to solve those automatically, or through configuration.
Maven also allows you to define scope to dependencies. For example, if you use Mockito for your unit tests, you can assign it the scope "test", so that the dependency is only used when running the tests, and not included in the final build (jar, war or ear). If your application runs on a server that provides the dependency B, you can assign it the scope "provided", and the dependency will not be included in the final build either.
3
u/XiiMoss Dec 29 '19
The POM tells maven to find that dependency on the maven repository. You just need to provide the correct details and maven will download it for you.
1
u/MyNameIsRichardCS54 Dec 29 '19
Maven will get the jar for you along with any other jars needed as dependencies.
1
u/StormsRider Dec 29 '19
Check out this screenshot guide for Idea Intellij: https://imgur.com/a/LuMHMKI. It shows how to add and install dependencies.
1
u/itsnancyn Dec 29 '19
*Disclosure I work at CloudRepo
CloudRepo has a few articles that might answer your questions.
What is a maven repository: https://www.cloudrepo.io/articles/what-is-a-maven-repository.html
Public maven repositories: Maven Central and more - https://www.cloudrepo.io/articles/public-maven-repositories-maven-central-and-more.html
Don't hesitate to message me if you have further questions!
1
u/my5cent Dec 29 '19
Is maven equivalent to how other language maintain depencies by locking down versions.
1
u/gwynnbleidd129 Dec 29 '19
In addition to the other answers, if you have the time and are interested in the details, the following helped me years back:
1
u/wsppan Dec 29 '19
Maven is a tool to manage all your external dependencies. It also manages your tests, builds, and deployment artifacts.
1
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 30 '19
Do i just add a dependency there and maven will magically find the JAR and download it for me
Aside from what others said; there is nothing 'magic' about this. It simply goes and looks in any maven repositories for the .jar file of whatever library you added. These maven repositories are just web servers with a fixed layout. So for example if you want to download the junit library it has a group (often the company name), artifact name (junit) and a version. So downloading junit 4.12 in the junit group, it would go and download https://repo1.maven.org/maven2/junit/junit/4.12/junit-4.12.pom and then https://repo1.maven.org/maven2/junit/junit/4.12/junit-4.12.jar
It downloads the pom.xml for junit itself to check if it needs to download additional dependencies. It knows where to find these because the POM is always at https://repo1.maven.org/maven2/{group}/{artifact}/{version}/{artifact}-{version}.pom
By default Maven has two repositories it will look in, your local cache ($HOME/.m2/repository/) and maven central (repo1.maven.org). You can add more repositories either in your settings or in the project's pom.xml.
In addition; one thing to know is that artifacts that have SNAPSHOT in their version get treated somewhat differently. Maven will try at least once a day (IIRC) to get that from whatever snapshot repository you defined, but it will not do that every single time. But that's something you probably won't run into often.
13
u/joranstark018 Dec 29 '19
Maven will automagically download all dependencies for you.
Maven will download and cache dependencies locally. If you add a new dependency it will first look in the local cache, if not fond there it will search the external repositories, if not found there it will fail the build with an "unresolved" error.
By default Maven will use Maven Central as remote repository you can change this if you have some corperate repository or if you want to include a repository from some third part, it's a list of url:s).