Function and use of maven

First, we need to clarify the role of maven:

  1. Maven is essentially a project management tool, Abstract the project development and management process into a project object model (POM). Developers only need to make some simple configurations to complete the construction, report and document generation of the project in batches. Using remote warehouse, local warehouse and a pom.xml similar to build.xml, download the jar file defined in pom.xml from the remote warehouse to the local warehouse. Each application uses the jar of the same local warehouse and the same version of JA R only needs to be downloaded once, and it avoids every application copying jars.

  2. There is a POM in the maven project XML file to complete the configuration and download of the jar package (the wheel already written by others)

  3. Each < dependencies > is configured with a dependency >,
    <groupIdorg. Springframework < / groupid project name
    < artifactidspring webmvc < / artifactid project module
    <version3. 0.5. Release < / version project version
    The pom file in maven project can be quickly downloaded to the corresponding jar package
    Introduce the class library into the project in a dependent manner

  4. You can dynamically integrate the plug-ins you need into Maven to expand new management functions.

Maven's life cycle

1. clear: remove the files generated by the last build

2. compile: edit the source code of the project

3. Test: use the unit test framework for testing, and the test code will not be packaged or deployed

4. Package: accept the compiled code and package it into a releasable format

5. Install: install the package to the maven local repository for use by other maven projects locally

6. deploy: publish the final package to a remote repository for use by other developers and maven projects.

7. Site: generate site documents for the project

Directory structure of maven project

Transitivity of dependency

  1. Definition: if project A depends on project B, if A jar package is added to project B, it can also be used in project A. matters needing attention:
  2. Need to be in POM of project A In XML, configure the dependency information
  3. Project B needs to be installed to ensure that project B exists in the local warehouse
  4. Project A relies on all jar packages of Spring. Now it needs to upgrade from version 4.0 to version 4.1. If you manually modify them one by one, it is cumbersome and error prone. You only need to change the value of the configuration.
<!-->properties Use a custom label in the label and name it yourself<-->
<dependency>
	<groupId></groupId>
	<artifactId></artifactId>
	<version>${dfr.Spring.Version}</version>
	<scope></scope>
</dependency>

Parent child project dependent inheritance

Due to the dependency of the scope, it does not have the feature of dependency transmission. As a result, each project needs to declare the dependent jar package, which is easy to cause version inconsistency.
Create parent project: the parent project needs to specify the packaging method as pom.

POM of parent project XML configures the jar dependency as follows:

<!--dependencyManagement The dependencies declared under the tag will not be automatically introduced by the subclass, but also need to be referenced by the displayed subclass-->  
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>com.dfr</groupId>
			<artifactId>test-jar</artifactId>
			<version>4.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</dependencyManagement>
 
<!--dependencies The dependencies declared under the tag will be automatically introduced by subclasses-->  
<dependencies>
	<dependency>
		<groupId>com.dfr</groupId>
		<artifactId>java-jar</artifactId>
		<version>4.1</version>
	</dependency>
</dependencies>

In the POM of the subproject Declare parent project in XML:

<!--Inherit parent class-->  
<parent>
  <groupId></groupId>
  <artifactId></artifactId>
  <version></version>
  
  <!-- Of the parent project based on the current file pom.xml Relative path to file -->
  <relativePath>../Parent/pom.xml</relativePath>
</parent>
 
<!--Dependency--> 
<!--If no version number is specified, the version number specified in the parent project is used--> 
<dependencies>  
	<dependency>  
		<groupId>com.dfr</groupId>  
		<artifactId>test-jar</artifactId>  
	</dependency>  
</dependencies>  

Keywords: Maven Spring Project management

Added by blmg911 on Sun, 26 Dec 2021 06:25:01 +0200