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

View all comments

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!"