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

Show parent comments

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.

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