Using Maven in IntelliJ IDEA

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone i am obsession today i'm your host mala gupta today i am pleased to welcome chandragunter director at bny melind a java champion leader of two java user groups international speaker and a java programmer since 1998. hi chandra it's a pleasure to have you present today thank you for having me here thank you jetbrains chandra will talk about using maven in intellij idea starting from creating the maven project working with a bomb file to customization of maven installations run configurations and much more but before i let chandra take the stage let me share some quick housekeeping details please use the youtube chat to post your questions i alexander and vladislav from the jet prince team will answer your questions as you post them chandra will take quick breaks while presenting to you to answer your questions so you don't really have to wait until the end of the presentations the session will be recorded and hosted to intellij ideas youtube channel chandra i'll let you take the stage now thank you so much and thank you so much mada for hosting and um to vlad and um alex for moderating i appreciate that thank you so let's get started uh very quick introduction about myself i know mala already did this so i don't have to spend a lot of time boring you to details but uh here's my main github page i took some paint creating it so i'd appreciate you take a look at it uh that was a joke by the way uh but yeah so here are some of the repos that i'm active in right now um and that's that's me in short i have some small already mentioned i'm a java champion i'm part of the gcp executive committee i represent bank of new york mellon there i've been programming with java for a long time and i'm a juggling two java user groups today we'll be talking about how how we use maven on intellij apache maven um on intellij so for this i created a small project and uh the link should be made available um on the chat i think malays was kind enough to volunteer to do that and this basically is a walkthrough of how you know intellij interacts with maven in multiple ways to give us some extremely good developer benefits and we will look at those using these sample projects um there are two sample projects here there is a multi-module project and there's a single module project uh first the maven single module project has a very simple project that is set up uh the github coordinates are listed here as well in case you wanted to get to it um and it is a simple project setup as a hello world application with a hello world test nothing much to it but we are more focused on the palm xml and seeing how it operates with maven so that is step one and the second mod project will take a look at is a multi-module project now this is multi-module in multiple senses in that it is a multi-maven module it's a multi-java module and it's a multi-intellij module so all three are differentiated and there is some amount of text that helps you sort of understand and relate between maven modules intellij modules in java modules themselves so this is a jpms enabled a java platform modular system enabled project and the project structure is like so you got a root bomb xml file which is a reactor or an aggregator palm we'll talk about that in a bit we have some ide run configurations that are already made available to you i intellij does this for you i just customized the location of where it is located this is module one it is daytime fetcher and it exports a certain number of packages to be used by other modules this is the jpms portion of it it is a maven module in itself and it is also an intellij module in itself next comes our greeting module which does um which uses some of the facilities of the date time module um and it is set up as a java module again uh it's a maven module and an intelhd module by itself and the greeting generator is where the main method exists and this collates information from both the date time and the greeting to create a greeting generator in certain formats and those are the examples we'll walk through and see how maven works on intellij so let's get started so quick overview on how we will work through our um agenda today is we have a few topics that we want to cover um and once we cover a few topics uh i would like to take a break at two points to ask for q a uh and uh mala will help me out by looking at or pouring over the comments through the youtube and and share those with me and i can happily answer those so that's the format we'll go in so please don't hesitate to ask questions i just wanted to keep the questions um sort of bucketed in certain sections they are located with the two dashes that you can see and that's where we'll take breaks for questions so let's get started the first thing we want to take a look at is how do we create a new maven project how do you use a quick start in intellij some of you may be aware of this but all you have to do is go create a new project in intellij and once this comes up you have options on your left navigation one of which is a maven so i don't want to choose 16 16 is too brand new and i don't want to choose 17 so i'm going to go with 15 which is already out now the way you can create a maven project in intellij is you can simply go ahead and hit next and it creates a structural maven project for you with nothing in it so you get what is called a source main java directory as you can see on my left nav here it gives you the directory structure but it doesn't give you anything below say java it creates a source and leaves it there um it also creates a test directory but leaves nothing inside so that's a very plain vanilla maven project and there's not much you can do with that so rather than do that let's skip it and create a maven project from what is known as a quick start archetype now what you do for that is you select create from an archetype and then you go search for maven archetype quick start now it's painful i mean there are so many archetypes how do you find chandra i'm sorry to interrupt you but you'll have to um increase the size of your phone for the intellij idea for the ide okay um so give me a second let me take care of that i have to first cancel this out i guess so bear with me um font i think we changed this i don't know how it magically went back but all right let me know if this is any better so let me just do this again is this better um yes this is much better thank you so much all right great and thank you thank you for prompting me right away about that um so going back to where we left off is we are going to try and create something from an archetype and then we have to go search for our start arctic archetype which is kind of hard now one of the beauties of intellij that a lot of people don't know about is most windows in intellij support type ahead searching so you can actually just go ahead and start typing over here quick start and notice i get a search which gives me a quick start option right away and there it is i find my quick start maven plugin i move ahead and i have to give it some name so i'm going to give it a name of name and demo this is the name of my project right this is the the directory structure name of my project it's called maven demo now i can obviously go change the coordinates that i use for this in this case i would like to call it demo dot cvg dot checked brains so i don't get into any trouble with i guess using names that are not allowed and this is my main demo so i've created an archetype of the same name as my um project folder name and this is generally the norm most people generally tend to do this because you don't want to have different names otherwise they show up as you know the name being different from the actual directory structure which will cause some amount of i guess real estate being used in the project window so best practice try to keep the artifact id name same as the you know directory structure name if you don't yeah you'll just notice some minor inconsistencies but it works fine otherwise too one of the things i definitely do change when i get through this window is i like to always use semantic versioning so three-digit version naming so i do include a dot zero whenever i do whenever i create a maven project so when i hit next it again gives me a summary of everything i use um i can it tells me everything i've set up i've set it up from a quick start project and i use a bundled maven now instead of a bundle maven which is what intellij brings to the table i mean as soon as you install intellij you get maven installed and version 360 is what is currently available on my version of intellij which is i believe uh 2020.3 right um so i could either use this or i could use my own install of maven so um if i were to search for my own install let us say i have tools um and i have apache maven 363 i could choose to use this instead so there are two flavors you can either you can use the bundle maven or you can use them even that is on your system if you are working in a team project you typically want to have a common maven version which is when you would use a version external to intellij if you're developing on your own intelligence bundled maven version works very fine for you so those are things that you should be aware of you could add more properties here we'll go through that as we go along but this is some of the information that you could immediately use so if i were to hit finish on this um a few things happen intellij creates a new project for me and unfortunately it is showing me a background image that should not be there uh but we will fix that later so it created a project right and let's take a quick walkthrough of the project itself so it gave me a source main and under my source main i have a few directories and you will notice that it created an app and then it created um nav test great so i was able to create a few things now ignore these because i have other plugins that i wrote um that helped me create some other information so this is not going to show up on yours it's going to be going to show up on mine but i just wanted to share that this is how it creates some sort of a rigorous test which is a humor itself um maybe project for you now let me just get rid of the background image so we don't actually get confused about it being something else so let me just change this to maven on intellij and get rid of the background right so you notice that it created something but there are a few things that you have to be aware of the maven quickstart plugin is still a bit old so it reverted back to jdk 1.7 instead of the java 15 that i had chosen this is a gotcha that you should be aware of um it has not yet been updated where it would actually go and use the version i supplied it originally creates a form that is jdk 15 and then by the time it does an automated build it somehow reverts back because of defaults elsewhere so those are things that you should be aware of and should change um should you actually go ahead and generate a project using the main the project wizard now in addition what it does is it provides for you a plugin management section and a plugin management section in maven is quite important uh any management section in maven is basically a dictionary of sorts which allows you to define a version of a certain artifact that you use either as a plugin or a dependency so it's a centralized dictionary that you create and anywhere else in your project should you choose to use this artifact the version from this dictionary is what gets picked up so you don't have to specify the version elsewhere if you create a management section so plugin management and dependency management are two different dictionaries that maven maintains and looks up the version from there in order to determine which version you will use so there is an even clean plugin of a certain version a resources compiler surefire etc we'll talk about this as we go along but i just wanted to show you that intellij creates a plugin management section up front for you so you have zero confusion in what versions of the plugin you will use in the rest of your projects right and that is an important thing to have so that is taken care of just be aware that the versions may be dated because of the generator still work focusing on an older version of java so that is that is something to be aware of but it's got very nifty features that um you know you could use um so with that i'm going to close out on generating a project i think we're done with that section so i'm just going to mark this as done okay moving on the next we will create a we'll try to open a maven project um from from from a command prompt and how do we do this so first let's go to a finder window obviously my finder window is very small and very tiny so i'm just gonna very quickly explain to you what i'm loading right so i have a project somewhere let us say i go to conferences and let's say i have paneer tikka masala it's one of the projects i recently spoke about and i would like to open this project right the way to do it in intellij is to essentially go open a project and once you open a project rather than just navigate to the project itself i go to my conferences i go to panitti masala instead of selecting the directory where my project structure is if i were to select the palm xml instead and open that intellij prompts me with a question of should i open this as a file or should i open this as a project now if you were to open this as a project intellij parses through your palm xml and sets up your directory structures accordingly and let us take a quick look at how it does that so notice it not only created my directory structure it also went ahead and created everything that is needed to sort of set it up and ensure that it runs right away so that is something intellij offers out of the box parsing a palm file and therefore creating a project from it without you having to do much this is the standard way in which i usually load um i guess any kind of source code that i checked out if it has a palm xml i am immediately excited because i can immediately load it into my intellij idea so that is something quite useful i found and something i heavily recommend and use so that i'm gonna mark as done and let's move on to the next how do we see the structure view of a palm a structure view is very important i mean if we use our java code for example i'm going to a java code you know you have a structure view which is probably command 7 on most macs and similar on other devices is basically a representation of all the fields and methods you have in a class now the same thing also applies to a palm xml and that is something very few people are aware of right and here is your pom xml file broken down as a structural view we notice that we have a model version a group id artifact id etc we have properties and we have plugins and we have dependencies listed here all this is great but how do i see this as a structural view you notice that my plugins are listing maven compiler plugin and maven surefire plugin and they list up as under plugins here of course even if you expand them there is not much because we have not configured these plugins should we have configured these plugins with other details they would have shown up as you know items under the expanded uh plugin slots similarly we have dependencies listed and you can notice that we have dependencies which don't have any further information associated no exclusions etc so that is something that is listed very easy for you to find out and navigate to so i can actually navigate to whatever plugin i want uh just by clicking on it within the structure view and you should take advantage of the structure view being around it is very helpful so um of course i know you wanted to go to properties it's a much bigger list that doesn't have anything to drill down within the properties yet so that is something um you know worth noting about how may when structure views are represented in intellij great going back to the agenda i think we can mark this as done let's take a look at uh the maven tool window itself um so where is the maven tool window on your um on your ide typically the maven tool window is on the right top and in my case i have shortcuts set up i have a command 5 shortcut setup for the maven tool window you may have something else so you may not have a tool window shortcut at all which is very easy to change you can go to preferences and under key map you can essentially go change the key map for say when i set it to command five right and you can go change this and you could right click um add a keyboard shortcut and i set up my keyboard shortcut that's command five so that is something you can do for your own tool windows and so i don't actually clutter my screen showing all the tool windows i hit command 1 and i lose my project i can hit command 7 i lose my structure and i can get them back with the command 1 and the command 7 and the command 5 gets 3 vacuum even tool window so that's how i've sort of reduced the clutter on my screen itself to make it more realistic for the actual code that i'm looking at here is the maven tool window the maven tool window first shows me a few toolbar icons which allow me to refresh or import maven project we'll talk about this later you can re generate sources we'll go through this as well you can download sources you can add more moving projects right here we'll go through that later and there are a few other options that you have including a settings which we'll look at in about a few minutes from now now what it does is it provides for you the maven lifecycle that you can execute the typical life cycles are listed the typical plugins used in your project are listed run configurations are listed we'll talk about this in a bit and then your dependencies are listed so there is a lot that is already available to you within your maven tool window and let's go over that real quick but before we jump into that i just wanted to very quickly show you what a life cycle looks like in maven and so here is my own blog where i have a whole series on apache maven how to understand apache maven and um one of the blogs is the maven lifecycles phases plugins and goals this is very common uh hiccup for most developers including myself when i first started using maven so if you are in the boat of getting confused when you see life cycles in intellij not sure what this is but some senior developer in the team just comes and says hey just run maven clean install and you're like okay and it worked and that's what your robotic routine becomes you're not doing yourself and we win it justice so it is it is definitely important to understand what these are and um the chapter four on the understanding apache maven goes through your life cycles so very quickly to for you to sort of get a quick overview on what this is there are three main life cycles in maven and you can create your own custom life cycles here is a clean life cycle there is a build or a default life cycle and there is a site life cycle a clean life cycle's main job is to clean any generated code or any generated binaries so if you have a target within your application that was generated because of a maven build a clean will clean it out so that is what is the purpose of a clean so when you call maven clean you are technically calling a phase called clean within it so this is the default phase of the clean life cycle so when you call clean it actually calls clean coal and clean that is something you should be aware of now similarly your build or default has several phases and these phases are executed in order for every one of these life cycles so when you call clean you are guaranteed that a pre-clean will be called and you can perform some action in the pre-clean that is something for you to be aware of so when i call compile for instance on maven it is validating it is initializing it is generating sources processing sources etc until it's compiled so every phase prior to the phase you call gets called and that is something important to know now while the phases get called they don't do anything by themselves these phases are essentially full of goals and goals are bound to these phases and these goals are essentially documented or created using plugins so when i have plugins listed in my palm xml such as a compiler plugin or a surefire plugin these are linked to an actual phase the compiler plugin links to the compile phase which is probably the easiest way to start understanding this so when i call compile what maven does is look for any plugins that are bound or associated to the compile phase and guess what it finds within its own scope is the maven compiler plugin and therefore the maven compiler plugin is executed and that is what is happening behind the scenes when you are running your maven clean compile right and so therefore um all plugins for example you can declare your own clean plugin you can create your own clean plugin associated with the clean face and have it run instead of or in addition to the maven clean plugin so those are things that you should be sort of aware of on how the tool windows work and why these life cycle pieces exist now going back to the life cycle a typical developer usually does maven clean install or maven clean verify um i recommend you use maven clean verify because an install is actually a more expensive step in most cases and install basically is taking the built artifact and putting it into your local repository you don't need that most of the time right that is not a requirement and it saves on a lot of io so if you want to prevent io and if you want your bills to be faster um you're better off using a verify than an install and as we already covered running an install actually covers running validate compile test package and verify now you'll ask me why are there only a few of these listed here where are the other things that i saw on the diagram there those are less common used and they are not listed here but you can technically call them even initialize for instance that is totally fine and you can call it generate sources which is fine but most of the time we find ourselves using validate compile test package verify install and deploy the rest are usually um you know a part of something else that we are trying to run so calling them individually does not make a lot of value at most times but can i do it in intellij yes and we will take a look at that in a short while great so that covers our agenda item to see the main toolbar and we did a quick walkthrough of the maven tool window now in that tool window it is possible for me to navigate to the palm itself and i'm a big keyboard geek i don't like to use the mouse much so i prefer using more of the keyboard whenever i can so if i were to command 5 i am again back here now if i wanted to actually go navigate to the source for the to the palm um in my ide in my configurations i have set it up to be an f4 key and that basically allows me to go ahead and navigate oops um give me a second f4 key navigates me to the actual palm xml file and that is something quite useful um when i want to quickly navigate to a section find out what is going on there um there is an easy shortcut for me to do that now i could also go to a dependency likewise and have direct access to a dependency using my f4t that's how i mapped it your key may be different and you may actually have other ways of doing it so let's again go back and take a look on other things i can do here there should be a way for me to uh maybe there isn't that's fine uh but yeah so this is how i can actually navigate to a palm from anywhere in my code let me assume i'm here and i just found my palm and i want to go to a certain location all i have to do is go to whatever i want to go to and hit my f4 key or command down or whatever you have a setup and it takes me there so that's something quite useful to know so we've we've seen this we've covered this and i'm gonna start there okay then last thing i want to do before a q a break is to show you how to download sources in java talk now when you're running an application it is sometimes useful to know what your dependencies um source code or javadoc is and there are several ways to do it you can either right click right here and download sources documentation or both you can also download for the entire project using a download sources and what it does for you is essentially if this project dependency has its sources listed somewhere and javadoc listed somewhere in a repository it will find them and download them for your use and the benefit you get is you can actually navigate to those classes and find out how they are set up if it is an open source project right so for our for our example let us go take a look at junit jupiter 5 and let us see if we can download the sources for that and sure enough if you notice it went too quickly my internet is very good today for some odd reason and it already downloaded the sources for me right um furthermore you have transitive dependencies within each um i guess dependency possibly right for instance in our case junit jupiter has transitive dependencies on the api params and engine so you could also download sources for these so maven tool window allows you to sort of control everything maven within intellij you can download sources you can sort of run life cycle phases you can check your plugins you can check your dependencies you can download i guess sections of sources for some of the dependencies and plugins that you are interested in you can do this for plugins as well by the way and these are the different goals that i was talking to you earlier there's a maven clean goal which is the default and there's a maven clean help maven compiler has a compiled goal and there are other i guess gold available to you as well so this is the way in which you can map and start using a maven tool window so with that i'm going to go back to my agenda and mark this is done and take a quick break and ask mala if there are any questions hi chandra uh thank you so much i'm so glad to say that alex and vladislav they are answering the questions so no questions for you you can look forward to the presentation great thank you now um so if that is the case i'm going to move on and see how we can customize uh the maven settings on the tool window so we've looked at maven um and we've seen how maven can help us sort of set up our project and how we run our projects through maven and we saw that there is a clean there is a verify and install and there is also a site in deploy a site basically allows you to generate reports and create a site of sort so if you go to any apache site you notice that they have a standard look and feel with a lot of content that can change with every version right um but the the content looks very similar every time you look at it that's because they run a maven site on it right um and then the deploy basically pushes out your deployment onto some external repository or some ci cd pipeline of some sort great so in order to customize maven uh there are a few things we can do uh there is a settings panel and under the settings panel there is something called maven settings and this is a quite a useful dialogue i'm not going to be able to go through everything these are options that you find on maven and the command prompt are listed as check boxes work offline using a plugin registry execute goals recursively are all options available to you on a maven command line and the equivalents are available as check boxes on your maven settings window the output level is quite useful it tells you how much of an output you want to see when you execute a maven command there is no global checksum policy used or your corporate may actually have some uh but but we are not going to talk through that right now now this is obviously if you have a multi-project um it could fail if one of the modules fails or it could fail if everything fails or it could fail at the first failure etc so there are different kinds of behaviors that are possible and you have to just experiment with what you want to do i rarely use the thread counts but if you're running a massive build that has i guess a lot of multi-threading associated with it both in terms of testing and in the build processes maybe you want to change the thread count this is rarely used but if you do um i actually would prefer doing this from a command prompt rather than from within the ide now um obviously i have customized the maven instance i use uh so i could either use the bundled maven or my maven that i wanted and in the in my case i'm using my own custom maven right this is one customization you can do if you are in a corporate world i'm not sitting at home doing stuff that is only going to maven central then you probably also want to create a settings.xml file this is where you set up a mirror to go to your corporate um mirror of the maven central which may be separated out for security reasons or for other reasons right um so your settings.xml is a place to do it and how can i find out more about what is this settings.xml file is in one of the blogs that i have so in my blog on form and effective forms i do mention about um how you use settings.xml what are the different kinds of settings you can have there is a global settings user settings and then there are different kinds of pumps you can use which then create a an effective form and the settings.xml we are talking about is the maven user settings and this is located in the user home under the dot m2 directory so that is something useful to know that you can always change this by clicking override and setting it to a different value maybe in a different directory that is fine and that is allowed through intellij right and the equivalent on a command prompt is for you to do a minus b and point to a settings.xml location so intellij provides you for a very easy way to do that likewise we talked about a local repository when you build stuff within your ide or within using a command prompt if you run a maven clean install kind of a command any install operation will actually try to copy the artifacts into your local repository this is the location of your local repository by default it is under your dot m2 directory under your user home and it is named as a repository now there are certain times when you just want to not use what's already available you want a fresh start you can always override it and name it to something else say i'm going to call it jb demo and this is my new repository right so i can create a repository that has not yet been touched and intellij is smart enough to go create this directory structure for me when i run my maven commands so that's super cool um i don't have to go create a file directory structure somewhere come back to intellij and point to it which i can also do using this so i can actually select a directory that is already available or i can simply create a brand new directory and intellij actually takes care of creating the directory when my maven commands are run that's something useful to know great so that should about cover how we customize our maven installation for the project and you could also do it on a general basis but this is how we do it on a per project basis so i'm going to mark this as done we also talked about how to customize your settings and repository so that is something i'm going to mark as done now life cycles are quite important we already covered what the life cycles are what the standard used life cycles are in our case they are clean validate compile test package etc and how do we run these life cycles there are a few ways in which we can do that right so the first thing you can do is select in my case i'm going to do let's say select clean and verify i control click or command click so select both and then i can hit the play button here which brings up my run window which lists everything that is being run to give me a built success now the output is quite interesting and intellij because it actually gives me a more ui centric queue of what is going on rather than me reading a command prompt i can actually see what part failed because of a checkbox that shows as a green or a red now i can further drill down it shows me what ran and how long it took and what were the processes so the look and feel is very similar to how junit functionality looks like and it's very useful because i don't have to worry too much about hey what part failed i can simply look it up and i know if my compile failed i would know because the rest would fail obviously but at least i would know that the compilation is where my issues are right and that's quite nifty now sometimes you don't want this you want to see the entire content and this is how you can see your console to look at everything going on it can also limit to what you're looking at so if you select let us say the maven clean verify route it gives you that it's using the library jdk 15 it's doing everything and gives you the build success however if you're looking at just the jar it gives you from the creation of this jar onwards until the end of the test it does not give you the success of the entire project it sort of helps you limit to what you're looking at for the drilling down my clean is exactly one step and i was able to quickly locate it same with compile etc so intellij has now provided for very easy ways for me to access what my output in maven looks like so they have sort of digested the output and presented it in a format that's easy for me to understand or focus on so that's very useful to know and that's something i wanted to share okay so we ran a few life cycles so i'm going to mark this as done we can also create what is called a custom build right so we just clicked on a few things in our i guess life cycle and hit run and that was fine um it worked what if i wanted to create a custom build like i wanted to run say clean and process resources because that's all i wanted to do i don't want to compile anything so you can actually do um another kind of build which is basically give me a second let me just create a new maven build you forgot the shortcut for that i think i forgot the shortcut for that but basically what i would do is i would go to my run configurations and create a new run configuration right off the bat so i create a run configuration here i can go create a plus look for maven and i could do a command of queen and let us say process resources lo and behold i can create a run configuration that allows me to run clean and process resources and if i were to run this it produces a clean and process resource output for me so i'm able to run custom life cycles not the standard ones listed out here and should have questions about what all exist my blog should help you find out all the available phases within the life cycle that you can invoke great now you can also create a custom build we just saw how you create a custom build um and and one of the cool features of using a custom build is that you can edit the configurations you can store this as a project file storing as a project file implies it gets saved as a file within your system so let's quickly change this to say a jb demo and why is this useful while i'm doing this when you create a custom build that is useful to be shared across the team the best way is to put it in your source control system and here we have jb demo that i just created and i'm saving it as a store as a project file it asks me for a location where i want this the standard location is under dot run but i don't want to store it under dot run because i already have a standard format i use called the slash ibe so if i were to create a slash ide directory structure you can create whatever you want and hit apply intellij will actually create a new directory structure called ide and it'll store a jb run demo.run.xml and i'm going to not add it to my project yet but here you can see that it created the general settings it tells me that my options are clean and process resources and that's the goals i'm running so intellij took care of all that created a run configuration for me to use and it's made available that's something quite nifty and useful where you can share run configurations so i'm going to mark this as done and this is done and take a quick break to check with my life without any questions thank you so much chandra again um alex and larry are answering the questions so i don't really have any questions for you now yeah no worries thank you right great um so let's move on to the multi-module project for a bit right so we saw everything in a single module and the quick walkthrough of the application that we used is there is an app just as a hello world and then there is an actual test for this hello world which is a very rigorous test of you know a certain true should return true now this is something that has been created as a template for you to set up your own examples but at least there is something to start with right and that's what a single module looks like great the next one we'll look at is a multi-module and i'll just walk you through a bit of the code before we go further deep into it and this is probably more important because the rest of the logic remains the same the first thing let us take a look at is the actual maven tool window before we go anywhere else so you notice that i have a maven multi project under which i have three projects of a daytime fetcher a greeting and a greeting generator now ignore everything else this is just a multi-module setup and the moment you create a multi-module structure you notice that maven also creates multiple modules in its own tool window including one marked as root now let us take a quick look at the palm the root form and you will notice that my root palm has three modules in it it has a date time fetcher a greeting and a greeting generator module and those are the three modules you will see listed in my maven tool window now some of the nifty features that i wanted you to be aware of which are not i guess not expected but super cool to have is i can actually control click on the modules and actually go to their palm definitions this is super useful i mean this is something that is not documented anywhere but it is so nice to be able to just navigate within modules by clicking on on their module names and intellij takes me to the actual pom xml file um i love this feature so this is something i use quite often when i'm dealing with multi modules and i find it to be quite useful to being able to navigate just by clicking on items now here is a good example of a plugin management where i have defined plugins of a certain version in my plugin management and in my other projects say my datetime fetcher i don't have to define anything because it's a standard plugin but if i were to put in a plugin i don't have to specify the version number for it anymore it's defined in the plugin management so it is something that is nice uh with i guess maven in general this has nothing to do with intellij but that you can create a dictionary of sort upfront using a plugin management or a dependency management and control the versions that you use great now how does this whole module setup look like a quick walkthrough on intellij itself this is known as an aggregator or reactor palm so this palm is not defining itself as anything more than a parent to certain children palms so this is a parent palm of some sort but i'll not call it a parent form it's an aggregator palm because invoking commands on this form the root form will actually invoke the same commands in this order on the three palms listed so daytime fetcher's pom will execute the commands i execute on the my one multi then greetings form will execute the same commands and then greeting generator will execute the same commands so for instance if i were to run a maven clean on maven multi datetime fetcher screen will be run then greetings clean will be run and then readings generator screen will be run so that is the order in which things proceed and it's pretty standard and what i'm doing is a decoupled hierarchy in maven that is something useful in that a parent can declare children but the children may associate no relationship with the parent that is possible in that case it's called a reactor palm or an aggregated form in such situations the parent simply drives or aggregates the results from each of the child poms the reverse relationship is called parentage where a child form may optionally declare a parent in our case we do and by declaring a parent it inherits all the details from the parent so property is declared in the parent dependencies declared in the parent are inherited by the child form as soon as it declares a parent so that is something useful to know there is a bidirectional hierarchy but it is not mutual all the time you have to specifically and explicitly declare hierarchies so in other words i could have not declared modules here just created a poem and have my datetime fetcher have it as a parent in which case it was a child to parent relationship but no parent to child relationship that is something very interesting in maven um and it is very powerful a feature it requires bi-directionality to be explicitly specified rather than implicitly implied and that is something useful to know great and how do i know more about this so once again um shameless posting to my own blog i basically have a blog on this aspect as well on on how your palm hierarchies are controlled so please feel free to go navigate to the palm hierarchies and tell you all about this information great coming back to our code this was our daytime fetcher um and how is this set up we talked about it being multi modules these are maven modules let's quickly go back to our readme and understand modules so i'm not going to spend way too much time here but i just wanted to share maven modules are a way of defining your project structure right so they are structural modules um you you can group items into different structures a palm declares a downwards hierarchy of modules as an aggregation and the child pom can optionally declare a parentage as an upward hierarchy right and these are mostly well suited for version control and for structural control so that is quite important to know about maven modules intelligent modules are quite similar to maven modules they perform almost similar actions they are focused on the structure um jrek versions being used customizable sources and resources per module etc so maven and intellij sort of work hand in hand when it comes to their concepts of modules while they are still a bit different from one another they are quite similar in nature to what they are doing java modules on the other hand are nothing related to these two java modules focus a lot on strong encapsulation preventing certain information from being leaked out to other modules or or i guess selectively sharing what can be leaked out etc is the purpose of a java module java modules do not control the versions of java not the subproject artifacts so they are slightly different than skewed from intellij and waven modules so while intelligent modules are still different from one another their area of focus is very similar whereas java modules has a completely different area of focus now how do you set up java modules obviously when you start with the java 9 and above you'll notice that you first have to create a module info.java file at the root based package that you are trying to use and let's take a quick look at the base package here i have a module that defines my module name and it exports i guess three packages and there is a reason we export the third package it's because of junit tests because we use one static method there so this is somewhat of a gacha case you usually don't have to do this uh opening of a certain base package um and opens versus exports is basically um how you use and exports is basically where you can only make the code available um so you can compile alongside but and opens is also available at runtime so that's something that you should be aware of as a difference and we use opens because of our junit test in the online number eight so that's an example for module info we can go into a lot more detail about this on um i guess should time permit but at least for now you have an understanding of how a java module is set up you saw how maven modules are set up now let's take a look at intellij modules so if i were to go to my project module structure and how did i get here i can right click on my project and in here i should be able to find an open module settings and i could get there and notice it's called the module and these are your modules they're very similar to how maven modules are you have the same modules listed there they're very similar to java modules because those are the same names listed everywhere now the differentiator is that the intellij modules basically give you the ability to specify your source folders your test folders your resources etc so if i wanted to mark say something else as my test folder maybe i under target i have a generated sources i would like to add this as a test folder um source folder so i could mark it as a sources and now you notice that target generated shows up as a generated sources source now this is something i don't want to do for this project but you could definitely do that so that is that is where an intellij module distinguishes itself from a maven module it is more towards the structure of the code that you're executing within the ide um you're better off controlling most of your modularity if for lack of a better word um in maven rather than within intellij and use intellij sparingly to make it a convenient ide to use this way other team members who have different setups don't have to worry about recreating the same infrastructure now depends on the team culture some teams actually sort of push in to source control their project settings under dot idea certain others don't but if you use maven you're sort of limiting yourself to not having to sort of share the dot ideas for the structures may be useful in some cases may not be useful in some cases you are the best distinguishes and you can make the best decision but both options are possible so that's something i wanted to share great so this is the multi-module we took a look at how module info.java looks like let's go quickly to take a look at our maven structures you have the daytime feature it has its own life cycle you have the greeting which has its own life cycle and so does greeting generator i don't want to expand everything but here is your route this is the root pump now if i were to execute the clean install on my root palm it would imply i'm running a clean install on all of these if i were to run a clean verify instead because i prefer to verify or a clean install you will notice that it executes my maven multi then my date time fetcher then greeting and then reading generator and i can see the outputs of individuals right so they give me all the outputs that i need to look at and they are available as uh consolidated maven command line output across everything or individual outputs should i wish to actually delve into each one of them independently so that covers maven monthly modules let's go back to the agenda to see how we're doing and uh mala how are we doing on the time are we doing okay um let me just check chandra so we have 11 minutes left and i think you can take up all of that time because no questions for you okay great thank you all right so the last two things that i wanted to talk about are dependencies diagrams and terminal use right um run anything is probably the sledgehammer of tools which is probably my best and most favorite uh maven command um intellij command that i use i'll talk about that too so very quickly right if you go to your palm um sometimes you just don't want to see stuff in text text may actually not solve your problem so easily maybe you're a visual person what do you do so you can actually go to your palm structure view and look for diagrams and you can actually say show me the dependencies as a picture so when i do this instead of showing me text it starts showing me a pictorial representation of how my connectivity is so it shows me that i have a maven single um and i use log4j api which is log4j2 and i use junit jupiter and within january jupiter has certain transitive dependencies of say jupiter api jupiter engine and jupiter params which then have further transitive dependencies so if i were to use some sort of exclusions this is the place where i will see it visually it's quite useful to do that you can also drill down over here there are some options you can choose um you can show conflicts you can show paths you can show neighbors of selected node etc quite a useful and this is probably very useful in sharing across with let's say the architects that look at your source code you can save this as a uml diagram so some very useful features um in the dependencies as a diagram is possible through intellij and i love this feature i'm i think this was one of the first features that sold me on just sticking with intellij to use maven it is amazingly useful it's very nifty and i heavily recommend that you use it now if you have a very large graph it also helps you because you start seeing overwhelmingly number of dependencies and you would want to cut it down when you see it in the palm xml that you have five dependencies you may not understand that you're probably pulling in 25 000 dependencies because they have many transitives right but with this picture everything becomes much more clearer right um this is closer to you generating an effective form now that said uh maven also allows you to generate an effective pump from your palm xml that is something quite useful so you can also go to the pom-xml and under maven you can show the effective palm this is the textual version of the same content and the effective form is nothing but a mash down of all your settings and all your parent palms into this current palm and create what is known as one single effective form that gets run for whatever product artifact that i want it's very useful because it has all the information that you need it has all the dependencies that i picked up it has all the repositories that i'm going to plug in repositories i'm going to um i guess where my source and your everything that is default on me when that you don't customize is something you can get to see using the effective palm right tells you all the plugins you're using tells you how your plugins are configured for you by default so you learn a lot by going through the effective form and intellij does an amazing job of rendering that output for you now certain things will show up in red ignore them for now because those are more of my personal settings and how i set up my intellij yours may look different but so something very useful to know and i think i don't know if you're already aware or if you've already used this feature intellij now supports what's called a reader mode um and for palms it does not make a big difference but if you're in a java class um the output looks amazingly crisp so that is something i heavily recommend that you um sort of check out uh we can do a quick example on that if you if we had the time uh it's something i find very interesting so let's just go to logger let's take a look at the logger api and you notice how my output is so well organized it has readable text the javadoc has been converted into actual text that is readable so that's something i find very useful and although it's not related to me when i just wanted to share how awesome this feature is great so that was that um and i think let's go back to the agenda to see what i did not cover yet so we covered this so i'm going to mark this as done terminal usage you can also use a terminal i forgot my shortcut for the terminal so bear with me um so i'm going to hit clear and since i'm at my project i can just do an la and notice that i have a pom xml right here which means i can run my maven commands right from here within my within my pde now there are two things i can do notice the green pop-up that came up you may not be able to read the text but it is super cool although i'm using a terminal it is telling me that intellij can take over and parse the output and show it to me in the standard run window so let's do that so let's first run it as a normal maven command instead of something i want intellij to do and you notice that actually it produced all the output on my command prompt which is fine and dandy we expected it to work and it did now let us do the opposite let us instead allow intellij to take over so here i'm going to run the maven clean verify and i'm going to just say command enter instead of enter and intellij tool window picked it up that's kind of nice there is a terminal integration now so my ide is understanding the terminal and picking up commands from there i love this feature this is so cool because um i'm more of a keyboard person and i like to just type things in and i see my output show up in my standard dialog windows something that you should leverage and use in my ide the shortcut is command enter if you were to go to your own terminal and type in a maven command intellij will prompt you on what to use for your specific operating system that is something super cool and i heavily recommend using it so i'm going to mark this as done and moving on to my probably the favorite feature in intellij idea i i heavily use this feature and i think everybody should too so this is my feature in my id it goes command shift a and i do run anything and this is the sledgehammer of every single tool that you want so if i wanted to open another project so if i wanted to open the maven multi module guess what i do i just type open even multi module and lo and behold i'm in that so keyboard geeks keyboard fanatics like me absolutely enjoy having the run anything it is it is probably my favorite command in intellij idea now why am i bringing up here is i can actually show you something super cool how intellij has integrated with maven and you will probably appreciate it when you look at it so when you type maven intellij gives you all possible options that you can run with maven so you don't have to remember options you don't have to learn them you guess them all directly from the run anything dialog and it is super cool so i simply type something here verify and lo and behold intellij runs it for me um and i absolutely love this feature so um that is those are the things that i wanted to share we had about three minutes remaining so i wanted to wrap up here and see if there are any further questions um thank you so much uh chandra it was i would say a rockstar session and attendees they are thoroughly enjoying it they are in for a treat with the live demonstrations and you are just amazing and everyone yeah and everyone from the attendees thank you for attending for asking the questions and if you like the session which i can see from the uh comments that you did don't forget to like the video uh so chandra as i mentioned that alex and lord they answered most of the questions so this is one question um i'm not sure whether you would like to answer so there's a question on should i be choosing maven over gradle as my build tool that's a great question and and there is something i'm happy to answer uh i like gradle i like maven i like both they have their own individual use cases so if i'm in a corporate world where i want fungibility of my people moving from one project to another without them having to spend a lot of time deciphering the build system but focus on the actual code i would prefer maven the biggest i guess hiccup to using gradle in a corporate sense is that gradle is extremely customizable not that maven is it so gradle allows you to create a script it allows you to program your build and sometimes it is not ideal for an ide to pick up a gradle i guess heavily customized script and convert it into the project format that you want versus maven is driven by convention so you don't get to do much in terms of customization on the xml itself should you want to tweak you create new plugins right so there is an entire lifecycle for that an sdlc process for that so it depends now on the contrary on the con side when i'm working on my private projects and i develop intellij plugins and i develop um android apps riddle is the way to go um i only use gradle for that because number one intellij plugins can probably don't work or at all or work very badly with maven so it's gradle i prefer there uh same goes with android anytime i'm creating an android project the first thing i have to do is set up a gradle right um that is that is pretty much normal for me but at workplaces i think the norm should be to use something that is more convention driven so that's how i distinguish between the both but don't think either is bad both are equally good at what they do it is just that they have different goals thank you so much that was i would say an excellent answer and uh there is also i would say our team on um the chat rodrigo julian zoeck nacho neha all are here to kind of support this session thank you so much every one of you um and as i said don't miss to like the video because i thoroughly enjoyed the session and once again thanks chandra for presenting this was an amazing session and thanks everyone for asking the questions and also please stay tuned for our next live stream on march 17th which would be on profiling your applications in intellij idea thanks again and we'll see you soon thanks chandra once again bye bye you
Info
Channel: IntelliJ IDEA by JetBrains
Views: 34,428
Rating: undefined out of 5
Keywords: Java, Maven, POM, webinar, intellij idea, using maven in intellij, JVM, Chandra Guntur
Id: D1sRK8JLCQ4
Channel Id: undefined
Length: 61min 2sec (3662 seconds)
Published: Wed Feb 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.