r/javahelp • • 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!

17 Upvotes

20 comments sorted by

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).

2

u/96dpi Dec 29 '19

But what is a dependency? I tried googling, but it seems like a broad term and does not refer to any specific thing. Can you provide an easy example to understand for someone who has yet to take a data structures course?

6

u/ColetBrunel Dec 29 '19

Let's say you want to create PDF files, to export bills made out of some text organized in a page. Java out of the box doesn't provide anything to create PDF files.

However, if someone knows how PDF files are made, they can program it by themselves, putting the right bytes in a read/write RandomAccessFile so that it is a PDF file. That can totally be done in Java if you know how to.

But not everyone knows how to make the right bytes for a PDF file, and even if they knew, they have better things to do. So instead, there is someone, somewhere, who programmed a simpler way to make PDF files, and can be re-used. Then they published it, in the form of a jar file that contains all the necessary classes. This jar file can be imported in whatever project needs it. This is called a dependency.

The problem of dependencies becomes harder when you start to realize that dependencies have their own dependencies and gathering the whole thing becomes tiresome after a while. That's why a dependency manager that has a public dependency repository does all the work for you as long as you can simply name the dependency you yourself are interested in, becomes a huge help.

(Well, in reality, there is more than one guy who made PDF enabling libraries. There are a few different ones around. But not everyone re-does PDF writing/reading just because they need it)

Also, it works particularly well in Java's ecosystem which is full of opensource/free-to-distribute libraries that anyone can put in any public repository they want as that is explicitly allowed. Because of that, even if you do need dependencies that aren't freely redistributable, the mere fact that you're using Java and the norm here is to use a Maven repository, will make your library provider willing to help you out by providing realistically easy solutions to manage their library using Maven.

2

u/96dpi Dec 29 '19

This is perfect, thanks so much!

So in theory, you don't need Maven, it's just a big QOL improvement that you probably don't want to go without.

2

u/sbhandari Dec 29 '19

Yes, you do not need maven. If you manually download all your dependencies, you can add them in your project classpath and should be fine. If the project is very big and needs lots of steps to make jar/war/ear , then you can use build tools like 'ant' .

1

u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 30 '19

So in theory, you don't need Maven

This is very theoretical because in any non-trivial project it will get complex fast. Dependencies also often have their own dependencies.

1

u/MyNameIsRichardCS54 Dec 30 '19 edited Dec 30 '19

(/u/sbhandari) then you can use build tools like 'ant'

He he he. No. Not unless you have strong masochistic tendencies.

Maven is also a build tool as well as a dependency manager. For example, running mvn compile in your project root directory, will build your project, running mvn package, compiles, runs your tests and produces your jar files. It does so much so well and makes life so much easier that once you get used to it, it almost physically hurts to do things manually or use ant again.

Gradle is an alternative that some developers prefer. It uses maven central to get dependencies, is a build tool and has much the same feature set as maven. Probably due to familiarity, I prefer maven even though it's pom.xml syntax is more verbose than the equivalent build.gradle.

1

u/sbhandari Dec 30 '19

That response was for OPs hypothetical question that of it is possible to build/deploy without maven. Gradle and Maven both can manage dependencies and are also a build tool, while ant is only build tool . So, of course ant is going to need lot more extra work.

About gradle using maven central for dependencies, that is not entirely correct. You can use any repository you want, you just need to specify them.

2

u/MyNameIsRichardCS54 Dec 30 '19

Yeah I know. I just made an attempt at a humorous "but don't do that!"

1

u/phoulgaux Dec 29 '19

Dependency is basically anything your code uses that is not within your code. In the context of Maven it means any external library (for example a JAR file) you want to be available for your program.

1

u/joranstark018 Dec 29 '19

A "dependecy" could be a jar-file that provides a functionallity that your project make use of (ie some math library, some string utilities or communication library). Some libraries provides trivial, but convient, functinallity like StringUtils.isEmpty(someString), or non-trivial like RestTemplate.getForObject(uri, someClasd).

(More about Maven dependency)

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:

Maven by example

Maven the Complete Reference

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.