1 the Maven profile
1.1 What is Maven
Maven is a project management tool that abstracts the project development and management process into a project object model
Project Object Model (POM) : Project Object Model
1.2 Functions of Maven
-
Project construction: provides standard, cross-platform automated project construction
-
Dependency management: Manages the resources (JAR packages) that the project depends on to avoid version conflicts between resources
-
Unified development structure
2 Basic Concepts of Maven
2.1 warehouse
Repository: Used to store resources, containing various JAR packages
Local repository: a repository of resources stored on one’s own computer, which can be obtained from a private server or a central repository
Central repository: The Maven team maintains and stores all resources
Private server: a repository for storing resources within a department or company, which can hold copyrighted resources (self-developed JAR packages) or be used as a cache (faster, avoiding access to a central repository)
The Maven repository is as follows:
mvnrepository.com/
2.2 coordinates
Coordinates in Maven are used to describe the location of resources in the repository
Major components of Maven coordinates
- GroupId: Define the name of the organization to which the project belongs (usually domain name reversed, e.g. Org.mybatis)
- ArtifactId: Define project name (usually module name, e.g. CRM, SMS)
- Version: Defines the project version number
2.3 Warehouse Configuration
- Maven automatically saves the downloaded resources to a local repository whose location can be configured
- Remote repositories that Maven connects to by default can also be configured
- Ali cloud mirror warehouse can be configured to improve download speed
3 the Maven project
3.1 Maven Project directory structure
Java project on the left and Web project on the right
Maven projects can be created using plug-ins
SRC The pom. XML configuration file is stored in the same directory
3.2 Maven build commands
Maven build commands start with MVN, followed by functional parameters, and you can execute multiple commands at once, separated by Spaces
Maven build command | role |
---|---|
mvn compile | compile |
mvn clean | Clean up the |
mvn test | test |
mvn package | packaging |
mvn install | Install to a local repository |
4 Dependency Management
4.1 Dependency Configuration
Dependencies refer to the jars that the current project needs to run, and a project can have multiple dependencies
Format:
<! --> <dependencies> <! -- Set specific dependencies --> <dependency> <! <groupId> XXX </groupId> <! <artifactId> XXX </artifactId> <! </version> </dependency> </dependencies>Copy the code
4.2 Dependency Transfer
Dependencies are transitive, including direct and indirect transfer.
Directly: in the current project build dependencies by relying on configuration (A with B, A and B is the direct transfer) indirect transmission: by relying on resources if rely on other resources, the current project indirect depends on other resources (compare man, mean if A relies on B, and rely on C, B is the indirect transfer) between A and C
Dependency passing conflicts:
Path first: when in the same resource, relying on the deeper level, the lower the priority, the hierarchy, the more shallow, the higher the priority Statement: priority when resources at the same level is dependent on the configuration sequence on the front cover configuration order special priority: when the configure different versions of the same resources at the same level, the configuration of cover configuration first
4.3 Optional dependencies and Dependency exclusion
Optional dependencies refer to hiding currently dependent resources from the outside world
<dependency> <groupId>xxx</groupId> <artifactId>xxx</artifactId> <version>xxx</version> <! Add the following line --> <optional>true</optional> </dependency>Copy the code
Exclude a dependent resource. You do not need to specify the version of the excluded resource
<dependency>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>xxx</version>
<exclusions>
<exclusion>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
</exclusion>
</exclusions>
</dependency>
Copy the code
4.4 Dependency Scope
Dependent JAR packages can be used anywhere by default, and their scope can be set through the Scope tag
Scope of action:
Main program scope valid (in the main folder) Test program scope valid (in the test folder) Whether to participate in packaging (in the package folder)
5 Life cycle and plug-ins
5.1 Life Cycle
The Maven project build lifecycle describes how many events occur during a build
Maven divides the project build lifecycle into three sets
- “Clean” : cleaning up
- Default: core work, such as compile, test, package, and deployment (compile – test-compile – test- packege – Install)
- Site: generates reports, publishes sites, etc
5.2 The plug-in
Plug-ins are bound to phases within the lifecycle and perform the corresponding plug-in functionality when the lifecycle is executed
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.2.1</version> <executions> <goals> <goal> </goal> </goals> <phase> generate-test-resources</phase> </execution> </executions> </plugin> </plugins> </build>Copy the code
Module development and design
7 the aggregation
Purpose: Aggregation is used to quickly build Maven projects, building multiple projects/modules at once
Make way
-
Create an empty module with a package type defined as POM
<packaging>pom</packaging> Copy the code
Module type: POM; jar; war…
-
Defines other module names associated with the current module’s build operation
<modules> <module>.. /ssm_controller</module> <module>.. /ssm_service</module> <module>.. /ssm_dao</module> <module>.. /ssm_pojo</module> </modules>Copy the code
Note: The final execution order of the modules involved in the aggregation operation depends on the dependencies between the modules, not the configuration order
8 inheritance
What it does: Through inheritance, you can implement the same configuration as the parent project in the child project (similar to Java).
Make way
-
Coordinates and corresponding positions of life’s parent project in a subproject
<! Itheima </groupId> <artifactId> SSM </artifactId> <version>1.0-SNAPSHOT</version> <! Fill in the parent project's POM file --> <relativePath>.. /ssm/pom.xml</relativePath> </parent>Copy the code
-
Define dependency management in the parent project
<! --> <dependencyManagement> <! <dependencies> <groupId>org.springframework</groupId> <artifactId> Spring context</artifactId> < version > 5.1.9. RELEASE < / version > < / dependency > < / dependencies > < / dependencyManagement >Copy the code
-
Use inherited dependencies: Define dependencies in the child project without declaring the dependency version. The version refers to the version of dependencies in the parent project
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> </dependencies>Copy the code
Inherited resources: groupId, Version, dependencies…
Inheritance and aggregation
-
Function:
- Aggregation is used to quickly build projects and inheritance is used for quick configuration
-
Similarities:
- Both the aggregation and inheritance pom. XML files are packaged in POM, and the two relationships can be made into the same POM file
- Aggregation and inheritance are design modules without actual module content
-
Difference:
- Aggregation is the configuration of relationships in the current module, and aggregation is aware of which modules are participating in the aggregation
- Inheritance is the configuration of relationships in submodules, and the parent module cannot sense which submodules inherit from it
9 attribute
-
Custom attributes
Function: Equal to define variables, convenient unified maintenance
Define the format
<! RELEASE</spring.version> <junit.version>4.12</junit.version> </properties>Copy the code
Call format
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> Copy the code
-
Built-in properties
Use Maven built-in properties for quick configuration
Call format
${basedir} ${version} Copy the code
-
Setting attributes
Purpose: Use the tag attribute in Maven configuration file setting.xml for dynamic configuration
Call format
${settings.localRepository} Copy the code
-
Java System Properties
Function: Reads Java system properties
Call format
${user.home} Copy the code
System attribute query methods:
mvn help:system Copy the code
-
Environment variable attributes
Purpose: Use the tag attribute in Maven configuration file setting.xml for dynamic configuration
Call format
${env.JAVA_HOME} Copy the code
Method for querying environment variable attributes
mvn help:system Copy the code
10 Version Management
SNAPSHOT RELEASE
11 Resource Configuration
The configuration file references the POM properties, as shown in the red line above, by calling ${jdbc.url} directly.
Effect: Loads properties defined in poM files in any configuration file
Call format
${jdbc.url}
Copy the code
Enable the configuration file to load POM attributes
<! <resources> <resource> <! ${project.basedir}/ SRC /main/resources</directory> <! -- Enable resource load filtering for configuration files --> <filtering>true</filtering> </resource> </resources>Copy the code
12 Multi-environment development configuration
<! -- Create multiple environments --> <profiles> <! Define the specific environment: production environment --> <profile> <! <id>pro-env</id> <! <properties> <jdbc.url> JDBC link </jdbc.url> </properties> <! -- Set default activation --> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <! Define the specific environment: development environment --> <! -- same format as above --> </profiles>Copy the code
Loading the specified environment
Call for
mvn install -P pro-env
Copy the code
13 private servers
It doesn’t need to be mastered yet
14 the reference
Maven project management from basic to advanced Bilibili