Maven Complete Tutorial with IntelliJ

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're a java developer your day-to-day activities while working on your projects include writing code compiling code testing the code packaging it in the form of an artifact like jar or raw file and then deploying this artifact to a web server there's a lot of tasks you have to take care of manually and obviously you can automate this task using a library called apache maven in this video you will learn what is apache maven and how you can use it effectively to build and manage your java projects we'll start by learning the fundamentals of maven and step by step we'll learn all the important concepts of maven including the advanced concepts like multi-module projects so without any further delay let's start the video [Music] hello and welcome to my channel programming techie where i teach you how to build real world web applications and improve your programming skills if you are interested in this subscribe to this channel and hit the bell icon to join us on our quest so what is mavin maven is a project management tool which is developed to help manage projects which are developed using jvm languages like java scala the major tasks of a project management tool include building the source code testing source code packaging it will artifact and handling versioning and releasing of the artifacts generating java docs from the source code and managing project dependencies as you can see maven is responsible for doing lots of things and for this reason you will hear maven is also called as a build tool or a dependency management tool now let's go ahead and see how to install maven in your machine i open the website apache.maven.org in my browser and when i go to the download section you can download the latest maven version here and at the time of creating the video the latest version of maven is 3.603 after you downloaded the binary zip archive and extracted the zip file the first thing we have to do is to create an environment variable in your system path called as m2 underscore home this is the place where other softwares and libraries look for the maven installation so it's similar to java and environ variable so let's open the environment variable window and create a variable called as m2 underscore home and here i'm going to give the value as the path of the pin folder of the maven installation directory after this is completed click on apply and ok and to test whether the environment variable is correctly set or not open a new terminal window and type mvn hyphen hyphen version so you should be able to see the maven version including maven home and some other details so okay we have installed maven successfully on our machine now let's see how to configure maven in an ide so intellij already comes with a bundle maven installation so it's not really necessary to specifically configure the ide unless you are working in a team so in a team normally you would work on one maven installation across the whole project so anyway let's see how to configure maven in intellij so in the intellij welcome window i'm going to click on configure and after that click on settings then you will see a new settings window and in there search for maven so here you can see the maven home directory it is pointed to the bundled maven 3 installation you can change it by clicking on the button you see on the top to the right of this drop down and select the main installation directory and once this done you can click on apply and ok and you have configured maven into your ide successfully so now it's time to create our first maven project so we're going to use intellij idea to create the movement project after you clear after you open the intellij idea and in the welcome window i'm going to click on the new project button this will open a new project dialog box and to the left side i am going to select the option maven and click on next in this next window we are going to provide the name of the project maven project demo and i am going to expand the option artifact coordinates where you will see three fields group id artifact id and the version a group id is like an identifier for your project which usually follows the standard java package naming conventions so in our example intellij has already provided the value or dot example we can change it like com.programming techie and the artifact id is the name of the project we are creating so i'm going to leave it as minimum project demo and the version is the is a unique number which is used to identify our id which is used to identify the version of our project so i'm going to leave the default value 1.0 snapshot so if you're wondering what is snapshot we will have a look at it in detail in the upcoming sections so i'm going to just click on finish and intellij will create the maven project automatically now let's have a look at the folder structure of the maven project we just created so if you expand the maven project demo folder you will see a folder called source which is the root of our application source code and the application unit tests so then we have some subfolders mainly source main java which contains the source code of our application and usually all the production code we write for the application is uh is inside this folder and then we have the source main resources folder where we use all where we store all the static files we are going to use in our project so the examples include some property files or any files where we need to read in our application like an xml file csv file if you are developing a web application we will usually place all the static resources like html css and javascript inside this folder next we have the source test java file where we showed the unit test and the integration test for application in this folder and the last part is the pom.xml which contains the metadata of the project dependencies so we will have a look at the project dependence so we will have a look at what is formed at xml exactly in the upcoming sections and the last one and i forgot to mention one more project one more folder called as target which is not current which is currently not visible but in this folder maven stores all the compiled java class files and we'll discuss about this also in the upcoming sections one of the core components of a maven project is the palm.xml file the pom in the bomb.xml file stands for project object model which contains the metadata of the project and is also responsible to manage the dependencies to configure plugins which helps us in automating many repetitive tasks here is how a basic form.xml file looks like so we have the project tag which is the top tab which is the top level tag of our form.xml file this project tag encapsulates all the information related to our moving project next we have the model version which represents the version of the palm you are using for when for maven 3 the model version is always set as 4.0.20 this number will never change unless you are using another major version of maven the fields group id artifact id and version we have already explained in the previous sections so the group id is a unique identifier of the project the artifact id is the name of the project and the version is a unique number which identifies the version of our artifact now as we know what is a pom file let's have a look at different types of form files so we have basically three types of form files first we have the simple pom file so this is the bom file we saw previously so this is just the file which is generated when creating a maven project it can't it contains only information which is relevant to our current project next we have a super pump file this is a parent for all the super pump files so it contains a lot of default configuration which is shared by all the simple pom files you can find the super pump file inside the maven installation directory under the path lib and inside the jar file named maven model builder we can find a package named org apache maven model so under the file this we have this pom4.0.txml so this is the super pump file i'm talking about and next we have an effective bomb file so an effective bomb file is nothing but a combination of simple pom and super pump files so it's just a way of checking all the information of the palm.xml files just in one place so we'll have a look at how this is helpful in the upcoming sections but if you want to have a look at the effective pump file all you have to do is go to your maven terminal type maven help effective bomb so maven takes a couple of seconds and then splits out the complete effective form.xml file form so it's better to see this contents inside an ide so i'm going to open intellij and to the right side of the intellij idea there is a section called as maven if i click on it and right click on the maven project demo root root folder you can see the option show effective palm so if you click on it so you can see the contents are just similar to the super form xml file you will see the plugins information which are the default information which maven provides us you can see a build section you can see we can disk we will discuss about all these components in the upcoming sections but it's just i'm showing it to you just to have a basic idea so if you scroll all the way up you can see the model version the group id is our group id we mentioned in the simpleform.xml file and also the artifact id and the version are the same if you're working on any non-trivial java projects chances are that you are mainly using some third-party jar files in your project to develop the application these jar files can be anything like a framework or a library like for example some junit framework spring framework or maybe selenium these kind of external libraries are usually called as dependencies maven provides an excellent way to specify and manage these dependencies in our project through the form.xml file without using maven you have to manually download the required jar files from the internet and add them one by one to your project so maven provides a dependency section in the form.xml where you can specify the information where you can specify the information of the chart you require in your project this usually is the group id artifact id and the version once you have specified the required dependency information maven will automatically download this dependencies from internet into your project and and adds them into the class path of your project so now let's see how to add the dependencies to our uh the demo project so now i open the form.xml in intellij and inside the and just be sure and just below the version tag i am going to add another tag called as dependencies and all the required dependencies should go inside this dependency stack so to add a dependency we were going to add junit 5 to our project to add the dependency information i'm going to open the browser and go to the sitemavenrepository.com and there i'm going to search for junit and here i'm interested in the dependency junit jupiter engine so i'm going to click on it and here you will see all the information about this particular jna dependency and you can see the all the latest versions of the junit dependency and i'm and i'm mainly interested in the version 5.7.0 so here you can see that we have this dependency tag so we can just copy this information i'm not going to include the comment so i'm going to just copy this information go back to my ide and copy this dependency inside the dependencies section so that's what we have to do so we have to force after specifying this dependency we have to force the intellij to download this dependency we can do that by clicking on the this maven icon you see on the top right corner so once i click on the icon this dependency is already downloaded into my machine so you can see whether the dependency is downloaded successfully or not by clicking on the maven section name and section here and if you expand the maven project you can see the section dependencies here and i'm going to expand this again and here we have the junit jupiter engine dependency in our project if you observe the jna dependency again so we see some other dependencies inside junit engine jnet jupiter engine dependency so we have the api garden api guardian jnet platform engine and jnet jupiter api so these dependencies are called as transitive dependencies that means they are dependencies of our dependency if we if i try to expand also the junit platform engine dependency we see we have again the apn guardian api open test 4j and degenerate platform commons so the main caveat of having all these dependencies transit to dependencies is over the time our dependencies over the time these dependencies can increase a lot and they can create a lot of mess so i'm going to explain this with an example i'm going to add another dependency called springboot starter test so this is the dependency which will enable us to test the spring boot app if i go back to maven section you can see that we have the spring boot starter dependency and if i expand it you can see that we have j unit 4 dependency as a translator dependency of spring good starter test so if i try if i write some tests in my project and try to run them there will be a conflict in our class path we have both g unit 5 and junit 4. so we have to manually exclude this j net 4 dependency to make sure that our tests are working fine to do that i can just add an exclusion tag inside the dependency tag so which looks something like that we have add we have added an exclusion tag and inside that i'm added and inside that we have an exclusion tag and inside the exclusion tag i am going to specify the group id of the dependency and the artifact id of the dependency i want to exclude and if you open the maven tab again you can see that the junit 4 dependency is no longer visible inside the dependencies section a moving dependency can be categorized into two ways a snapshot dependency and release dependency a snapshot dependency generally means that a project is under active development if you're working in a java if you're working on a project in a team chances are that you are following some kind of iterative process where you go through the phase of first developing the software and at the end of the phase you release the software to the end users so at the time of developing software we mainly use the snapshot versions for the dependencies and once the software is released we will use the release versions so you can say that snapshot dependencies are unstable and and the release dependencies are more stable we can control the visibility of a maven dependency using dependency scopes there are five types of scopes available from even dependencies the first one is compile this is the default scope for all the dependencies and the dependencies marked with compile are available inside the class path of the project and are also packaged into the jar file the second one is provided which is similar to compile but with small differences a dependency which provided scope will be provided at runtime either by jdk or the web server a good example of the dependency using the scope is the servlet api dependency the web server which is running our project provides the servlet api at runtime so the dependency will be available in the class path of the project but it will not be packaged into the jar file or war file next we have the run time scope as the name suggests the dependency marked with runtime scope are available only at runtime but not at compile time a good example in this case is the mysql jdbc connector dependency by marking this dependency as runtime you can make sure that we don't use the mysql jdbc classes in our code instead of the standard jdbc api the next scope is test these dependencies are only available at the time of writing and running the tests so the examples include junit and spring boot starter test dependencies as you can already see in our form.xml and lastly we have the system scope which is similar to provided scope but only difference is we explicitly mentioned where we can find the dependency in the system so instead of a jdk or web server providing this dependency at runtime we provide the system path where maven can find this dependency you can provide the path using the system path tag so in the previous sections we saw how maven manages and automatically downloads the dependencies into our project so the next question is where exactly does maven stores this dependencies maven stores it inside a special directory called as repository so we'll have basically two kinds of repository in maven the first one is a local repository this is a directory or a folder which is inside the machine where the maven is running the default location of the local repository is under the user home and inside the dot m2 folder so next we have the remote repository this is a website where we can download the maven dependencies so this can be a repository provided by maven or maybe a custom repository set up inside an organization using software like iot factory or nexus so now let's go into more details and see how maven resolves the dependencies so first whenever we define the dependency inside form.xml maven first checks whether the dependency is already present inside the local repository that means inside the dot m2 folder and if it is not able to find this dependency here then it then tries to connect to the remote repository and will download the depository and store them inside the local repository so we can define the repository in our palm.xml by just adding this repository tax just below the dependencies tag so here we have the repository and with an id i have just provided the my internal site as the id and to the url we will usually provide the url of the remote repository in our case it can be the url of the artifactory or nexus repository so till now we have learned the core concepts of maven now it's time to deep type and understand how maven builds our projects so maven follows something called as a build life cycle to build a project and this life cycle is divided into three different steps so the first step is called as default so as part of this step uh maven performs a lot of tasks so if we zoom into a bit up into the default step we will see that first step the first we have some different phases so as part of the steps look at these steps we have different phases inside this uh steps so the first step so the first phase is validate so as part of this phase maven reads our pom.xml and validates whether we have valid xml here or not so if there are any errors when creating the pom.xml those are identified as part of the step next comes the compile phase so as part of this phase as the name suggests maven will compile our source code and create the the class files and it stores it under the target folder so the next step is test so as part of this step maven runs our unit test inside the project and after this step we have the package face where mavin packages the source code into an artifact of our choice so this can be a jar file a war file or er or maybe also zip file after the package phase we have the integration test phase so if our project contains any integration test so these are executed as part of this phase and after the integration test phase we have the verify phase so here so as part of the verify phase maven checks whether the integration tests which are run in the previous phase are successful or not and after the verify phase we have the install phase where maven will install the created package file so it may be a jar file or whatever artifact which was generated it installs it into a local repository so that means the dot m2 repository folder after the installation maven will try to deploy this created package into the remote repository so the remote repository is configured in our uh in our settings.xml and palm stone.xml then even tries to deploy this package into that remote repository these phases are inclusive of each other so if we run compile so then we are implicitly also running validate if you are running test so as part of this phase we are running validate compile and test so if you are running install that means you are running validate compile test package integration to s35 and also install so now let's discuss about the next step inside the maven life cycle called as clean so this step is mainly responsible to clean the compiled java classes or whatever metadata which was generated as part of the default life cycle phases so for this reason you will see this clean strap together with the life cycle phases so the famous example for this is the maven clean install command so what this command exactly does is it is first it first as part of the install phase as i told before it compiles the source code it runs the unit test it creates the jar file and then installs this charge file and then installs the star file into the local repository and before doing all that it will make sure that we are starting uh this phase in a clean way so that it will create it will clean all the um previously generated class files and metadata from our project so the next step is the site lifecycle step so this is responsible to generate java documentation which is present inside our project all these three lifecycle styles all these three lifecycle steps and also the phases contains some additional phases which i am not going to cover in this tutorial so that will make it more advanced and more confusing for the beginners so for this reason i am going to link the documentation section where you can go to the maven documentation and refer these phases in much more detail to be able to execute the lifecycle phases we saw in the previous section maven provides us with different plugins so that we can perform each task in the life cycle so maven plugins are not only used to execute the lifecycle phases but to automate the other repetitive tasks as part of your build so meven provides us with different kinds of plugins this the plugin ecosystem is very huge so there are lots of things which we can find to automate the repetitive tasks in our build so each plugin is associated with a particular goal so let's have a look at each of the plugins and the associated goals for this plugins the first plugin we are going to talk about is the compiler plugin this plugin is responsible to compile our source files as well as test files in our project this is similar to running java command for the whole project so now let's see a demo of how to use this plugin inside the pom.xml all the plugins must be defined under the build tag so we will usually define this build tag under the dependencies section and inside the build tag i am going to define a tag called as plugins and inside the plugins section i am going to define the compiler plugin which has the group id as org apache maven plugins and artifact id as maven compiler plugin at the time of preparing the video the version is 3.8.1 and after defining the plugin we can test it by opening the terminal and typing the command maven compiler compile here compiler is the name of the plugin and compile is the goal associated to the plugin this goal will trigger the compile lifecycle phase in the maven build lifecycle so if i now press enter you can see that maven has nothing to compile in our project because we did not create any java classes so let's go ahead and create a java class quickly and i'm going to run the command again so now you can see that maven scanned the project and detected some changes and compiled the java classes into the target folder i previously explained about target folder and this is the folder where maven stores the compiled class files so you can see the class files under the target folder and under the classes directory maven also compiles the test files in our project so let's go ahead and create a java class inside the test folder and now i'm going to run the command maven compiler test compiled which will compile all the test classes in our project and this command is going to execute the test compile goal and so now you can see that maven compiled our test classes into a different folder called as test classes under the target folder so if i open the target folder we can see the hello world test class file under the test class folders we can also try to execute these commands from intellij instead of running from the terminal we can do that by first clicking on the maven tab to the right side of the intellij ide you can find this tab to the right side of the ide and under the plugin section expand the compiler plugin and click on the compile goal so we can see that the compilation failed because the java compiler version of maven inside intellij is configured to java 1.6 by default we can change those settings by changing the configuration of this plugin so i'm going to add a new tag called as configuration under the maven compiler plugin and inside the configuration tag i am going to provide the source and target properties as 15 which is the java version installed on my machine by adding these properties maven will use the java 15 version to compile our class files after adding these properties if i try to click on the compile goal again you can see that the files are you can see that maven compiles our project without any errors the next plugin we are going to talk about is the maven surefire plugin using this plugin we can run our unit test inside our java project to configure the plugin in your project add the group id of the plugin as org apache maven plugins and artifact id as maven surefire plugin at the time of producing the tutorial the latest version of the plugin is 2.22.2 once you have configured the plugin you can open the terminal and type maven clean test and here the clean command will make sure that we run our test in a clean state and the term test will run the test goal of the maven surefire plugin once i press enter you can see that maven will first delete the existing contents inside the target folder and then compiles the java classes and the test classes and then runs the tests in our project after the tests are executed we can see a simple report on how many tests are executed successfully and how many have failed the surefire plugin also generates an xml report inside the target folder this report can be used to visualize the test results in a ci environment like jenkins the maven install plugin is used to run the install lifecycle phase in the maven build lifecycle it compiles our source code runs the unit test packages the source code into an artifact and then installs the artifact into her local repository to define the name and install plugin i'm going to add another plugin tag inside the plugins section of our pom.xml file and i'm going to use the same group id as the previous plugins and the artifact id is going to be maven install plugin and the version is 2.5.2 once this is configured open the terminal again and this time let's run maven clean install where install is the goal of the maven install plugin once i press enter you can see that maven is running all the previous build lifecycle phases like compile test package and at the end you can see that it's installing the package.jar file to my local repository this jar file is first generated inside the target folder and maven then copies this jar file into the local repository the maven deploy plugin is used to run the deploy lifecycle phase in the maven build lifecycle it runs all the phases which are included as part of the install phase and then deploys the created artifact to the remote repository to be able to deploy the artifact to the remote repository we first need to specify the remote repository details inside your palm.xml file to do that i'm going to open the form.xml file and add a tag called as distribution management about the dependencies tag and under the distribution management tag i am going to add the repositories information so to uniquely identify the repository inside of form.xml i added an id tag and a url tag to point to the url of the remote repository in our example i do not have access to any remote repository software like like artifactory or nexus so that's why i provided a dummy url so now if i go to the terminal type maven clean deploy and press enter you can see that maven throws an error that it cannot connect to the remote repository this is expected but as we are working with a dummy remote repository url but if you are working in an organization which uses either artifactory or nexus then the deploy phase should work just fine till now we have covered the fundamental aspects of working with maven now let's deep dive into some advanced concepts like multi-module projects in the real-world projects the application you are working on is usually divided into various modules and you can maintain the source code of these modules effectively by using the maven multimodule projects as you see in the image on the screen we can define a parent bomb which acts as a parent for different child modules and these child modules can have some nested child modules in this way we can modularize our whole project based on a particular functionality and we can use multi-module projects to actually do this now let's go ahead and create the multimodule project using intellij under the root folder of the movement project i am going to right click and select new module this will open the movement module window in here i am going to provide the module name as child project 1 and if i expand the artifact coordinates section you can see maven automatically generated the required group id artifact id and version i am going to change the group id to the same group id we used for the parent module and click on finish so now you can see the child project 1 module inside the project window and if you open the pom.xml under the root folder maven automatically added the module details to the form.xml so we can see the child project 1 inside the module section if i open the pom.xml under the child project 1 module you can see that the maven project is defined as parent for the child project 1 module so i am going to create a couple of more child modules in the same way inside the project so now you can see that we have three child modules inside our project and we still have the source folder under the parent project so i'm going to copy paste this folder under the child module and after that i'm going to delete this folder as this is not used anymore so now if you run the maven install command on the parent project maven will scan through the whole project and understand that we have child modules and will start building them automatically maven uses something called as a reactor which is responsible to scan the whole project and identify the parent and child moving projects so if there are any dependencies between our child projects as we saw in the image previously maven reactor will make sure that our maven reactor will build our project according to these dependencies when you are working with multiple maven modules you may be working with different dependencies in different modules and chances are that you are maybe using similar dependencies in multiple modules for example if we have j unit as a dependency in all our child projects there is a chance that we may there is a chance that we can by mistake use a different version of the dependency a j unit dependency in one child module and another version of the dependency in other modules we can manage the dependencies and its versions in a centralized location inside the parent project with the help of dependency management tags by maintaining the version of the dependency in the parent module we can have a single place where we can manage all the dependencies inside all the child modules let's see a demo of how to use this dependency management in our project inside the parentpoem.xml i am going to create a dependency management tag about the dependencies section and i am going to move all the dependencies inside the dependency section of the dependency management tag now you may ask what is the difference between adding the dependencies under the dependency management tag and just leaving them under the dependencies tag by default the dependencies marked inside the dependency section are available also for the all child modules for example if i am going to leave the common slang dependency under the dependencies section and move all the remaining dependencies onto the dependency management section if i open the palm.xml of child project 1 and check the dependency tree by typing the command maven dependency 3 you can see that the common slack dependency is available to the child project 1 but not the other dependencies to access the dependencies which are marked inside the dependency management tag we have to manually define the dependencies inside the child project 1 so for example if i want to use junit 5 inside my child project 1 i can define the dependency by providing the group id and artifact id in this way the child projects can control which dependencies it needs and it actually solves the problem that all the dependencies which are defined inside the parent are not also present inside the child module going forward if your project is very big then this will avoid the dependency health so this is a best practice we have to follow in our projects we can also manage the plugins inside our maven multimodule project similar to the dependencies to manage plugins we have the plugin management section inside the pom.xml and each child project can choose the plugin it needs from the plugins which are available so this is how our parentpom.xml looks like after introducing the plugin management tag the plugin management tag also goes under the build tag so as we just moved the plugins section from under the build tag to under the plugin management tag so in this way we can also log down the plugin versions we like instead of using the default maven plugins which are configured inside the super form file profiles can be used in maven to create customized build configurations in our project this means we can customize the behavior of our bills based on some specific conditions for example sometimes we need to test whether the source code packaging is working correctly or not in that case we may want to skip the test execution because the bill process may take longer in those cases we can create a profile which will skip the test execution we can configure this profile in our palm.xml by adding the profile tag and inside the profile tag i am going to provide an id for this profile and i'm going to declare the maven test skip property which is the internal mobile property by default i'm going to set this value as true so when we activate this profile while running the build maven will make sure to skip the test execution so let's go to the terminal and type maven minus p skip tests clean install where minus p stands for the profile option and i provided the id of the profile which is skip test and you can see that the build was successful and maven did not execute any test as part of this build so this is the end of the tutorial i hope you learned something about maven through this tutorial and if you liked it please don't forget to like this video and subscribe to the channel for more upcoming tutorials i will see another video
Info
Channel: Programming Techie
Views: 31,849
Rating: 4.9087634 out of 5
Keywords: maven complete tutorial, complete maven tutorial, maven beginners tutorial, maven explained, maven introduction, maven learn automation online, maven lifecycle, maven tutorial for beginners, maven tutorial intellij, maven tutorial java, maven with intellij tutorial, devops tutorial for beginners, devops tools, maven intellij
Id: JhSBS2OpGdU
Channel Id: undefined
Length: 38min 16sec (2296 seconds)
Published: Wed Dec 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.