Spring learning - Maven advanced

Sub-module development and design

  1. create module

  2. Write module code

  3. Install the module to the local warehouse through the maven command (install command)

    1. Import coordinates in pom.xml
    2. 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-->
    	<!--Set specific dependencies-->
            <!--Depends on the group id-->
            <!--Depends on the project id-->
            <!--Dependency version number-->

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
    <!--Optional dependency is to hide the resources that the current project depends on. After hiding, the corresponding resources will not have dependency transitivity-->

exclude dependencies

  • Excluding dependencies refers to actively disconnecting dependent resources. Excluded resources do not need to specify a version - no need
    <version>1.0-SNAPSHOT </version>
    <!--Excluding dependencies is to hide the dependencies corresponding to the current resource-->
  • To exclude dependent resources, only specify GA, no need to specify V

Aggregation and Inheritance


  • 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

  1. Create a Maven module and set the packaging type to pom

    • Each maven project has a corresponding packaging method, the default is jar, and the web project packaging method is war
  2. Set the submodule name contained in the current aggregation project

    	<module>../maven_ssm</ module>
        <module>../ maven_dao</ module>
    • 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


  • 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)


  1. Create a Maven module and set the packaging type to pom

    • It is recommended that the packaging method of the parent project be set to pom
  2. Configure dependencies in the pom file of the parent project (the child project will inherit the dependencies in the parent project)

  3. Configure optional dependencies in subprojects

  4. Configure the parent project inherited by the current project in the subproject

    <!--Define the parent project of this project-->
    	<!--Fill in the parent project's pom document-->
  5. Configure coordinates that use optional dependencies in the parent project in the subproject

    • 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


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

  1. define properties

    <!--Define custom properties-->
    </ properties>
  2. reference attribute

    Use ${} to get the value:

  • Refer to the properties defined in maven in the resource file
  1. Define properties (in the pom file)

    <!--Define custom properties-->
  2. Reference attributes in the configuration file (in the properties configuration file)

  3. 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
  4. 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:

other attributes

  • Other attributes (understand)

attribute list

  1. Custom attributes (commonly used)
  2. built-in properties
  3. Setting property
  4. Java system properties
  5. 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
  1. Define multiple environments

    <!--Define multiple environments-->
    	<!--define a specific environment:Production Environment-->
    		<!--Define the unique name corresponding to the environment-->
    		<!--Defines property values ​​specific to the environment-->
    		<!--Set default startup-->
    	<!--define a specific environment:development environment-->
  2. Using multiple environments (build process)

    mvn instruction -P environment definition id


    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

        <!--Set skip test-->
        <!--Contains the specified test case-->
        <!--Exclude specified test cases-->

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)

  • 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 categoryEnglish nameFunctionAssociated operations
host repositoryhostedPreserve independent research and development + third-party resourcesupload
Agent warehouseproxyProxy connects to central warehousedownload
warehouse groupgroupGroup for repositories to simplify download operationsdownload

Steps for usage

Local warehouse access private server permission settings

  • Configuration location (in maven's setting.xml file)
    <!-- Configure permissions to access private servers -->
      <!-- private servers id name -->
      <!-- private servers id name -->
    <!-- Configure the warehouse address of the private server -->
      <!-- Configure all resources from this warehouse -->

Project upload to private server settings

  • Configuration location (in the project pom file)

    <!--Configure private server-->
        <!--release Version-->
        <!--snapshot Version-->
    • 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

Tags: Spring Maven

Posted by naboth_abaho on Fri, 10 Mar 2023 23:42:31 +1030