Why use Maven for Java? - Java Brains

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so if i were to ask someone why do we need maven vastu java developer what's the point of using maven there's a good chance that i'm going to get one of two answers i'm going to get an answer that it is for managing dependencies r another common answer that i get is that it's a build tool okay even as a build tool that allows you to build java projects well maven does have a very popular repetition of being a built tool but that's actually not the primary goal of maven the primary goal of maven is a project management tool right it wants to be a project management tool managing your java projects what does it mean what does managing a java project mean let me ask you a question if i were to say okay you're a java developer and i say hey can you make a java project for me can you create a java project for me what do you do well one thing that comes to mind is you open an ide right you open something like intellij and you create a new project where you open eclipse and create a new new project but if i were to tell you oh hang on i don't want it to be ide specific i don't want to be attached to an ide i want you to create a project in general which works across ids and it's not associated with an ide okay well think about it how would you do it it brings into question the concept of a project in java right how do you create a project in java a typical way is an ide of course an ide allows you to kind of start with that right you know you can give it a name you say okay i'm working on this project you give it a name of your application i say here all your source code yours are your test files so the ide provides the structure for you but if you were asked to do this irrespective of the ide right have an idea agnostic concept of a project so how do you do it well it turns out there is no such concept of a project in java okay in java you basically throw in a bunch of classes or java files and then say okay these all work together to build an application well the application is in your head right it's not something that you manifest as a java thing okay it's just a group of classes a group of java files some xml files some json files configuration and then you have you know assets and if you're building a web project the best you can do is put them all in a directory and say well here's your project but then there's a whole lot more that goes to being a project okay so think about this what does a java project need to consist of what's the kind of stuff that a java project needs to consist of first of all it has to have the source code of course right all your java files has to be there next it has to have all their test cases all your test files right if you're using something like junit all your test java files need to be there so a java project needs to consist of this and are you done well no not only do you need to have the files you also need to have the structure for those files you need to know which directory contains which files which files goes where like if you have assets you know where does that go images where does that go resources where does that go you need a structure for your project so you're going to have to manage that hierarchy manage okay i'm going to designate this to be the source directory and how your assets are managed how your packages are managed all those things have to be managed in a java project okay next you need to manage libraries and dependencies most often if you're working on a hello world project you know you're you have just one class right you don't need a lot of dependencies but what if you have a lot of external dependencies external charge and all that you're going to have to manage that right you need to have some way of going to those websites downloading those jars adding it to the class path well it's not enough if you just do it once right you have to share this project with your colleague you have to give it to them and say hey go download those jazz from those websites they're gonna have to do all that themselves right so it's another thing that you're gonna have to manage with your java project is that done well no you have other stuff you have configuration right there's a lot of configuration that specifies how certain jars work there is configuration that specifies how your application works and those configuration files need to reside somewhere right this is another thing that you're going to have to manage then you have task running to do you give somebody a java folder which contains all the java files and they're going to say now what do i do how do i run this thing right again you have to tell them hey you have to go run these commands this is how you compile it your java project might have 10 classes or 50 classes you're going to have to compile all of them and then you're going to have to test okay this is a command to test your java code there's a command to run your java code you need to have the ability to run these tasks and not just that you need to have the ability to run those tasks in the uniform way so that if 10 people are working on the project that you've checked into source source control all those 10 people better be running the same commands for building and testing and compiling and all that all right and then the final thing which is not super important as opposed to the other things that we talked about still i think it's a good to have is some kind of a reporting structure okay you need some way of reporting and saying okay how is my project doing okay what are the number of classes that i have what are the dependencies that i have what is the pass rate for test cases right what is the test coverage so these are all some kind of reporting that you want when you have something called the project right you need some kind of a check for how the project is doing so these are all the things that any typical java project would need and it turns out the language does not have support for something called the project at least not in an ide agnostic way so this is where maven comes in maven says i'm going to manage this project for you i'm going to tell you where the source code lies i'm going to tell you where the test lies i'm going to manage the project structure manage dependencies configuration i have the ability to run commands and then do the building and the testing and all that stuff and even also throws in reporting for good measure right so maven manages java projects it takes these group of you know assorted class files and resources and all that and groups them into one thing called a project and the wim even does this is using something called the project object model we're going to be looking at what the project object model is later in this course but this is the primary reason why somebody uses maven to manage a single thing give it an entity called a java project right okay so maven manages java projects what is it and how does it do it well even it's an open source project it's actually an apache project and it is something that you can install on your machine and have it manage your java projects for you okay so how does it do it maven provides this structure for any java project and it manages the contents of that project okay so you know we talked about what are the different things that could be in a java project it kind of manages it for you by providing that structure it comes with this opinion of how that structure should be where all those different things should go and then it provides you the structure and it manages it for you okay secondly it helps manage dependencies for your project you have a bunch of dependencies on other libraries or frameworks or anything of that sort you don't have to go to the websites download search for jars and all that stuff maven has a really elegant solution for managing your dependencies so maven is also very configurable it has this project object model which it uses to track your project and you can make tweaks to it it is opinionated but it listens to you if you say i want it to do things differently and then maven provides these tasks which allow you to do build and compile and deploy and all those things it comes with a task runner and you can say this is the order in which you want the tasks run even is going to handle all of that for you this is not all of course there's a lot of other features but these are some of the important things that you should know there are certain characteristics of maven that you should be aware of so the first characteristic which really stands out is this thing about convention over configuration what does that mean it means that there is a certain convention that you can follow when you're working with maven so that you don't have to tell it a whole lot as long as you're following the convention maven knows all of the necessary information about your java project okay so it is opinionated it has its own convention as long as you follow it no configuration required but it allows you to configure it as well okay so if you say i don't like this portion of the convention you can configure it so it is both are possible but then if you follow the convention you don't need the configuration and that's what conventional configuration means maven is very uh conventional over configuration oriented it comes with a lot of conventions uh that convention makes it consistent you can go from one maven project to another maven project and things are going to look similar because they're both following that same convention right uh having said that you can of course also configure it it is also configurable so for example uh maven expects the source directory to always be src main java okay src main slash java that's where your source files should be that's the convention if you put your source code in src main java maven knows exactly that that's where it's looking for source code right so it knows exactly where it is it knows where to find it but you can configure it to look somewhere else for source code right that works too by default it is looking at a specific convention all right so here are some characteristics other characteristics of maven first of all it's id agnostic you don't need an ide to use maven you don't need an id to create a java project and build it and deploy it and all that maybe with me even you can do pretty much the whole development life cycle without opening an ide at least the syntax highlighting and the autocomplete and all you will obviously not be there if you open it in a simple text editor but then all the other stuff the project management stuff maven's got your back it's going to do all of that work for you even though it's id agnostic it is ide supported pretty much every major java ide has support for maven you can open the maven project in an id and it's going to treat it just like the experience is going to be just like you were opening an intellij project or an eclipse project since mayman is opinionated the ide too knows where the source code is and where the test files are and it can easily open main projects work on even projects in an ide right all ids pretty much all ids support maven maven also has this concept of declarative dependency management there's a full unit about dependency management in this course if you're going to be looking at how dependency management in maven is declarative okay as opposed to imperative you know we have to get a jar and add it to the class path there are some imperative steps you have to do go to the website download it copy it those are all the steps you're going to have to do but with me that's not what you do but may when you say i need apache comments you declare it you specify your need for apache commons and then maven is going to go fetch apache comments and make it available in your class path okay so the way you add a dependency is by declaring it we'll cover more later in the course and then finally maven has a very plug-in based architecture right most of the features of maven that we're going to be looking in this course is all powered by plugins it adds functionality to maven and it also makes it extensible you can have maven do different things by adding different plugins and there are a lot of different plugins which allow you to extend maven so that's a very plug-in based architecture plug-in heavy architecture all right so these are some of the characteristics of maven let's move on to actually working on maven now that you have a little bit of an idea of what maven is we're going to configure our development environment install maven and we'll take it from there so see you in the next tutorial you
Info
Channel: Java Brains
Views: 36,899
Rating: 4.9585919 out of 5
Keywords: java, java brains, tutorial, brains, koushik, kaushik, explained, programming, koushik kothagal, beginner, maven, apache maven, maven tutorial, project object model, pom, java project
Id: 2axtcJHP0RA
Channel Id: undefined
Length: 12min 7sec (727 seconds)
Published: Fri Sep 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.