preface

In the early days before using Maven, we referred to public or API jars, which we might have done by importing them manually into the project’s classpath path.

With Maven, our company may build maven private storehouses such as Nexus, upload these public jars or API jars to nexus private storehouses, and configure the coordinates of these jars in POm. XML to reference them.

Today we are going to talk about some of the common ways in which projects can be packaged and published to a Maven repository

Steps to publish to maven’s repository

1. In maven settings. XML, configure the user name and password in the < Servers > node as follows:

<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>
Copy the code

Note: ID can be regarded as an identifier first. Username and password are the username and password of the nexus vault

2. Specify the URL to publish to nexus private warehouse and publish

Method 1: Add a distributionManagement node to the POM. XML file

Form is as follows:

 <distributionManagement>
         <! -- Official version -->
        <repository>
            <! -- Settings. XML <server> id-->
            <id>nexus-releases</id>
            <url>http://192.168.0.11:8081/nexus/content/repositories/releases/</url>
        </repository>

         <! -- Snapshot version -->
        <snapshotRepository>
             <id>nexus-snapshots</id>
             <url>http://192.168.0.11:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
Copy the code

Note:

  • If parent exists, just configure it in pom.xml of parent; if not, configure it in pom.xml of this project
  • The < ID > under the < repository > node corresponds to the SERVER ID in the Maven configuration file settings. XML. The two must be the same
  • Whether the official or SNAPSHOT version is uploaded to the private warehouse depends on whether the pom. XML file version is SNAPSHOT or RELEASE. For example, your project is configured as follows
<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1 - the SNAPSHOT</version>
Copy the code

Is uploaded to private warehouse is snapshot version

Finally, run maven’s deploy command to publish the database

Method 2: In Maven’s settings. XML, the < profiles > node is configured < properties >, In < the properties > < specified altSnapshotDeploymentRepository > and < altReleaseDeploymentRepository >

Form is as follows:

<profiles>
     <profile>
	 <id>nexus</id>
     <properties>
         <altSnapshotDeploymentRepository>
             nexus-snapshots::default::http:/ / 192.168.0.11:8081 / repository/maven - snapshots
         </altSnapshotDeploymentRepository>
         <altReleaseDeploymentRepository>
            nexus-releases::default::http:/ / 192.168.0.11:8081 / repository/maven - releases /
         </altReleaseDeploymentRepository>
     </properties>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
Copy the code

Note:

  • To get the same SERVER ID as in maven’s settings. XML configuration file, get the same server ID as in Nexus -snapshots and Nexus -Releases
  • Attribute altSnapshotDeploymentRepository and altReleaseDeploymentRepository is introduced with maven – release – 2.8 version of the plugin. If the version is earlier than 2.8, the following error is displayed when the MVN deploy command is executed
Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter

Copy the code

The solution is to specify the plugins above 2.8 in the release project, as follows

<build>
        <plugins>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
            </plugin>
        </plugins>
    </build>
Copy the code

Finally, run the deploy command of Maven to publish the database

Method 3: Specify parameters using MVN deploy

  • Method one: through the -d parameter specifies altSnapshotDeploymentRepository and altReleaseDeploymentRepository

Form the following

mvn deploy -DskipTests -DaltSnapshotDeploymentRepository=nexus-snapshots::default::https://YOUR_NEXUS_URL/snapshots
-DaltReleaseDeploymentRepository=nexus-releases::default::https://YOUR_NEXUS_URL/releases
Copy the code

Similarly, to execute the above command successfully, ensure that the deploy plug-in is based on version 2.8 or later

  • Method 2: Run -d to specify information about the JAR to be published, the address of the jar, the id of the jar, and the ID of the jar to be published to be the same as the ID of the server in settings. XML

Form the following

MVN deploy:deploy-file -DskipTests -Dfile= JAR package file address (absolute path) -dgroupid = Group name -dartifactid = project name -dversion = Version number -dpackaging =jar -drepositoryID = private database ID (same as the SERVER ID in setting. XML) -durl = private database addressCopy the code

Method 4: Upload the JAR and publish it on the Visual interface of the Nexus

The following figure

The choice of several distribution methods

The first option, distribution through distributionManagement, is probably the most popular option. But if you want to release a lot of projects, we can consider to use two, through the global Settings file configuration altSnapshotDeploymentRepository and altReleaseDeploymentRepository, configure it only once, All projects can be published without having to specify multiple project POMs

Method one and method two are more suitable for the company’s own internal development projects. For some third-party JARS, You are advised to use the MVN deploy-drepositoryId = private library ID (the same as the SERVER ID in the settings. XML file) -durl = private directory address or upload the file using the Visual interface of the Nexus