Transcript for:
Understanding Maven Lifecycle and Components

hello everyone welcome back to my channel this is the next video about maven and today we will take a look at maven lifecycle phases goals plugins and profiles after watching this video and previous video about maven on my channel you will be able to answer on almost every question about mavin on java interview ok let's start maiden lifecycle it's just a sequence of phases it means that life cycle consists of phases that are executed one after another sequentially and there are only three life cycles in the maven the first life cycle is the site the result of this would be ready website with information about our project next it's a clean it just removes target folder from root directory default it's a main maven lifecycle which consists of more than 20 phases let me show you it in intellij idea here i have a simple maven java project let's open pom.xml file and i want to show you how to run life cycles for example we can run maven site lifecycle all we need to do is to print mb nbn site and with we see that the build success so we can refresh our project structure and see in the target folder the folder is named site and here we have website about our project we can open it in the browser and see every information about our project like for example dependency information it's information to implement our project into another project which dependencies does it use for example gunit hamcrest and summary here we can see name description home page from tags in pom.xml file name description in url maybe basics project even basics project also here we see the group id artifact id version type of packaging and java version another lifecycle it's clean to start it we need to print nbn clean as we see build success and if we refresh our project structure we see that target folder removed from our project also we can use the maven toolbar to run these life cycles and faces for example here we have clean and sight life cycles other phases like validate compile test packaging and other are related to default lifecycle at which we will take a closer look also note that we can run multiple life cycles in one command by writing for example like this clean site also we can print maven clean package package is a phase of default lifecycle and we see that here we have a target directory with our compiled classes packaged dot jar file test reports and other information now let's take a look at default lifecycle and its phases at first what is the phase phase is a step of a life cycle phase may consist of zero or more goals here you can see most important phases of default life lifecycle first is compile on this face the maven compile source code of your project it evaluates all files with java extension to files with that class extension which can be executed by jvm and put these files to target folder test compile it is the same as compile just do it for tests folder next tests it runs tests package packs compiled project files into executable jaw var er file install puts an executable file to local repository from where it can be imported by local projects and deploy copies an executable file to the remote repository for sharing with other developers these phases are consecutive when you run for example the package phase such phases like compile test compile and test will be executed in advance note that you can skip some phases for example to skip test phase you need to specify a property which i want to show you in the intelligent idea nbn package dash d maven test skip equal true let's run it and as we see tests are skipped another way to define this property is to write nbn package d keep tests it's a special alias next what is the goal it is a unit of work in most cases goals are linked to phases also every goal is running with specified plugin for instance the compiler plugin runs goals compile and test compile let me show you example of growth the first word before colon is the name of a plugin which is used to run this goal it's possible to run goals separately from lifecycle and faces we just need to write a command with defined plugin let me show it to you in antelope idea nbn column compile as we see the compile goal we are run and there no any other goals which would be if you run it with printing the face for example nbn you see that before the compile where resources face started next let's take a look at plugins plugin it is a java class assembled in a special way which runs goals every plugin contains its own goals which can be executed with only this plugin there are lots of plugins exist for them even almost for every task you can think of let's take a look at the official maven plugins page here you can see bunch of plugins and can read what does they do and what goals they have for example let's take a look at the core plugins it's clean compiler deploy we can see here the description of every plugin for example shufi plugin which runs the junit tests in an isolated class loader and install plugin which installed the build artifact into the local repository let's take a look at the page of compiler plugin here we can see its goals compile and test compile we can go to this goal on this page we can see lots of parameters of compile plugin let me show you how to configure these properties to specify it we need to open intelligent idea and print nvn compile d and then print the name of the property which we can find on the page here maven compiler verbose let's pass it equal true and let's run i forget that we need to change anything in our source code to run this command with verbose flag let us add something to the stream and rerun our compile stage and here we see the verbose output of the compile stage also we can specify the properties for our plugins inside the pawn.xml file to do this we need to add a special build tag inside where we add the plugins tag and the plugin we need to specify the group id in artifact id for specific plugin also we have to specify the version of our plugin and then the configuration here we specify the verbose flag and true and source and target version of java to the 1.8 for now if we will run our and then compile let's change something in source code we will see it has verbose output another example it's a configuration of a shoefire plugin which runs our test phase for example we can set test failure ignore in true and now if we will break our test and run for example the package phase we will see that we have failed test but our build is success if we will comment this property and already run the package or test face we will see that build failed because our tests are own let's fix it and rerun our package face so for now everything is okay failures is zero sometimes necessary to have a different project and plugins configuration for building for example if you need to build your project on different operation system for these purposes the maven has a such tool as profiles a profile is a set of configurations which rewrites project configurations and can be applied in certain conditions let me show you an example of profile in our pom.xml file we have the tag profiles and we have two profile first profile is the production note that every profile has to have an id tag and then we can specify any configuration like in familiar to us pom.xml configuration for example here a target directory would be replaced with production directory which we specified in our production profile to run task with the profile you need to write a command nbn p which means profile production package success let's refresh in the project structure and here we see the production folder which contains the same information and the target folder also you can define here any properties for example we can define the configuration of our plugins like here just as we do in our common that xml and our common pom.xml file and it would work perfectly you may have noticed that it's not quite useful to apply profile with command line we want to do it automatically in special conditions for these purposes profiles has activators in this profile with name change file name we have a configuration build and we change the final name of our project on new final name in our target directory we have a new file name dot java file let me remove this target directory so for now if we will run the package face and with some time we will have a target directory with the maven basic.jar file which we will specified in build final name property and for now and with this profile we want to change our final jar file name to do this here we have an activation tag and we can define our activators it is the first activator which activates our profile by default now let's run the package and we see that there are another file with name new final name there are several other activators like property file jdk os property means that it applies if some property is specified file if special file exists jdk if used the specified gdk version and os on a specific operation system thank you for watching this video today i told you everything you need to know about maven lifecycles plugins and profiles all this information may be particularly useful on a real project or job interview in the next video we will take a look at how to create a multimodule project with the maven and how to manage dependencies in this multi-module project hope you liked this video hit the like button and if you want to see more make sure you down subscribe button have an awesome day i'll see you once again in the next one [Music]