maven authentication when publishing components

Posted by Oldiesmann on Wed, 19 Jan 2022 05:15:17 +0100

1. Release

mvn deploy is used to distribute the artifacts generated by the project to the remote Maven warehouse.

The components of the local Maven warehouse can only be used by the current user. After being distributed to the remote Maven warehouse, all users who can access the warehouse can use your components. To do this, you need to create a project in the POM XML to specify the location of Maven distribution components, as follows: (distributionManagement cannot be specified in setting.xml)

<project>    
  ...    
  <distributionManagement>    
    <repository>    
      <id>nexus-releases</id>    
      <name>Nexus Release Repository</name>    
      <url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>    
    </repository>    
    <snapshotRepository>    
      <id>nexus-snapshots</id>    
      <name>Nexus Snapshot Repository</name>    
      <url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>    
    </snapshotRepository>    
  </distributionManagement>    
  ...    
</project>    

Maven distinguishes between release version components and snapshot version components. Snapshot is the version in the development process, which is real-time but unstable, while release version is relatively stable. Maven will judge which warehouse to distribute the components according to the version of your project. Note: the above name tag can be omitted

Generally speaking, distributing components to remote warehouses requires authentication. If you do not configure any authentication information, you often get 401 errors. At this time, the following is in settings Configure authentication information in XML:

<settings>    
  ...    
  <servers>    
    <server>    
      <id>nexus-releases</id>    
      <username>admin</username>    
      <password>admin123</password>    
    </server>    
    <server>    
      <id>nexus-snapshots</id>    
      <username>admin</username>    
      <password>admin123</password>    
    </server>      
  </servers>    
  ...    
</settings>  

It should be noted that settings The value of id under the server element in XML must be exactly the same as the value of id under repository or snapshot repository in POM. Put the authentication information under settings instead of POM, because POM is often visible to others XML is local.

2. Get component:

In a project, if the dependent library needs to be downloaded from a repository, it can be downloaded in POM Specify repository in XML or in setting Specify repository in XML. If maven's setting on a compiler If the XML is not editable, it needs to be in the pom.xml of the project Specify repository in XML.

<repositories>
    	<repository>
    		  <id>zeus_tob</id>
		      <url>https://mirrors.abc.com/repository/maven/zeus_tob</url>
		      <releases>
		        <enabled>true</enabled>
		      </releases>
		      <snapshots>
		        <enabled>true</enabled>
		      </snapshots>
    	</repository>
</repositories>

Note: the id of any warehouse declaration must be unique. In particular, it should be noted that the id used by Maven's own central warehouse is central. If other warehouse declarations also use this id, the configuration of the central warehouse will be overwritten.

1) updatePolicy and checksumPolicy:

For releases and snapshots, in addition to the enabled element, there are two other child elements, updatePolicy and checksumPolicy:

<snapshots>
    <enabled>true</enabled>
    <checksumPolicy>ignore</checksumPolicy>
    <updatePolicy>daily</updatePolicy>
</snapshots>
  • updatePolicy: used to configure how often Maven checks for updates from the remote warehouse. The default value is daily, which means it checks once a day. Other available values include: never never never check for updates; always - check for updates every time you build; interval:X - check for updates every X minutes.
  • checksumPolicy: used to configure the policy for Maven to check the checksum file. When the component is deployed to Maven warehouse, the corresponding checksum file will be deployed at the same time. When downloading the build, Maven validates the checksum file. When the value of checksumPolicy is the default warn, Maven will output a warning message when building. When it is fail, Maven fails the build when it encounters a checksum error; ignore so that Maven completely ignores checksum errors.

2) Certification:

Components usually need authentication when they are deploy ed to a remote location; Most remote warehouses can be accessed without authentication, but sometimes for security reasons, we need to provide authentication information to access some remote warehouses.

The method is the same as deploy: settings The id of the server element in the XML must be exactly the same as the id of the repository element to be authenticated in the POM.

Topics: Java Maven intellij-idea