Sub-module development and design
-
create module
-
Write module code
-
Install the module to the local warehouse through the maven command (install command)
- Import coordinates in pom.xml
- Execute the maven install command to install the module to the local maven warehouse
- The internal development of the team can publish the module function to the warehouse that can be shared within the team (private server)
dependency management
-
Dependency refers to the jar s required for the current project to run, and a project can set multiple dependencies
-
Format:
<!--Set all the dependencies of the current project jar--> <dependencies> <!--Set specific dependencies--> <dependency> <!--Depends on the group id--> <groupId>org.springframework</groupId> <!--Depends on the project id--> <artifactId>spring-webmvc</artifactId> <!--Dependency version number--> <version>5.2.10.RELEASE</version> </dependency> </dependencies>
dependency transitive
Dependency is transitive
- Direct dependencies: dependencies established through dependency configuration in the current project
- Indirect dependency: If the dependent resource depends on other resources, the current project indirectly depends on other resources
dependency transitive conflict
Dependency transfer conflict problem
- Path priority: When the same resource appears in the dependency, the deeper the hierarchy, the lower the priority, and the shallower the hierarchy, the higher the priority
- Declaration priority: When resources are depended on at the same level, the configurations in the first order override the configurations in the latter order
- Special priority: When different versions of the same resource are configured at the same level, the one configured later overrides the one configured earlier
optional dependencies
- Optional dependency refers to hiding the resources currently dependent on the outside world - opaque
<dependency> <groupId>com.itheima</groupId> <artifactId>maven_03_pojo</artifactId> <version>1.0-SNAPSHOT</version> <!--Optional dependency is to hide the resources that the current project depends on. After hiding, the corresponding resources will not have dependency transitivity--> <optional>false</optional> </dependency>
exclude dependencies
- Excluding dependencies refers to actively disconnecting dependent resources. Excluded resources do not need to specify a version - no need
<dependency> <groupId>com.itheima</groupId> <artifactId>maven_04_dao</artifactId> <version>1.0-SNAPSHOT </version> <!--Excluding dependencies is to hide the dependencies corresponding to the current resource--> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </exclusion> </exclusions> </dependency>
- To exclude dependent resources, only specify GA, no need to specify V
Aggregation and Inheritance
polymerization
- Aggregation: The process of organizing multiple modules into a whole and building projects at the same time is called aggregation
- Aggregate project: usually an "empty" project without business functions (with and only one pom file)
- Function: Use the aggregation project to group multiple projects, and build the aggregation project to realize the synchronous construction of the included modules
- When a module in the project is updated (changed), it is necessary to ensure that the modules associated with the updated module in the project are updated synchronously. At this time, the aggregation project can be used to solve the problem of synchronous building of batch modules
Polymer Engineering Development
-
Create a Maven module and set the packaging type to pom
<packaging>pom</packaging>
- Each maven project has a corresponding packaging method, the default is jar, and the web project packaging method is war
-
Set the submodule name contained in the current aggregation project
<modules> <module>../maven_ssm</ module> <module>../maven_pojo</module> <module>../ maven_dao</ module> </modules>
- The modules contained in the aggregation project will set the construction order according to the dependencies between the modules when they are built. It has nothing to do with the configuration writing position of the modules in the aggregation project. The projects participating in the aggregation cannot sense upward whether to participate in the aggregation, and can only configure which modules downward. Participate in the aggregation of this project
inherit
-
Concept: Inheritance describes the relationship between two projects, similar to inheritance in java, sub-projects can inherit the configuration information in the parent project, common in the inheritance of dependencies
-
effect:
- Simplified configuration
- Reduce version conflicts
-
Dependencies and optional dependencies can be configured in the parent project (using dependencyManagement)
step
-
Create a Maven module and set the packaging type to pom
<packaging>pom</packaging>
- It is recommended that the packaging method of the parent project be set to pom
-
Configure dependencies in the pom file of the parent project (the child project will inherit the dependencies in the parent project)
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.10.RELEASE</version> </dependency> ... </dependencies>
-
Configure optional dependencies in subprojects
<dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency> ... </dependencies> </dependencyManagement>
-
Configure the parent project inherited by the current project in the subproject
<!--Define the parent project of this project--> <parent> <groupId>com.itheima</groupId> <artifactId>maven_parent</artifactId> <version>1.0-SNAPSHOT</version> <!--Fill in the parent project's pom document--> <relativePath>../maven_parent/pom.xml</relativePath> </parent>
-
Configure coordinates that use optional dependencies in the parent project in the subproject
<dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> </dependencies>
- When using optional dependencies in the parent project in the sub-project, only the group id and project id need to be provided, and the version is not required. The version is provided by the parent project to avoid version conflicts. Sub-projects can also define dependencies that are not defined in the parent project relation
The Difference Between Aggregation and Inheritance
- effect
- Aggregation for quickly building projects
- Inheritance for quick configuration
- Same point
- The pom.xml files of aggregation and inheritance are packaged in pom, and the two relationships can be made into the same pom file
- Aggregation and inheritance belong to design modules and have no actual module content
- difference
- Aggregation is to configure the relationship in the current module. Aggregation can perceive which modules participate in the aggregation
- Inheritance is to configure relationships in submodules, and the parent module cannot perceive which submodules inherit itself
Attributes
custom attributes
- The version of the dependency used in the pom file is represented by a variable, which can be managed uniformly when it is modified
Attribute configuration and application steps
-
define properties
<!--Define custom properties--> <properties> <spring.version>5.2.10.RELEASE</spring.version> <junit.version>4.12</junit.version> </ properties>
-
reference attribute
Use ${} to get the value:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency>
- Refer to the properties defined in maven in the resource file
-
Define properties (in the pom file)
<!--Define custom properties--> <properties> <spring.version>5.2.10.RELEASE</spring.version> <junit.version>4.12</junit.version> <jdbc.url>jdbc:mysql://127.0.0.1:3306/ssm_db</jdbc.ur1> </properties>
-
Reference attributes in the configuration file (in the properties configuration file)
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=$ijdbc.url} jdbc.username=rootjdbc.password=root
-
Enable the filter of resource file directory loading properties (in the pom file)
- After it is enabled, the ${} placeholder in the properties configuration file will be replaced with the corresponding variable value
<build> <resources> <resource> <directory>${project.basedir}/src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>
-
When configuring maven to make a war package, ignore the web.xml check (in the pom file)
- When the web.xml file is missing in the webapp/WEB-INF directory, an error will be reported when using war packaging
- You can create an empty web.xml file
- Or configure the packaging tool, as follows in the pom:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.2.3</version> <configuration> <failoOnMissingwebXm1>false</failonMissingwebXm1> </configuration> </plugin>
other attributes
- Other attributes (understand)
attribute list
- Custom attributes (commonly used)
- built-in properties
- Setting property
- Java system properties
- environment variable properties
version management
-
Engineering version:
-
SNAPSHOT (snapshot version)
The version temporarily output during the project development process is called the snapshot version
The snapshot version will be updated continuously as the development progresses
-
RELEASE (release version)
After the project development reaches the milestone in the stage, a relatively stable version is released to the outside of the team. The component files corresponding to this version are stable. Even if the subsequent development of functions is carried out, the content of the current released version will not be changed. This version is called release version
-
-
release version
- alpha version
- beta version
- Pure Digital Edition
Multi-environment configuration and application
Multi-environment development
- maven provides settings for configuring multiple environments to help developers quickly switch environments (development, testing, production) during use
-
Define multiple environments
<!--Define multiple environments--> <profiles> <!--define a specific environment:Production Environment--> <profile> <!--Define the unique name corresponding to the environment--> <id>env_dep</id> <!--Defines property values specific to the environment--> <properties> <jdbc.url>jdbc:mysql://127.0.0.1:3306/ssm_db</jdbc.url> </properties> <!--Set default startup--> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <!--define a specific environment:development environment--> <profile> <id>env_pro</id> ... </profile> </profiles>
-
Using multiple environments (build process)
mvn instruction -P environment definition id
example:
mvn install -P pro_env
skip test
Application Scenario
-
The function is being updated and not yet developed
-
quick pack
-
... ...
Use the command to skip tests
mvn instruction -D skipTests
- example
mvn install -D skipTests
Fine-grained control over skipping tests
<plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> <configuration> <!--Set skip test--> <skipTests>true</skipTests> <!--Contains the specified test case--> <includes> <include>**/User*Test.java</include> </includes> <!--Exclude specified test cases--> <excludes> <exclude>**/User*TestCase.java</exclude> </excludes> </configuration> </plugin>
private server
-
Private server profile
- The private server is an independent server, which is used to solve the problem of resource sharing and resource synchronization within the team
-
Nexus
-
A maven private server product of Sonatype company
-
Download address: https://help.sonatype.com/repomanager3/download
-
Nexus installation and startup
-
Start the server (command line start)
nexus.exe /run nexus
-
Access server (default port: 8081)
http://localhost:8081
-
Modify basic configuration information
- The nexus-default.properties file in the etc directory under the installation path saves the basic configuration information of nexus, such as the default access port
-
Modify server running configuration information
- The nexus.vmoptions file in the bin directory under the installation path saves the configuration information corresponding to the startup of the nexus server, such as the memory space occupied by default
Private server warehouse classification
Warehouse category | English name | Function | Associated operations |
---|---|---|---|
host repository | hosted | Preserve independent research and development + third-party resources | upload |
Agent warehouse | proxy | Proxy connects to central warehouse | download |
warehouse group | group | Group for repositories to simplify download operations | download |
Steps for usage
Local warehouse access private server permission settings
- Configuration location (in maven's setting.xml file)
<servers> <!-- Configure permissions to access private servers --> <server> <!-- private servers id name --> <id>jihua-release</id> <username>admin</username> <password>admin</password> </server> <server> <!-- private servers id name --> <id>jihua-snapshot</id> <username>admin</username> <password>admin</password> </server> </servers>
<mirrors> <!-- Configure the warehouse address of the private server --> <mirror> <id>maven-public</id> <!-- Configure all resources from this warehouse --> <mirrorOf>*</mirrorOf> <url>http://localhost:8081/repository/maven-public/</url> </mirror> </mirrors>
Project upload to private server settings
-
Configuration location (in the project pom file)
<!--Configure private server--> <distributionManagement> <!--release Version--> <repository> <id>jihua-release</id> <url>http://localhost:8081/repository/jihua-release/</url> </repository> <!--snapshot Version--> <snapshotRepository> <id>jihua-snapshot</id> <url>http://localhost:8081/repository/jihua-snapshot/</url> </snapshotRepository> </distributionManagement>
- When publishing, it will be automatically published to the corresponding warehouse according to the version number suffix
-
Publish command (maven lifecycle deploy command)
mvn deploy
Private server access to central server settings
-
configuration location
Set the page in the background of the nexus server, enter the maven-central warehouse, set Remote storage and modify the url to the corresponding address, for example, the maven warehouse address of Alibaba Cloud: https://maven.aliyun.com/repository/public
-
Remember to save after modification