Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The principle and usage of Maven

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "the principle and usage of Maven". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the principle and usage of Maven.

Maven Foundation

1. Coordinate

Maven coordinates are mainly composed of (GAV), which determines the location of a jar package.

GroupId: defines the current Maven organization name

ArtifactId: define the actual project name

Version: defines the current version of the current project

two。 Search of coordinates

Visit http://www.mvnrepository.com

3. Dependency management

(1) scope dependency scope, which is used to control the relationship between dependency and compiled, tested and run classpath

(2) compile: default compilation dependency scope. Required for compilation, testing, running, and packaging, for example: struts2-core

(3) test: test the scope of dependency. You need it for testing, but not for compiling, running, and packaging, for example: junit

(4) provided: dependency scope has been provided. It is needed when compiling and testing, but not when running and packaging. Because it is already provided by the container, such as jsp-api, servlet-api

(5) runtime: provided at run time. Testing, running, and packaging are required, but not at compile time. For example: jdbc driver

4. Add jar package

Add the following code to pom:

Junit junit 4.9 test javax.servlet Servlet-api 2.5 provided javax.servlet Jsp-api 2.0 provided

You will find that the jar package has been added to the project

5. Dependency transfer

Direct and indirect dependence, test2 depends on test1,test3, depends on test2,test2, directly depends on test1,test3, depends on test1 indirectly

When the dependency scope is compile, the dependency can be passed

When the dependency scope is test, the dependency will not be passed.

6. Optional dependency

Whether the true/false is optional can also be understood as whether it is passed down. Add the optional option to the dependency to determine whether the dependency is passed downwards, not if it is true, or false if it is false.

7. Rely on the resolution of version conflicts

(1) the principle of first declaration of priority

Org.springframework spring-context 4.2.4.RELEASE org.apache.struts struts2-spring-plugin 2.3.24

(2) according to the principle of giving priority to those who have a near path, add your own jar package.

Org.springframework spring-beans 4.2.4.RELEASE

(3) exclusion principle

Org.apache.struts struts2-spring-plugin 2.3.24 org.springframework spring-beans

(4) the principle of version locking

4.2.4.RELEASE org.springframework spring-context ${spring.version}

8. Life cycle

Compile test package install deploy (post to private server)

Three life cycles:

Clean Lifecycle:

Do some cleanup before you actually build

Default Lifecycle:

Core parts of compile test package install deploy build, compilation, testing, packaging, deployment, etc.

Site Lifecycle:

Generate project reports, sites, publish sites

(1) Clean lifecycle: clean up the project

The Clean life cycle consists of three phases:

Pre-clean does some work that needs to be done before clean clean removes all files generated by the last build post-clean does some work that needs to be done immediately after clean

In other words, mvn clean is equivalent to mvn pre-clean clean.

If we run mvn post-clean, then pre-clean,clean will be run.

This is an important rule for Maven and can greatly simplify command line input.

(2) Default lifecycle: construction project

The Default life cycle is the most important one in the Maven life cycle, and most of the work takes place in this life cycle. Here are some of the more important and commonly used stages

Validate generate-sources process-sources generate-resources process-resources copies and processes the resource files to the target directory, ready to be packaged. Compile compiles the source code of the project. Process-classes generate-test-sources process-test-sources generate-test-resources process-test-resources copies and processes the resource files to the target test directory. Test-compile compiles the test source code. Process-test-classes test runs the tests using the appropriate unit test framework. The test code will not be packaged or deployed. Prepare-package package accepts compiled code and packages it into a publishable format such as 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 so that other developers can share it with the project

When you run any phase, all the phases that precede it will be run

That's why when we run mvn install, the code is compiled, tested, packaged, and installed in the local repository.

In addition, Maven's plug-in mechanism is completely dependent on the life cycle of Maven, so it is important to understand the life cycle.

(3) Sit lifecycle: build project site

Site phase and site-deploy phase are often used here to generate and publish Maven sites, which is a very powerful feature of Maven.

Manager prefer, documents and statistics are automatically generated, very good-looking. We don't have to learn. We'll study it ourselves later.

Pre-site performs some work that needs to be done before the site document is generated site the site document of the generation project post-site performs some work that needs to be done after the site document is generated, and prepares site-deploy to deploy the generated site document to a specific server

9. Model diagram

At this point, I believe you have a deeper understanding of "the principle and use of Maven". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report