Maven Learning Summary (iv)--maven core concept One, MAVEN coordinates 1.1, what is coordinates?
In planar geometry, coordinates (x, y) can identify the only point in a plane.
1.2, MAVEN coordinates are mainly composed
- GroupId: Organizational identity (package name)
- Artifactid: Project Name
- Version: Current versions of the project
- Packaging: How to Package a project, the most common jar and war Two kinds
Examples:
1.3. Why does maven use coordinates?
- Maven world has a lot of build, we need to find a unified specification that uniquely identifies a build.
- With a unified specification, you can hand over the search to the machine.
Ii. Dependency Management 2.1, dependency configuration
The dependency configuration consists primarily of the following elements:
1 <!--Add Dependency configuration--2 <dependencies> 3 <!--project to use the jar package to JUnit, so add the JUnit jar package here--4 <dependency> 5 <groupId>junit</groupId> 6 <artifactid>junit</artifactid > 7 <version>4.9</version> 8 <scope>test</scope> 9 </dependency>10 <!--project to use the jar package to Hello, so add the Hello Jar package here dependency-->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> </dependencies>
2.2. Range of dependencies
The dependency scope scope is used to control dependencies and compile, test, and run classpath relationships . The main three types of dependencies are as follows:
1.Compile: default compilation Dependency range . For compiling, testing, running three kinds of classpath are valid
2.test: Test the dependency range. Valid only for test classpath
3.provided: The range of dependencies has been provided. For compilation, the classpath of the test is valid, but not valid for running. Because by the container already provided, for example Servlet-api
4.runtime: Available at runtime. For example: JDBC Driver
2.3. Transitive dependence
Makefriends.jar directly depends on the Hellofriends.jar, and Hellofriends.jar is directly dependent on the Hello.jar, then Makefriends.jar also relies on Hello.jar, which is transitive dependence, but this dependence is indirectly based on As shown in the following:
2.4. Optional dependency three, warehouse management 3.1, MAVEN warehouse
The location for unified storage of all Maven shared builds is the warehouse
3.2. Maven Warehouse Layout
Define the unique storage path for each build in the warehouse according to MAVEN coordinates, roughly: groupid/artifactid/version/artifactid-version.packaging
3.3, the warehouse classification 3.3.1, the local warehouse
Each user has only one local repository, which by default is the user directory represented by ~/.m2/repository/,~
3.3.2, Remote Storage
1, Central warehouse: MAVEN default remote warehouse, URL address: http://search.maven.org/
2: A special Remote Storage, it is built in the local area network warehouse
Four, life cycle 4.1, what is the life cycle?
The MAVEN lifecycle is designed to abstract and unify all build processes, including project cleanup, initialization, compilation, packaging, testing, deployment, and almost any build step
4.2. Maven Three life cycle
MAVEN has three sets of independent lifecycles, please note that this is said to be "three sets", and "independent", these three sets of life cycle are:
- Clean Lifecycle Some cleanup work before making a real build.
- The core part of the Default Lifecycle build, compile, test, package, deploy, and so on.
- Site Lifecycle generates project reports, sites, and publishing sites.
Again, they are independent, and you can simply call clean to clear the working directory and simply call site to build the site. Of course you can also run MVN clean install site to run all of these three sets of lifecycles directly.
The clean life cycle consists of a set of stages (Phase), and the commands we normally enter at the command line always correspond to a specific stage. For example, running MVN clean, this clean is a phase of the clean life cycle. There is a clean life cycle, and there is a clean phase. The clean life cycle consists of three phases:
- Pre-clean perform some work that needs to be done before clean
- Clean removes all previous build-generated files
- Post-clean performs some work that needs to be done immediately after clean
The clean in "mvn clean" is the clean in the above, and in a life cycle, when running a phase, all the stages before it will be run, that is, "mvn clean" is equivalent to MVN pre-clean clean, if we run MVN Post-clean, then Pre-clean,clean will be run. This is a very important rule for MAVEN, which can greatly simplify the input of the command line.
Site life cycle Pre-site perform some work that needs to be done before the site document is generated
- Site-generated Project Web document
- Post-site performs some work that needs to be done after the site document is built, and prepares for deployment
- Site-deploy deploy the generated site document to a specific server
The site phase and the Site-deploy phase are often used here to generate and publish Maven sites, which is a pretty powerful feature of Maven, and the manager prefers that documents and statistics are generated automatically and beautifully.
The default life cycle default lifecycle is the most important one in the Maven life cycle, and most of the work happens in this life cycle. Here, only some of the more important and common stages are explained:
- Validate
- Generate-sources
- Process-sources
- Generate-resources
- Process-resources copy and process the resource files to the target directory, ready to package.
- Compile the source code of the compiled project.
- Process-classes
- Generate-test-sources
- Process-test-sources
- Generate-test-resources
- Process-test-resources copy and process the resource file to the target test directory.
- Test-compile Compile the test source code.
- Process-test-classes
- Test runs the tests using the appropriate unit test framework. These test codes are not packaged or deployed.
- Prepare-package
- The package accepts compiled code, packaged in a ready-to-publish format, such as a JAR.
- Pre-integration-test
- Integration-test
- Post-integration-test
- Verify
- Install installs the package to the local repository for other projects to rely on.
- Deploy copies the final package to a remote repository for other developers to share with the project.
At any stage of the run, all the stages in front of it will be run, which is why when we run MVN install, the code is compiled, tested, packaged. In addition, MAVEN's plug-in mechanism relies entirely on the life cycle of maven, so understanding the lifecycle is critical.
V. Maven Plugin
- The core of Maven simply defines the abstract life cycle, and the specific tasks are done by the plugin.
- Each plug-in can implement multiple functions, each of which is a plug-in target.
- MAVEN's lifecycle is tied to plug-in targets to accomplish a specific build task, such as compile is a plug-in target for plug-in Maven-compiler-plugin.
Maven Learning Summary (iv)--maven core concept