Maven Learning Summary (iv)--maven core concept

Source: Internet
Author: User

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:

<!--Add Dependency configuration-    <dependencies>        <!--project to use the jar package to JUnit, so add JUnit's jar package here----        < dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.9</version>            <scope>test</scope>        </dependency>        <!-- The project is going to use the jar package to Hello, so add the Hello jar package here--        <dependency>            <groupid>me.gacl.maven</ groupid>            <artifactId>Hello</artifactId>            <version>0.0.1-SNAPSHOT</version>            <scope>compile</scope>        </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:

    1. Clean Lifecycle Some cleanup work before making a real build.
    2. The core part of the Default Lifecycle build, compile, test, package, deploy, and so on.
    3. 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:

    1. Pre-clean perform some work that needs to be done before clean
    2. Clean removes all previous build-generated files
    3. 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

    1. Site-generated Project Web document
    2. Post-site performs some work that needs to be done after the site document is built, and prepares for deployment
    3. 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
      1. The core of Maven simply defines the abstract life cycle, and the specific tasks are done by the plugin.
      2. Each plug-in can implement multiple functions, each of which is a plug-in target.
      3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.