Maven study summary (four) - Maven core concepts
A, Maven coordinates
1.1 What are the coordinates?
In the planar coordinate (x, y) plane may identify a unique point.
1.2, Maven coordinates major component
- groupId: organizational identity (the package name)
- artifactId: Project Name
- The current version of the project: version
- packaging: a packaging project, the most common jar and two kinds of war
Example:
1.3, Maven coordinates Why?
- Maven has a large build the world, we need to find a unified standard to uniquely identify a build.
- Have a unified and standardized, you can find the job to the machine.
Second, dependency management
2.1, depending on configuration
Configuration dependent mainly includes the following elements:
1 <!--添加依赖配置-->
2 <dependencies>
3 <!--项目要使用到junit的jar包,所以在这里添加junit的jar包的依赖-->
4 <dependency>
5 <groupId>junit</groupId>
6 <artifactId>junit</artifactId>
7 <version>4.9</version>
8 <scope>test</scope>
9 </dependency>
10 <!--项目要使用到Hello的jar包,所以在这里添加Hello的jar包的依赖-->
11 <dependency>
12 <groupId>me.gacl.maven</groupId>
13 <artifactId>Hello</artifactId>
14 <version>0.0.1-SNAPSHOT</version>
15 <scope>compile</scope>
16 </dependency>
17 </dependencies>
2.2, depending on the range
Dependent relationship range to control the scope and classpath dependence compile, test, run mainly three kinds of dependencies is as follows:.
1. the compile : default build dependencies range . For compile, test, Operation of Three classpath valid
2.test: testing relies range. Valid only for the test classpath
3.provided: provided dependent range. For compile, test the classpath are valid, but the run is invalid. As has been provided by the container, for example, API-the servlet
4.runtime: providing runtime. For example: jdbc driver
2.3, transitive dependencies
MakeFriends.jar HelloFriends.jar directly dependent on, and directly dependent on the HelloFriends.jar hello.jar, it also depends on MakeFriends.jar hello.jar, this dependence is transferred, but this dependence is indirectly dependent, as in FIG. below:
2.4, optional dependencies
Third, warehouse management
3.1, Maven repository
Unified storage for all Maven build a shared repository location is
3.2, Maven warehouse layout
The unique Maven coordinates define each construct storage path in the warehouse, substantially: groupId / artifactId / version / artifactId-version.packaging
3.3 classification, warehouse
3.3.1 local warehouse
Each user has only one local repository, the default is ~ / .m2 / repository /, ~ represents the user directory
3.3.2 remote repository
1 Central warehouse: Maven default remote repository, URL address: http: //search.maven.org/
2, PW: it is a special remote repository, which is set up in the LAN warehouse
Fourth, the life cycle
4.1 What is the life cycle?
Maven life cycle is to be abstract and unified all of the build process, including almost all of the steps to build the project clean-up, initialization, compile, package, test, deploy, etc.
4.2, Maven three life cycle
Maven has three sets of independent life cycle, please note here that the "three" and "independent", the three sets of the life cycle are:
- Clean Lifecycle performed before the actual construction of some cleanup work.
- Default Lifecycle build the core part, compile, test, package, deploy, and so on.
- Site Lifecycle generate project reports, sites, publish site.
Again what they are independent of each other, you can only call clean to clean up the working directory, just call the site to generate the site. Of course, you can also run directly mvn clean install site to run all three sets of the life cycle.
clean life cycle of each phase of the life cycle consists of a set (Phase) composition, we usually always command on the command line input corresponds to a specific stage. For example, run mvn clean, this is a stage of clean Clean lifecycle. There Clean lifecycle, but also clean stage. Clean the life cycle consists of a total of three stages:
- pre-clean need to perform some work done before the clean
- clean Removes all files generated on a build
- perform some post-clean needs to be done immediately after the clean work
When "mvn clean" in the above clean is clean, in a life cycle, run some stage, all stages will be run before it, that is to say, "mvn clean" equivalent to mvn pre-clean clean, If we run mvn post-clean, then the pre-clean, clean will be run. This is a very important rule Maven, can greatly simplify the command-line input.
Site life cycle of pre-site to perform some need to complete paperwork before generating station
- Build the project site, site documentation
- post-site to perform some need to complete paperwork after generating sites, and prepare for deployment
- site-deploy the generated site documentation deployed on specific server
Here is a site frequently used site-deploy phase and phase to generate and release Maven site, this is Maven quite powerful, Manager prefer, documents and statistical data to automatically generate, very nice.
Default Default life cycle life cycle Maven life cycle of the most important, most of the work takes place in the life cycle. Here, just to explain some of the more common and important stages:
- validate
- generate-sources
- process-sources
- generate-resources
- process-resources Copy and processing the resource file to the destination directory, ready to pack.
- compile the source code to compile the project.
- process-classes
- generate-test-sources
- process-test-sources
- generate-test-resources
- process-test-resources Copy and process resources, to test target directory.
- test test-compile the source code to compile.
- process-test-classes
- using a suitable test unit tests run the test frame. These test code will not be packaged or deployed.
- prepare-package
- receiving compiled code package, packaged into a publishable format, such as JAR.
- pre-integration-test
- integration-test
- post-integration-test
- verify
- install the package is installed to a local warehouse to make other items dependent.
- deploy a copy of the final package to the remote repository to allow other developers and project sharing.
When running any of a stage, all stages will be run in front of it, which is why when we run mvn install, the code will compile, test, package. In addition, Maven plug-in mechanism is entirely dependent on Maven life cycle, so crucial to understanding the life cycle.
Five, Maven plug-ins
- The core of Maven only defines the abstract of the life cycle, specific tasks are handed over to complete the plug-in.
- Each plug-in can implement multiple functions each of which is a plug-in goal.
- Maven lifecycle and plug-target binding each other to complete a specific task to build, for example, is to compile a plug-in plug-target maven-compiler-plugin's.
Reproduced in: https: //my.oschina.net/zhanghaiyang/blog/606260