Maven instructions

Table of contents

1. Description

2. Detailed description

3. Maven model

4. Maven commonly used commands

5. Maven life cycle

6. Maven coordinates

7. Dependency management and dependency scope


1. Description

Maven is a tool specially used to manage and build Java projects. It is based on the concept of the project object model (POM). The main functions are:

Provides a standardized project structure

Provides a standardized build process (compile, test, package, release)

Provides a set of dependency management mechanisms

2. Detailed description

(1) Maven provides a set of standardized project structures

The project structures built by all IDEs are consistent, and the projects built by all IDEs are universal. For example, projects created by eclipse can be directly imported into idea for use.

(2) Maven provides a set of standardized construction process

 Maven provides a set of simple commands to complete the construction of the project.

Common commands:

mvn -v //查看版本 
mvn archetype:create //创建 Maven 项目 
mvn compile //编译源代码 
mvn test-compile //编译测试代码 
mvn test //运行应用程序中的单元测试 
mvn site //生成项目相关信息的网站 
mvn package //依据项目生成 jar 文件 
mvn install //在本地 Repository 中安装 jar 
mvn -Dmaven.test.skip=true //忽略测试文档编译 
mvn clean //清除目标目录中的生成结果 
mvn clean compile //将.java类编译为.class文件 
mvn clean package //进行打包 
mvn clean test //执行单元测试 
mvn clean deploy //部署到版本仓库 
mvn clean install //使其他项目使用这个jar,会安装到maven本地仓库中 
mvn archetype:generate //创建项目架构 
mvn dependency:list //查看已解析依赖 
mvn dependency:tree com.xx.xxx //看到依赖树 
mvn dependency:analyze //查看依赖的工具 
mvn help:system //从中央仓库下载文件至本地仓库 
mvn help:active-profiles //查看当前激活的profiles 
mvn help:all-profiles //查看所有profiles 
mvn help:effective -pom //查看完整的pom信息

Common packaging commands:

1. mvn clean package -Dmaven.test.skip=true
executes clean, resources, compile, testResources, testCompile, test, jar (package) and other 7-stage
package commands in sequence to complete project compilation, packaging functions, and skip units Test, but did not deploy the packaged executable jar package (war package or other form of package) to the local maven warehouse and remote maven private server warehouse.
2. mvn clean install -Dmaven.test.skip=true
executes clean, resources, compile, testResources, testCompile, test, jar (packaging), install and other 8 phase
install commands in sequence to complete the project compilation, packaging function, skip The unit test is performed, and the executable jar package (war package or other form of package) is deployed to the local maven warehouse, but not to the remote maven private server warehouse.
3. mvn clean deploy -Dmaven.test.skip=true
executes clean, resources, compile, testResources, testCompile, test, jar (package), install, deploy and other 9 stage
deploy commands in sequence to complete the project compilation, packaging functions, The unit test is skipped, and the executable jar package (war package or other form of package) is deployed to the local maven warehouse and the remote maven private server warehouse at the same time.

(3) Provides a set of dependency management mechanisms

Dependency management is actually managing the third-party resources that your project depends on, such as jar packages, plug-ins, etc.

When maven is not used:

You need to download the jar package, copy the jar package to the project, and add the jar package to the working environment.

The steps are relatively cumbersome.

When using Maven:

Maven uses standard coordinate configurations to manage various dependencies.

Only simple configuration is required to complete dependency management.

3. Maven model

Warehouses are divided into local warehouses, central warehouses and remote warehouses.

Local warehouse: a directory on your own computer.

Central warehouse: The only warehouse in the world maintained by the Maven team.

Address: https://repo1.maven.org/maven2

Remote warehouse (private server): Generally, a private warehouse built by a company team.

When the coordinates are used in the project to import the corresponding dependent jar package, it will first check whether there is a corresponding jar package in the local warehouse:
if there is, it will be directly referenced in the project;
if not, go to the central warehouse to download the corresponding jar package to the local warehouse .

You can also build a remote warehouse. After building, the search order of the jar package becomes:

Local warehouse -> remote warehouse -> central warehouse

4. Maven commonly used commands

5. Maven life cycle

(1) Clean Lifecycle (Clean Lifecycle)
Clean Lifecycle performs some cleaning work before the actual construction. The Clean life cycle consists of three phases:

pre-clean performs some work that needs to be done before clean.

clean removes all files generated by the previous build.

post-clean performs some work that needs to be done immediately after clean.

(2) Default Lifecycle (Default Lifecycle)
Default Lifecycle is the core part of construction, compiling, testing, packaging, deployment, etc. Default lifecycle is the most important one in the Maven lifecycle, and most of the work takes place in this lifecycle cycle.

Some of the more important and commonly used stages:

compile compiles the source code of the project.

install installs the package to the local warehouse so that other projects can depend on it.

deploy copies the final package to a remote repository for other developers to share with the project.

process-test-resources Copy and process resource files to the target test directory.

process-resources Copy and process resource files to the target directory, ready for packaging.

test-compile compiles test source code.

test runs tests using a suitable unit testing framework. These test codes will not be packaged or deployed.

package takes compiled code and packages it into a distributable format, such as a JAR.

validate

generate-sources

process-sources

generate-resources

process-classes

generate-test-sources

process-test-sources

generate-test-resources

process-test-classes

prepare-package

pre-integration-test

integration-test

post-integration-test

verify

(3) Site Lifecycle (Site Lifecycle)
Site Lifecycle generates project reports, sites, publishing sites, and site documents (site information, dependencies..). Site life cycle, generating site information in four stages:

pre-site performs some work that needs to be done before generating site documentation.

site Generates the project's site documentation.

post-site performs some work that needs to be done after the site documentation is generated and prepared for deployment.

site-deploy deploys the generated site documentation to a specific server.

6. Maven coordinates

What are coordinates?
A coordinate in Maven is a unique identifier for a resource.
Use coordinates to define the project or import dependencies needed in the project.

Maven coordinates mainly consist of
groupld: define the name of the organization to which the current Maven project belongs (usually the domain name is reversed, for example: com.itheima).
artifactld: defines the current Maven project name (usually the module name, such as order-service, goods-service).
version: defines the current project version number.

7. Dependency management and dependency scope

(1) Dependency management

 

(2) Dependency scope

Through the scope, you can set the jar package to be valid in the compilation environment, the test environment or the running environment.

Guess you like

Origin blog.csdn.net/m0_72167535/article/details/129230835