The benefits of Maven

Dependency management: Maven’s process of managing jar packages in a project. In traditional projects we place jar packages directly in the project. The maven project’s real JAR package is placed in the repository, and only the coordinates of the JAR package are used in the project.

One-click Build: Maven integrates the Tomcat plugin itself to compile, test, package, install, and publish projects

  • Save disk space
  • Can be built with one click
  • Cross-platform
  • Application in large projects can improve development efficiency

warehouse

  • Local repository
  • Remote warehouse (private server)
  • The central warehouse

Relationship between warehouses: By default, if there is no jar in the local repository, the Maven project automatically downloads the jar from the central repository. In the company, if there is no jar in the local repository, the Maven project automatically downloads the jar from the central repository. The jar package will be downloaded from the private server first. If the private server does not have the IAR package, you can download it from the central repository or upload it locally.

Maven commands

The command explain
mvn package Packaging releases
mvn clean This command can be used together with related commands, such as MVN clean package and MVN clean test
mvn install Package it and install it in the local repository, then provide other project Settings dependencies to import
mvn build Repackage the Maven project
mvn deploy Publish packaged files to a remote (such as a server) reference for other people to download and rely on
mvn compile To compile the project, use this command to compile the code in the SRC /main/ Java directory
mvn test Test command, or execute all junit test cases under SRC /test/ Java /

The main composition and writing specification of Maven coordinates

coordinates explain
groupId Defines which project the current Maven project belongs to, in reverse order of the company or organization domain name
artifactId Project name or module name that defines a module in the actual project
version Defines the current version of the current project
package Defines how the current project is packaged

Add the coordinates

  1. Search your local warehouse
  2. Search on the Internet, recommend the website www.mvnrepository.com/

Maven features

Depend on the transfer

In Maven, dependency is transitive, that is, assuming that there are three projects, namely project A, project B and project C, assuming that C depends on B and B depends on A, then we can deduce that project C also depends on A according to the characteristics of Maven project dependency.

Inheritance in Maven

For example, we have A parent project, maven-parent, which has A child project A. If the parent project relies on junit, then the child project A can still use junit even if junit is not introduced. Because the subproject naturally inherits the junit dependencies from the parent project.

Maven jar package conflict

Direct dependencies: Jar packages imported directly into the project are the direct dependencies of the project. Pass dependencies: There is no jar package directly imported in the project, you can pass the jar package directly to the project to resolve the JAR package flushing method:

  1. Declarator first: whichever JAR has the coordinates of the top jar is declared first. Dependencies under the coordinates of the jar package declared first can be entered in the project first.
  2. Path-nearest priority principle: The direct dependent path is closer than the pass-dependent path, so the final JAR package entered by the project will be the jar package with the closest path
  3. Exclusions: If the EXCLusions tag is configured for a JAR dependency package, the version number can be omitted. This is because the dependency package uses the same version and default as this JAR package. As examples:
<exclusions>
	<exclusion>
		<group Id>org. springframework</group Id>
		<artifactId spring-core</artifactId) < /exclusion>
</exclusions>
Copy the code

Maven has three life cycles

Each life cycle consists of phases, which are sequential, with subsequent phases depending on previous phases.

1. Clean Lifecycle: The clean project consists of three phases.

  1. Pre-clean: indicates the work to be completed before the cleaning
  2. Clean: Clears the files generated during the last build
  3. Post-clean: indicates the work to be completed after the cleaning

2. The Default lifecycle: Build the project

  1. Validate: Verifies that the project is correct and all required resources are available.
  2. Compile: Compile the source code of the project.
  3. Test: Test compiled source code using an appropriate unit testing framework. These tests do not need to be packaged and deployed.
  4. Package: Packages compiled code into a releasable format, such as a JAR.
  5. Integration-test: Process and publish packages to an environment where integration testing can take place, if necessary.
  6. Verify: Runs all checks to verify that the package is valid and meets quality standards.
  7. Install: Installs packages into maven’s local repository that can be used as dependencies by other projects.
  8. Deploy: Executes in an integration or publishing environment and copies the final version of the package to a remote repository so that other developers or projects can share it.

3. Site life cycle: establish and publish the project site

  1. Pre-site: The work that needs to be done before the project site is generated
  2. Site: Generates project site documents
  3. Post-site: The work that needs to be done after the project site is generated
  4. Site-deploy: Publishes the project site to the server