As you've probably heard, the magic word or words in maven is "mvn clean install". I remember when I heard of maven for the first time, experienced developers fired mvn clean install a million times. So let's have a look into what these magic word means. So this posts revolves around this magic words.
Maven works in something called life cycles. You may have heard of build life cycle in maven. By passing a few commands to maven, we basically execute one or more phases in one or more life cycles defined in maven. There are 3 built-in life cycles in maven; default, clean and site. Does this ring any bells?? Recall the magic words; "mvn clean install". Yes, the 2nd word!!! By typing the word clean, we are invoking a life cycle of the maven. I hope you got the idea.
So what does these 3 life cycles do;
- clean: Cleaning all the generated artifacts from maven in a previous build.
- default: Compiling, packaging, installing and deployment stuff of your project (Will look into this deeper in a bit)
- site: Creating documentation of your project as a site.
Before proceed further, let's have a closer look at the 3 magic words; "mvn clean install". The mvn command basically invokes the maven runtime. Anything following the term mvn are the commands we passed into maven. The first command we pass into maven is clean. Which means we are telling maven to execute the clean phase of the clean life cycle. Now hold on, a new word!!! "phase". A life cycle is composed of a sequence of activities or phases. clean is a phase belong to the clean life cycle. Thus maven will execute the clean phase of the clean life cycle.
Important: maven execute activities on the sequence of the parameters fed into it. In the preceding case, since we pass clean as the first argument, maven executes clean before install.
When you build using the default life cycle, the generated class files, artifacts, documentation stuff goes in to the target directory in the maven project. So whenever you build using maven, make sure you invoke clean phase which will delete all the items in the target folder and generate everything fresh.
Now lets look at what install means. This is a phase belong to the default life cycle. We use this life cycle very often. So I'm elaborating the phases in the default life cycle;
- validate
- initialize
- generate-sources
- process-sources
- generate-resources
- process-resources
- compile
- process-classes
- generate-test-sources
- process-test-sources
- generate-test-resources
- process-test-resources
- test-compile
- process-test-classes
- test
- prepare-package
- package
- pre-integration-test
- integration-test
- post-integration-test
- verify
- install
- deploy
Preceding illustrates 23 phases in default life cycle. Note that install phase is the 22st phase in the default life cycle. If we instruct maven to execute a phase in a life cycle, all the preceding phases prior to it gets executed. Which implicitly means a particular phase in a life cycle, depends on all the proceeding phases. In our case, when we execute install phases, all the 21 phases prior to install phase in the above list are getting executed. As you may have heard, maven manages your project build and dependencies. Thus knowing these phases could come handy.
Before concluding this post, let me elaborate 3 phases in default life cycle which you might be using extensively.
- compile : This will compile all the classes in the project.
- package : This is yet another phase in the default life cycle. You may have noticed that in your pom file, a tag called packaging. This tag may containing different values(i.e: jar, bundle, pom, etc). This is used by the package phase. Depending on the package type, maven decide how to package your project. If it is jar, then your classes are bundled into a jar file. There are lot more to this than what I have mentioned here. I'll elaborate more on this in a different post.
- install : If you read this post carefully, you are aware that package phase gets executed before install phase. Once the maven artifact(the package) is generated install will go and place the artifact in the local repository.
What is a Local repository?
Maven deposits your build artifacts in a repository. There are local repositories which resides in your local computer. Remote repositories residing in remote servers which your project can access to download your project dependencies. install phase will make sure that the artifact you built(the jar file of your project) get deposited into the local repository.
By default, when you execute maven for the first time, a folder called .m2 is created in your user folder. This is your local repository. All the project dependencies will get downloaded into this repository so maven can compile your project. This is a storage location of your build artifacts which are yet to be published. Thus when you run maven install command on your project, it will place your project artifacts in the .m2 folder.
By default, maven is configured to use a set of predefined remote repositories containing extensively used java artifacts. If you need to access custom java artifacts (i.e: java artifacts developed in the company that you work on), then maven give you the freedom to point to remote repositories in which the required java artifacts resides.
By reading this I hope you got a clear how maven works. Feel free to give your thoughts on this.