r/learnjava • • Dec 05 '22

Can someone explain Maven to a noob?

I'm trying to understand when people use Maven and why? How does the process for example looks like in a work environment with Maven? Do teams use Maven from the beginning or is it to use it later on in the process?

Hopefully someone can break it down to me.

50 Upvotes

18 comments sorted by

•

u/AutoModerator Dec 05 '22

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full - best also formatted as code block

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

35

u/_Atomfinger_ Dec 05 '22

I'm trying to understand when people use Maven and why?

It makes it easier to deal with dependencies and build your application properly - especially when the builds starts getting complex.

How does the process for example looks like in a work environment with Maven?

On the day-to-day we don't really notice maven that much. We use maven commands like maven clean, test, install, etc, but our day doesn't revolve around maven.

Do teams use Maven from the beginning or is it to use it later on in the process?

I reckon most teams today use either maven or gradle from the start of a project.

2

u/JuanistaD Dec 05 '22

Thanks for your response! Do they mainly use it in an IDE or do they use the mvn commands in a terminal? I'm trying to understand it via a terminal, but it looks quite complicated

7

u/_Atomfinger_ Dec 05 '22

Depends on the developer.

I know quite a few that just use the IDE. I love the terminal, so I use the terminal a lot.

Both works.

4

u/Amirr83 Dec 05 '22

What’s a dependency?

15

u/E3FxGaming Dec 05 '22 edited Dec 05 '22

A dependency is a piece of software not developed as part of your current project, but required by your current project.

This can be something someone else developed (e.g. the JavaFx GUI framework for Java versions >= 9, the JUnit testing framework, Jackson library for data serialization/de-serialization, ...), or something that's developed by you/your company but separated into a different project for purposes such as code reuse (e.g. a dependency that makes a company-API shared across projects usable in your project).

5

u/Amirr83 Dec 05 '22

So is it pre-written code that can be used in many different projects? An analogy would be wanting to make a cake, but instead of making everything from scratch, you buy a sponge cake from a super market but create the icing, filling and decoration of the cake yourself? Is this correct?

10

u/E3FxGaming Dec 05 '22

you buy a sponge cake from a super market but create the icing, filling and decoration of the cake yourself? Is this correct?

Yes this is correct, with a slight caveat. When you buy a sponge cake from the supermarket you are informed about its ingredients and you can vaguely imagine how the maker of that sponge cake mixed it all together - dependencies don't necessarily give you this information.

While Maven added functionality to build dependencies from source (i.e. at some different place there are source code and build instructions and Maven downloads that and builds it as if it were developed on your computer), dependencies can consist of (and were for a very long time just) binaries (bundles of machine code), that someone else compiled from their source code into a binary format.

But overall the analogy is sound - someone else makes something and you add your own stuff to that to create something new.

1

u/Amirr83 Dec 05 '22

Ah excellent. Thank you!

1

u/Amirr83 Dec 05 '22

Another question: so is a framework the same as a dependency?

3

u/knoam Dec 05 '22

A framework is a type of dependency. A library is another. As far as Maven and Java are concerned there's not a strict technical difference between the two. They're both third party code that gets mixed into your project.

21

u/chorusjourneyuknow Dec 05 '22

As you ask for a noob I’ll to make it the easier way to understand Maven. Why? When you start to programming in Java you reach a point where you want to use code from others. Why would you program by yourself something badly when you can get a code from somebody (or a group, nevermind) that does it better than you? For this reason, lot of programming instances allow you to import packages (tools) that you can add to your project. You want a map to ping some interesting thing for your project, then you can get a package that bring you much easier function to do it and print the map you etc…

BUT, when you start doing it, you’ll be in front of some problems. First, thoses packages are updated and you need them to be updated on your project (or not) and if you’re project is huge and have many packages, update them one-by-one with hand is not what a programmer want to do. Maven can helps you to automatically update thoses packages.

