SoFunction
Updated on 2025-04-11

Use and instructions in Maven

The difference between SNAPSHOT warehouse and RELEASE warehouse

During the software development stage, we will import many dependent public libraries into POM files. These public libraries may need to be modified and published at any time, and may have to be published once a day or even N times a day.

When using maven to manage dependent public libraries, since maven's dependency management is based on version management, if we already have a dependency on the public library locally, even if the components on the internal server change, if the local and dependent versions are the same as those on the server, maven will no longer pull the latest dependency on the internal server, which means that the updates on the server cannot be synchronized to the local area in real time.

If you want to solve this problem, you need to upgrade the component version number (version), but this obviously does not meet the requirements and actual situation. The SNAPSHOT snapshot version can solve this problem. Every time the component is updated on the internal server, it will be pulled again locally.

There are two types of repositories in maven:

  • snapshot snapshot repository
  • Release Release Repository

The snapshot snapshot repository is used to save unstable versions during the development process, while the release official repository is used to save stable release versions (such as Spring and SpringBoot).

To define a component/module as a snapshot version, you only need to add -SNAPSHOT to the version number of the module in the pom file, as follows:

    <modelVersion>4.0.0</modelVersion>
    <groupId></groupId>
    <artifactId>data-export-center</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>

maven will determine whether it is a snapshot version or a formal version based on whether the module version contains -SNAPSHOT.

If it is a snapshot version, it will be automatically published to the snapshot version library when mvn deploy. When using the snapshot version module, when directly compiling and packaging without changing the version number, maven will automatically download the latest snapshot version from the mirror server.

Then if it is a formal release version (release), it will be automatically released to the official version library when mvn deploy. When using the official version of the module, without changing the version number, if the module of that version already exists locally during compilation and packaging, it will not actively download it on the mirror server.

Therefore, in the development stage, we can set the version of the public library as the snapshot version, and the dependent components refer to the snapshot version for development. After the snapshot version of the public library is updated, we do not need to modify the pom file prompt version number to download the new version. We can directly execute relevant compilation and packaging commands to re-download the latest snapshot library, which is also convenient for us to develop.

distributionManagement defines SNAPSHOT repository and RELEASE repository

Usually the content format of a POM file is as follows:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;project xmlns="/POM/4.0.0"
         xmlns:xsi="http:///2001/XMLSchema-instance"
         xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."&gt;
     
    &lt;parent&gt;
        &lt;groupId&gt;&lt;/groupId&gt;
        &lt;artifactId&gt;common-server&lt;/artifactId&gt;
        &lt;version&gt;2.0.0-SNAPSHOT&lt;/version&gt;
    &lt;/parent&gt;    
     
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;&lt;/groupId&gt;
    &lt;artifactId&gt;data-export-center&lt;/artifactId&gt;
    &lt;packaging&gt;pom&lt;/packaging&gt;
    &lt;version&gt;${}&lt;/version&gt;
    
    /**
     Submodules
     */
    &lt;modules&gt;
    XXXXXXX
    &lt;/modules&gt;
    
    /**
     Custom properties
     */
    &lt;properties&gt;  
        &lt;&gt;5.1.&lt;/&gt;
        &lt;&gt;1.0.0SNAPSHOT&lt;/&gt;  
    &lt;/properties&gt; 
    /**
     Unified version dependent, submodules do not need to write version number
     */
    
	&lt;dependencyManagement&gt;
	    &lt;dependency&gt;
   	      &lt;groupId&gt;&lt;/groupId&gt;
    	  &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
    	&lt;version&gt;${}&lt;/version&gt;
	   &lt;/dependency&gt;
	&lt;dependencyManagement&gt;
    
   &lt;profiles&gt;  
        &lt;profile&gt;  
            &lt;id&gt;release&lt;/id&gt;  
        &lt;properties&gt;  
            &lt;&gt;2.0.0RELEASE&lt;/&gt;  
        &lt;/properties&gt;  
        &lt;/profile&gt;  
    &lt;/profiles&gt;  
    
    /**
     <!--Define the address of snapshots repository and releases repository-->
     */
  	&lt;distributionManagement&gt;
        &lt;repository&gt;
            &lt;id&gt;huawei&lt;/id&gt;
            &lt;url&gt;/artifactory/seeker/&lt;/url&gt;
        &lt;/repository&gt;
        &lt;snapshotRepository&gt;
            &lt;id&gt;seekerWorkds&lt;/id&gt;
            &lt;url&gt;/artifactory/seeker/&lt;/url&gt;
        &lt;/snapshotRepository&gt;
    &lt;/distributionManagement&gt;

First of all, we see that the definition of version in the pom file is in the form of placeholders. The advantage of this is that the version information can be replaced according to different profiles. For example, maven uses 1.0.0SNAPSHOT as the version of this module by default. If the mvn deploy -P release command is used during release, 2.0.0RELEASE will be automatically used as the release version. According to the rules of maven processing snapshot and release, since the version number does not contain -SNAPSHOT, it is considered to be the official release version.

It will be published to the release repository. The addresses of the snapshot snapshot library and the release publishing library are configured in the distributionManagement section.

For the version library, it mainly configures id and url, and after the configuration is completed, it can be published through mvn deploy.

Common commands and their functions

  • maven clean: Clean the project and delete the compiled content in the target directory (delete the old file class bytecode file that was previously compiled).
  • maven compile: Compile the project source code and compile the JAVA source program into a class file.
  • maven test: Run the project to test it.
  • maven packet: Packaged files and stored in the target directory of the project. The packaged files are usually compiled class files.
  • maven install: Generate the installation package of the warehouse in the local warehouse, which can be referenced by other projects, and the packaged files are placed in the target directory of the project.
  • maven deploy: You can upload the jar packages made by maven to the remote repository for easy sharing by other developers and projects.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.