"Maven combat" learning summary (5)-life cycle and plug-in

       In the previous article, we introduced some basic concepts in the Maven world: coordinates, dependencies, warehouses, etc. This article will introduce two other core concepts-life cycle and plug-ins.

       We have mentioned the definition of project construction earlier, and before the advent of Maven, each project has its own construction process, there is no unified standard, and it is Maven ’s life cycle that summarizes the construction experience of a large number of projects. Abstract and unify to extract a construction process that can adapt to most projects.

        The life cycle defined in Maven is actually an abstract concept. The build command we usually enter in the command line window corresponds to a " stage " of the life cycle in Maven. We can understand that the life cycle of Maven stipulates the project During the construction phase, the actual execution is done through a plug-in. The two are collaborative and inseparable. This is very close to the design idea of ​​the template method in the design pattern.


Life cycle

         Maven actually has three sets of life cycles, as shown below

        

         The clean life cycle is mainly a cleaning project, which includes three stages: pre-clean, clean, and post-clean;

         The default life cycle contains the steps required for real building, and is the core of all life cycles, including compile, test, package, install, deploy and other stages.

         The site life cycle is to establish and publish a project site. Based on the information contained in the POM, the site is automatically generated to facilitate team communication and information release.

        The three sets of life cycles are independent of each other, and each set of life cycles has a front-to-back dependency relationship (see the arrow direction shown in the figure ). When we executed the mvn clean install command, Maven actually executed the pre-clean and clean phases of the clean life cycle, and the default life cycle from the actual validate phase to the install phase.


Plugin

       The Maven core only defines the concept related to the abstract unified project construction of the life cycle, as if only the interface was defined, and no implementation was given. The real realization of the build goal is done by various plug-ins. This also explains that the Maven core distribution package is only 2 / 3M in size.

       In order to reuse the code, a plug-in can often achieve many functions, and actually bind to each stage of the life cycle is a plug-in goal (Plugin Goal), a plug-in goal represents a function of the plug-in. We are generally used to say this: compiler: compile (plug-in prefix: plug-in target)


Plugin binding

      Maven provides built-in binding of life cycle stages and plug-in goals, so we can use Maven to build our project on the basis of zero configuration. It should be noted here that due to the different packaging methods of the project, the built-in binding provided by the default life cycle Plug-ins will also be different, we can know the specific implementation of the plug-in target by observing the printout when building the project. At the same time, we can customize the plug-in to a certain life cycle stage; we can also configure the runtime parameters of the plug-in according to the scope of the plug-in. Plugin target configuration


Command line execution plugin target

        In addition, Maven also supports calling the plug-in from the command line to complete the execution of the plug-in goal, such as this way: mvn dependency: tree, where dependency represents the plug-in prefix, we can not explicitly specify the coordinates of the plug-in, so it is more concise to use Maven internal The resolution mechanism is based on the plug-in prefix, so as to find the corresponding plug-in and complete the execution of the plug-in target. Of course, you can also enter the command to specify the plugin coordinates explicitly:

       mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:tree


summary

      In this article, we introduced the concept of Maven life cycle and the processes it contains, and then briefly introduced the plug-in and the mechanism of binding the plug-in goals to the life cycle process. It can be said that the coordination between the Maven life cycle and the plug-in works and complements each other, so that we can appreciate the highly perfect and easily expandable features of Maven.

发布了159 篇原创文章 · 获赞 225 · 访问量 21万+

Guess you like

Origin blog.csdn.net/lyg673770712/article/details/51029718