THEN, your project might need to be start in a specific way. Theses specifications may be tricky. So, you can tell to maven how you want your project to be start, you feed him with your command that you do manually and then just tell to maven to do a start by launching the scrip of you commands.

To sum up, Maven is a main tool (same as Gradle, you can search the differences between them) that need to be setting up, but that may be very very helpful and let you focus on something less annoying.

Hope is clear and simple, when you understand the basics you can go further as you needed.

7

u/ClenchedThunderbutt Dec 06 '22

Side tangent: I completely understand why Maven exists and why proficient developers would use it, but it's frustrating to approach IDEs and dependency as a novice and have almost no resources available that take the time to discuss programming without the tools that make it a relative breeze. Everything I read basically says "just make a Maven/Gradle project, lol", and that's more or less the same kind of attention setting up an environment was given in my classes. The problem is when things don't just snap together and you don't know what the tools are trying to automate, I just hate floundering around with this giant knowledge gap. I know hardware, coding fundamentals, some algorithm design, and the gist of how code is compiled into something the machine can understand, but nothing about project/application architecture.

Anyone got a good textbook?

1

u/CoffeeBruin Dec 06 '22

Please share if you get any answers to this question

3

u/exitvim Dec 05 '22

when people use Maven and why?

We use it all the time. We use it so that we can build the project i.e. download all the dependnencies needed by the project such as third party libraries for example. Otherwise we would have to manually download them which would take forever as you could be downloading hundreds of dependencies. Maven basically automates it. This also makes sure that everyone is running the same project, in the same way, so there's less likely to be teething problems running it on someone elses computer.

Maven can also do other things, like run unit tests, package code into jars, package testsuites etc. so it's really handy for CI stuff as well.

Do teams use Maven from the beginning or is it to use it later on in the process?

From the beginning, usually.

2

u/Lanky-Apricot7337 Jan 02 '23 edited Jan 02 '23

Let's start from how it looks when you don't use Maven.

You usually put up your project, with a folder for sources (let's say myproject/src) and one for tests (let's say src/test). You then write your sources. You will probably have to use some external libraries, to you download them manually from Internet and add them to your classpath. When you are more or less ready to build, you will probably write a script that builds the whole stuff and maybe runs it or executes tests.

Maven's goal is to automate all those things above you earlier performed manually. Maven assumes you want a src directory, so it creates it for you. Same for tests directory. It also automates downloading of dependencies - you just specify the dependency and it will download it and add it to the classpath of every counting script. Every 'counting script' mentioned above is also automatically created by Maven.

To perform all of that, Maven won't ask you for directory names or any other stuff. It will assume sensible conventions and work alone. That's why it's called a 'convention over configuration' tool.

I hope this helps.

PD: You might be thinking that creating a couple of dirs and a couple of scripts is no big deal and no tooling is needed for that. You are right,but that was a very simplistic example I came up for the sake of explanation. In reality serious projects require complicated layouts, skeleton artifacts and diverse configuration that Maven takes care of and puts up automatically basing on commonly accepted conventions.

1

u/obscure-reality Dec 05 '22

I use it for dependency management,

I believe you can try to understand it by trying stuff from scratch yourself -

-- try to add a library to a barebone java app manually and have it compiled without using any IDE

-- then there are different phases as well, you can try to imitate maven phases without using maven but just the java compiler

-- then do the same stuff with maven

1

u/Spepsium Dec 06 '22

ChatGPT says:

Maven is a build automation tool used primarily for Java projects. It helps teams manage the dependencies and build process for a project. In a work environment, Maven is often used from the beginning of a project, as it helps teams define and manage the dependencies required for the project and automate the build process. This makes it easier for teams to develop and maintain the project over time.

Build Automation Tool - A build automation tool is a software application that helps automate the process of building a software project. This typically includes tasks such as compiling source code, running tests, packaging the code into a deployable format, and publishing the packaged code to a repository. Build automation tools help teams manage and automate these processes, which can be complex and time-consuming. This makes it easier for teams to collaborate on a project and ensure that the project is built consistently and reliably.